繁体   English   中英

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

[英]Check for new emails from Gmail with Gmail API and OAuth2 in Android

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

所以我将继续在这条路上获取新的电子邮件如何=)

退房: 使用Android 处理Android中过期的访问令牌,无需用户专注Gmail API访问

他们只是回答了关于让oauth2在android上运行(对于任何谷歌API)的类似问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM