英文:
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'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 'failedPrecondition' as below,
>Exception Details:
{
"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": "{\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}",
"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": "execute",
"fileName": "HttpRequest.java",
"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<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());
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论