我正在尝试从我的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());我得到的坏RequestException的理由是'failedPrecondition‘如下,
异常详细信息:{ "detailMessage":"myProject.exception.EmailMessageException:未能在部分同步时提取电子邮件。“,”原因“:{ "detailMessage":”无法在部分同步时获取电子邮件。“,”原因“:{ "statusCode":400,"statusMessage":”坏请求“,“内容”:"{\n \“代码\ 400,\n \”错误\“:{\n \”域\“:\”全局\“,\n \”消息\“:\”坏请求\“,\n \”原因\“:\”故障预条件\“\n },\n\”消息\“:\”坏请求\\n}“,"detailMessage":“400BAD请求\n {\n \”代码\ 400,\n \“错误\”:{ \n \“域\”:\“全局\”,\n \“消息\”:\“坏请求\”,\n\“原因\”:\“故障预条件\”\n},\n\“消息\”:\“坏请求”,"stackTrace":[{ "declaringClass":declaringClass "methodName":"from","fileName":"GoogleJsonResponseException.java","lineNumber":146 },{ "declaringClass":declaringClass "methodName":"newExceptionOnError","fileName":"AbstractGoogleJsonClientRequest.java","lineNumber":113 },{ "declaringClass":declaringClass "methodName":"newExceptionOnError","fileName":"AbstractGoogleJsonClientRequest.java","lineNumber":40 },{ "declaringClass":declaringClass "methodName":"interceptResponse","fileName":"AbstractGoogleClientRequest.java","lineNumber":321 },{ "declaringClass":"com.google.api.client.http.HttpRequest","methodName":"execute","fileName":"HttpRequest.java",“li.
发布于 2020-05-05 06:19:13
你应该遵循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());
}
}https://stackoverflow.com/questions/61595793
复制相似问题