我真的走投无路了。我一直在尝试弄清楚如何使用我之前从谷歌收到的现有 OAuth2 token 从 Android 应用程序访问我的 Gmail 收件箱。我可以用这个 token 来验证自己:
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());
}
所以我会继续这条路,以某种方式获取新电子邮件 =)
最佳答案
查看: Handling expired access token in Android without user concent 和 Gmail API access using Android
他们刚刚回答了关于让 oauth2 在 android 上工作(对于任何谷歌 API)的类似问题。
关于java - 在 Android 中使用 Gmail API 和 OAuth2 检查来自 Gmail 的新电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25732107/