简体   繁体   English

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

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

I'm really at a dead end. 我真的走到了尽头。 I've been trying to figure out how to reach my Gmail-inbox from an Android-app with an existing OAuth2-token that I have received from Google moments before. 我一直试图弄清楚如何使用我之前从Google收到的现有OAuth2令牌从Android应用程序到达我的Gmail收件箱。 I can auth myself with a token with this: 我可以用一个令牌来证明自己:

URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" +token);
HttpURLConnection con = (HttpURLConnection) url.openConnection();

But it feels like I'm going at this problem at a wrong angle, somehow. 但感觉就像我以某种方式以错误的角度解决这个问题。 I've been trying to decipher Gmail's API but haven't gotten anywhere. 我一直试图破译Gmail的API,但没有得到任何结果。 Can somebody push me in the right direction here? 有人能把我推向正确的方向吗?

Or if the code I posted is correct, how would I proceed from that? 或者,如果我发布的代码是正确的,我将如何继续?

EDIT: So I've figured out how to use Gmail's API to get the latest 100 mails with this code: 编辑:所以我已经找到了如何使用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());
}

So I will continue on this path to get new emails some how =) 所以我将继续在这条路上获取新的电子邮件如何=)

Check out: Handling expired access token in Android without user concent and Gmail API access using Android 退房: 使用Android 处理Android中过期的访问令牌,无需用户专注Gmail API访问

They just had their similar question answered about getting oauth2 working on android (for any google API). 他们只是回答了关于让oauth2在android上运行(对于任何谷歌API)的类似问题。

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

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