【问题标题】:Check for new emails from Gmail with Gmail API and OAuth2 in Android在 Android 中使用 Gmail API 和 OAuth2 检查来自 Gmail 的新电子邮件
【发布时间】:2014-11-02 03:15:54
【问题描述】:

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

所以我将继续沿着这条路以某种方式获取新电子邮件 =)

【问题讨论】:

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


    【解决方案1】:

    查看: Handling expired access token in Android without user concentGmail API access using Android

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

    【讨论】:

      猜你喜欢
      • 2020-03-23
      • 2022-06-18
      • 2023-03-14
      • 1970-01-01
      • 2020-07-21
      • 2020-05-21
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      相关资源
      最近更新 更多