我正在尝试根据我的Gmail邮箱中的历史记录ID提取新的电子邮件。我使用OAuth进行身份验证。我收到错误的请求异常,原因为failPrecondition。但我不知道该怎么做。
参考-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 \“:\”错误的请求\“,\ n \”原因\“:\”失败的前提条件“” \ n}],\ n \“消息\”:\“错误的请求\” \ n}“,“ detailMessage”:“ 400错误的请求\ n {\ n \” code \“:400,\ n \” errors \“:[{\ n \” domain \“:\” global \“,\ n \”消息\“:\”错误请求\“,\ n \”原因\“:\”失败的前提条件“” \ n}],\ n \“消息\”:\“错误请求\” \ n}“,“堆栈跟踪”: [{“ declaringClass”:“ com.google.api.client.googleapis.json.GoogleJsonResponseException”,“ methodName”:“来自”,“ 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 ...
您应该遵循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());
}
}