2016-10-18 8 views
2

私はguideに続いて、Gmailスコープで読み取り専用のアカウントアクセスをユーザーに要求し、authCodeを生成し、バックエンドサーバーに送信するAndroidアプリケーションを作成しています。accessTokenでGmail SDKを使用してGmailにアクセスしますか?

バックエンドサーバーはauthCodeを受信し、acessTokenを生成します。ここではそのためのコードがあります:今、このアクセスを使用して

private static String CLIENT_SECRET_FILE = "/client_secret.json"; 

private static String REDIRECT_URI = ""; 

String accessToken = null; 

// Exchange auth code for access token 
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.class.newInstance(), 
       new FileReader(CLIENT_SECRET_FILE)); 

GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(), 
       JacksonFactory.class.newInstance(), "https://www.googleapis.com/oauth2/v4/token", 
       clientSecrets.getDetails().getClientId(), clientSecrets.getDetails().getClientSecret(), authCode, 
       REDIRECT_URI).execute(); 

accessToken = tokenResponse.getAccessToken(); 

は、バックエンドサーバはGmailのSDKを使用してGmailのにアクセスする必要があるトークン。

どうすればよいですか?

答えて

0

私はついにそれを考え出したいくつかの時間のためにドキュメントを通過した後:このGmailのオブジェクトを使用し

static final String APPLICATION_NAME = "POC"; 

/** 
* Global instance of the JSON factory. 
*/ 
static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); 

/** Global instance of the HTTP transport. */ 
static HttpTransport HTTP_TRANSPORT; 

public Gmail getGmailService(String accessToken) throws IOException { 
    GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken); 
    return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build(); 
} 

あなたはその後、さらに要求を行うことができます。

関連する問題