首页 > 解决方案 > 使用历史 ID 从 gmail 获取 nextMessages 时出现 failedPrecondition 错误 - Google API

问题描述

我正在尝试根据我的 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”:“无法在部分同步时获取电子邮件。”,“原因”:{ "statusCode": 400, "statusMessage": "Bad Request", "content": "{\n \"code\" : 400,\n \"errors\" : [ {\n \"domain\" : \ "global\",\n \"message\" : \"Bad Request\",\n \"reason\" : \"failedPrecondition\"\n } ],\n \"message\" : \"Bad Request \"\n}", "detailMessage": "400 错误请求\n{\n \"code\" : 400,\n \"errors\" : [ {\n \"domain\" : \"global\",\n \"message\" : \" Bad Request\",\n \"reason\" : \"failedPrecondition\"\n } ],\n \"message\" : \"Bad Request\"\n}", "stackTrace": [ { "declaringClass ": "com.google.api.client.googleapis.json.GoogleJsonResponseException", "methodName": "from", "fileName": "GoogleJsonResponseException.java", "lineNumber": 146 }, { "declaringClass": "com .google.api.client.googleapis.services.json。AbstractGoogleJsonClientRequest”,“methodName”:“newExceptionOnError”,“fileName”:“AbstractGoogleJsonClientRequest.java”,“lineNumber”:113 },{“declaringClass”:“com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest” ,“methodName”:“newExceptionOnError”,“fileName”:“AbstractGoogleJsonClientRequest.java”,“lineNumber”:40 },{“declaringClass”:“com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1”,“methodName”:“interceptResponse”,“fileName”:“AbstractGoogleClientRequest.java”,“lineNumber”:321 },{“declaringClass”:“com.google.api.client.http.HttpRequest”,“methodName”:“执行” , "fileName": "HttpRequest.java", "li...“执行”、“文件名”:“HttpRequest.java”、“li...“执行”、“文件名”:“HttpRequest.java”、“li...

标签: javagoogle-apigmail-apigoogle-api-java-client

解决方案


您应该遵循Java 快速入门。

创建服务

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());
    }
  }

推荐阅读