【问题标题】:when i try to list gmail threads using Gmail API i get GoogleJsonResponseException: 500 Internal Server Error当我尝试使用 Gmail API 列出 gmail 线程时,我得到 GoogleJsonResponseException: 500 Internal Server Error
【发布时间】:2015-03-03 03:43:39
【问题描述】:

我正在尝试使用 GMAIL API 通过 OAuth 2.0 服务器从我的电子邮件帐户下载附件到服务器应用程序。这是我的代码:

    private static final String USER = "MYACCOUNT@gmail.com";
    private static String emailAddress = "XXXXXXXX@developer.gserviceaccount.com";
    {
       try {
              httpTransport = GoogleNetHttpTransport.newTrustedTransport();
              credential = new GoogleCredential.Builder()
              .setTransport(httpTransport)
              .setJsonFactory(JSON_FACTORY)
              .setServiceAccountId(emailAddress)
              .setServiceAccountPrivateKeyFromP12File(new File("myfile.p12"))
              .setServiceAccountScopes(Collections.singleton(GmailScopes.GMAIL_READONLY))
              .build();
          }
          catch (IOException | GeneralSecurityException e)
          {
              throw new RuntimeException(e);
          }
    }
    // Create a new authorized Gmail API client
    Gmail service = new Gmail.Builder(httpTransport, JSON_FACTORY, credential)
        .setApplicationName(APP_NAME).build();

    // Retrieve a page of Threads
    ListThreadsResponse threadsResponse = service.users().threads().list(USER).execute();
      List<Thread> threads = threadsResponse.getThreads();

   // Print ID of each Thread.
    for (Thread thread : threads) {
      System.out.println("Thread ID: " + thread.getId());

}

我在调用threadsResponse 时收到错误消息。这些是错误:

线程“主”com.google.api.client.googleapis.json.GoogleJsonResponseException 中的异常:500 内部服务器错误。

{ “代码”:500, “错误”:[{ “域”:“全球”, “消息”:“后端错误”, “原因”:“后端错误” }], “消息”:“后端错误” } 在 com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145) 在 com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) 在 com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312) 在 com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460) 在 core.crescent.gmail.GmailApiQuickstart.main(GmailApiQuickstart.java:192)

【问题讨论】:

  • 如果你想访问单个用户,你可以使用单个用户的身份验证而不是服务帐户。我不认为 gmail API 支持服务帐户。 stackoverflow.com/questions/24523003/…
  • 我正在使用 OAuth2 服务器到服务器应用程序 link,因为它似乎是在未经用户同意的情况下获得访问 gmail 帐户授权的唯一方法(无需打开浏览器并进行手动同意) .有没有另一种方法可以做到这一点。谢谢

标签: java google-api gmail-api


【解决方案1】:

您可以使用指数退避重试请求。为我工作。见https://developers.google.com/drive/web/handle-errors

【讨论】:

    【解决方案2】:

    所以我发现了问题并解决了它!

    设置“凭据”时(在我上面发布的代码中)。我需要添加 .setServiceAccountUser(USER) 子例程。通过这样做,您可以在创建的服务帐户和您要访问的 GMAIL 帐户之间建立链接。 我之前在https://developers.google.com/accounts/docs/OAuth2ServiceAccount 看到过,但我的印象是,只有当您想与不同域名下的帐户关联时才会这样做。

    这解决了我的问题。谢谢大家的回复

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 2019-03-20
      • 2014-10-23
      • 1970-01-01
      相关资源
      最近更新 更多