failedPrecondition error when fetching nextMessages from gmail using history ID – Google API

huangapple 未分类评论46阅读模式
英文:

failedPrecondition error when fetching nextMessages from gmail using history ID - Google API

问题

我正在尝试基于历史记录 ID 从我的 Gmail 邮箱中获取新的电子邮件消息。我使用 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());
} catch (Exception ex) {
    throw new EmailMessageException("Failed to fetch emails on partial sync.", ex);
}

我得到了一个带有失败前提条件('failedPrecondition')原因的坏请求异常,如下所示:

异常详细信息:
{
"detailMessage": "myProject.exception.EmailMessageException: Failed to fetch emails on partial sync.",
"cause": {
"detailMessage": "Failed to fetch emails on partial sync.",
"cause": {
"statusCode": 400,
"statusMessage": "Bad Request",
"content": "{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Bad Request",
"reason" : "failedPrecondition"
} ],
"message" : "Bad Request"
}",
...
},
...
},
...
}


<details>
<summary>英文:</summary>

I am trying to fetch new email messages based on history ID from my gmail mailbox. I do authentication using OAuth. I am got Bad Request Exception with reason as failedPrecondition.  but I can&#39;t figure out what I need to do here.

Reference - [History.list](https://developers.google.com/gmail/api/v1/reference/users/history/list)



Here is the piece of code getting bad Request Exception.  Can someone help me on this issue.
        
        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());
         
         
I am getting the Bad RequestException with reason as &#39;failedPrecondition&#39; as below,
        
&gt;Exception Details:
        {
          &quot;detailMessage&quot;: &quot;myProject.exception.EmailMessageException: Failed to fetch emails on partial sync.&quot;,
          &quot;cause&quot;: {
            &quot;detailMessage&quot;: &quot;Failed to fetch emails on partial sync.&quot;,
            &quot;cause&quot;: {
              &quot;statusCode&quot;: 400,
              &quot;statusMessage&quot;: &quot;Bad Request&quot;,
              &quot;content&quot;: &quot;{\n  \&quot;code\&quot; : 400,\n  \&quot;errors\&quot; : [ {\n    \&quot;domain\&quot; : \&quot;global\&quot;,\n    \&quot;message\&quot; : \&quot;Bad Request\&quot;,\n    \&quot;reason\&quot; : \&quot;failedPrecondition\&quot;\n  } ],\n  \&quot;message\&quot; : \&quot;Bad Request\&quot;\n}&quot;,
              &quot;detailMessage&quot;: &quot;400 Bad Request\n{\n  \&quot;code\&quot; : 400,\n  \&quot;errors\&quot; : [ {\n    \&quot;domain\&quot; : \&quot;global\&quot;,\n    \&quot;message\&quot; : \&quot;Bad Request\&quot;,\n    \&quot;reason\&quot; : \&quot;failedPrecondition\&quot;\n  } ],\n  \&quot;message\&quot; : \&quot;Bad Request\&quot;\n}&quot;,
              &quot;stackTrace&quot;: [
                {
                  &quot;declaringClass&quot;: &quot;com.google.api.client.googleapis.json.GoogleJsonResponseException&quot;,
                  &quot;methodName&quot;: &quot;from&quot;,
                  &quot;fileName&quot;: &quot;GoogleJsonResponseException.java&quot;,
                  &quot;lineNumber&quot;: 146
                },
                {
                  &quot;declaringClass&quot;: &quot;com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest&quot;,
                  &quot;methodName&quot;: &quot;newExceptionOnError&quot;,
                  &quot;fileName&quot;: &quot;AbstractGoogleJsonClientRequest.java&quot;,
                  &quot;lineNumber&quot;: 113
                },
                {
                  &quot;declaringClass&quot;: &quot;com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest&quot;,
                  &quot;methodName&quot;: &quot;newExceptionOnError&quot;,
                  &quot;fileName&quot;: &quot;AbstractGoogleJsonClientRequest.java&quot;,
                  &quot;lineNumber&quot;: 40
                },
                {
                  &quot;declaringClass&quot;: &quot;com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1&quot;,
                  &quot;methodName&quot;: &quot;interceptResponse&quot;,
                  &quot;fileName&quot;: &quot;AbstractGoogleClientRequest.java&quot;,
                  &quot;lineNumber&quot;: 321
                },
                {
                  &quot;declaringClass&quot;: &quot;com.google.api.client.http.HttpRequest&quot;,
                  &quot;methodName&quot;: &quot;execute&quot;,
                  &quot;fileName&quot;: &quot;HttpRequest.java&quot;,
                  &quot;li...
    
    
   

</details>


# 答案1
**得分**: 0

你应该遵循[Java快速入门](https://developers.google.com/gmail/api/quickstart/java)。

# 创建服务

```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());
    }
}
英文:

You should probaby follow the Java quickstart.

#Create service

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

#List History.

 public static void listHistory(Gmail service, String userId, BigInteger startHistoryId)
      throws IOException {
    List&lt;History&gt; histories = new ArrayList&lt;History&gt;();
    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());
    }
  }

huangapple
  • 本文由 发表于 2020年5月4日 23:26:29
  • 转载请务必保留本文链接:https://java.coder-hub.com/61595793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定