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

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

我真的走投无路了。我一直在尝试弄清楚如何使用我之前从谷歌收到的现有 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 concentGmail API access using Android

他们刚刚回答了关于让 oauth2 在 android 上工作(对于任何谷歌 API)的类似问题。

关于java - 在 Android 中使用 Gmail API 和 OAuth2 检查来自 Gmail 的新电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25732107/

相关文章:

java - 如何从 JPA 中的持久性异常中获取不同类型的错误?

java - BufferedReader和BufferedInputStream默认缓冲区是8192什么的?分别是字符和字节?

oauth - 请求 YouTube Analytics API 时出现 403 禁止错误

android - 如何在Android应用程序中访问谷歌开发者API

java - 如何检索JSP中的内容/文件?

java - 消费者在 Apache Kafka 中消费消息的延迟

android eclipse 在布局中使用字符串资源作为文本大小

android - Facebook/Twitter 集成在我的 Android 应用程序中

android - 在 RenderScript 中实现算法

python - 谷歌驱动器 API : The user has not granted the app error