【问题标题】:failedPrecondition error when fetching nextMessages from gmail using history ID - Google API使用历史 ID 从 gmail 获取 nextMessages 时出现 failedPrecondition 错误 - Google API
【发布时间】:2020-08-19 02:09:12
【问题描述】:

我正在尝试根据我的 gmail 邮箱中的历史 ID 获取新的电子邮件。我使用 OAuth 进行身份验证。我收到错误请求异常,原因为 failedPrecondition。但我不知道我需要在这里做什么。

参考 - History.list

这是一段代码出现错误的请求异常。有人可以帮我解决这个问题吗?

    try {
          History.List listRequest =
              this.gmail
                  .users()
                  .history()
                  .list(userId)
                  .setMaxResults(SYNC_PAGE_SIZE)
                  .setStartHistoryId(new BigInteger(historyId))
                  .setHistoryTypes(Collections.singletonList(MESSAGE_ADDED_EVENT));
          if (response != null) {
            listRequest.setPageToken(((GmailPartialSyncResponse) response).getNextPageToken());
          }

          return new GmailPartialSyncResponse(listRequest.execute());

我收到了 Bad RequestException,原因如下 'failedPrecondition',

异常详情: { "detailMessage": "myProject.exception.EmailMessageException: 无法在部分同步时获取电子邮件。", “原因”: { "detailMessage": "部分同步时获取邮件失败。", “原因”: { “状态代码”:400, "statusMessage": "错误请求", "content": "{\n \"code\" : 400,\n \"errors\" : [ {\n \"domain\" : \"global\",\n \"message\" : \" Bad Request\",\n \"reason\" : \"failedPrecondition\"\n } ],\n \"message\" : \"Bad Request\"\n}", "detailMessage": "400 Bad Request\n{\n \"code\" : 400,\n \"errors\" : [ {\n \"domain\" : \"global\",\n \"message \" : \"Bad Request\",\n \"reason\" : \"failedPrecondition\"\n } ],\n \"message\" : \"Bad Request\"\n}", “堆栈跟踪”: [ { "declaringClass": "com.google.api.client.googleapis.json.GoogleJsonResponseException", “方法名”:“来自”, "fileName": "GoogleJsonResponseException.java", “行号”:146 }, { "declaringClass": "com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest", "methodName": "newExceptionOnError", "fileName": "AbstractGoogleJsonClientRequest.java", “行号”:113 }, { "declaringClass": "com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest", "methodName": "newExceptionOnError", "fileName": "AbstractGoogleJsonClientRequest.java", “行号”:40 }, { "declaringClass": "com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1", "methodName": "interceptResponse", "fileName": "AbstractGoogleClientRequest.java", “行号”:321 }, { "declaringClass": "com.google.api.client.http.HttpRequest", “方法名”:“执行”, "fileName": "HttpRequest.java", “我……

【问题讨论】:

  • 请编辑您的问题并包含我们需要查看您如何为初学者进行身份验证的其余代码。
  • 您使用的是普通帐户还是服务帐户?可以分享一下认证步骤相关的代码吗?

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


【解决方案1】:

你应该关注Java quickstart.

创建服务

Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
            .setApplicationName(APPLICATION_NAME)
            .build();

列出历史记录。

 public static void listHistory(Gmail service, String userId, BigInteger startHistoryId)
      throws IOException {
    List<History> histories = new ArrayList<History>();
    ListHistoryResponse response = service.users().history().list(userId)
        .setStartHistoryId(startHistoryId).execute();
    while (response.getHistory() != null) {
      histories.addAll(response.getHistory());
      if (response.getNextPageToken() != null) {
        String pageToken = response.getNextPageToken();
        response = service.users().history().list(userId).setPageToken(pageToken)
            .setStartHistoryId(startHistoryId).execute();
      } else {
        break;
      }
    }

    for (History history : histories) {
      System.out.println(history.toPrettyString());
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    相关资源
    最近更新 更多