我遵循这个指南构建了一个Android应用程序,该应用程序请求用户以只读的方式访问Gmail作用域,然后生成一个authCode并将其发送到后端服务器。
后端服务器然后接收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访问Gmail。
我怎样才能做到这一点?
发布于 2016-10-20 04:22:02
在阅读了几个小时的文档之后,我终于明白了这一点:
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();
}使用这个Gmail对象,您可以提出进一步的请求。
https://stackoverflow.com/questions/40100842
复制相似问题