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

Java 在Android中使用Gmail API和OAuth2检查来自Gmail的新电子邮件,java,android,google-oauth,gmail-api,google-oauth-java-client,Java,Android,Google Oauth,Gmail Api,Google Oauth Java Client,我真的走投无路了。我一直在试图弄清楚如何通过Android应用程序访问我的Gmail收件箱,该应用程序带有我刚才从谷歌收到的OAuth2令牌。我可以使用以下令牌对自己进行身份验证: URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" +token); HttpURLConnection con = (HttpURLConnection) url.openConnection(); 但不知

我真的走投无路了。我一直在试图弄清楚如何通过Android应用程序访问我的Gmail收件箱,该应用程序带有我刚才从谷歌收到的OAuth2令牌。我可以使用以下令牌对自己进行身份验证:

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());
}
HttpTransport-HttpTransport=new-NetHttpTransport();
JsonFactory JsonFactory=新的JacksonFactory();
GoogleTokenResponse=新的GoogleTokenResponse();
response.setAccessToken(myExistingToken);
GoogleCredential credential=new GoogleCredential().setFromTokenResponse(响应);
Gmail服务=new Gmail.Builder(httpTransport,jsonFactory,credential).setApplicationName(myApplicationName).build();
ListThreadsResponse threadsResponse=service.users().threads().list(“voxcommunis@gmail.comexecute();
List threads=threadsResponse.getThreads();
对于(com.google.api.services.gmail.model.Thread线程:线程){
Log.d(Log_标记,“线程ID:+Thread.getId());
}
因此,我将继续沿着这条路径获取新的电子邮件some how=)

签出: 和

他们刚刚回答了关于让oauth2在android上工作的类似问题(适用于任何google API)