3

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

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

4

1 回答 1

1

签出: 在没有用户集中的情况下处理 Android 中过期的访问令牌使用 Android 访问 Gmail API

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

于 2014-09-09T16:51:47.510 回答