在Android中使用Gmail API和OAuth2检查来自Gmail的新电子邮件

时间:2014-09-08 19:53:28

标签: java android google-oauth gmail-api google-oauth-java-client

我真的走到了尽头。我一直试图弄清楚如何使用我之前从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());
}

所以我会继续这条路来获取新的电子邮件,如何=)

1 个答案:

答案 0 :(得分:1)

退房: Handling expired access token in Android without user concentGmail API access using Android

他们只是回答了关于让oauth2在Android上工作的问题(对于任何谷歌API)。

相关问题