【发布时间】:2014-11-02 03:15:54
【问题描述】:
我真的走投无路了。我一直在试图弄清楚如何使用我之前从 Google 收到的现有 OAuth2 令牌从 Android 应用程序访问我的 Gmail 收件箱。我可以使用以下令牌对自己进行身份验证:
URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" +token);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
但不知何故,我感觉好像我从一个错误的角度来解决这个问题。我一直在尝试破译 Gmail 的 API,但没有得到任何结果。有人可以在这里把我推向正确的方向吗?
或者如果我发布的代码是正确的,我将如何处理?
编辑:所以我想出了如何使用 Gmail 的 API 使用此代码获取最新的 100 封邮件:
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleTokenResponse response = new GoogleTokenResponse();
response.setAccessToken(myExistingToken);
GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);
Gmail service = new Gmail.Builder(httpTransport, jsonFactory, credential).setApplicationName(myApplicationName).build();
ListThreadsResponse threadsResponse = service.users().threads().list("voxcommunis@gmail.com").execute();
List<com.google.api.services.gmail.model.Thread> threads = threadsResponse.getThreads();
for ( com.google.api.services.gmail.model.Thread thread : threads ) {
Log.d(LOG_TAG, "Thread ID: "+ thread.getId());
}
所以我将继续沿着这条路以某种方式获取新电子邮件 =)
【问题讨论】:
标签: java android google-oauth gmail-api google-oauth-java-client