我正在嘗試使用Google Java API服務(wù)基于Gmail REST API發(fā)送郵件。我已經(jīng)通過Google Develover Console配置了應(yīng)用程序客戶端,并下載了p12和json文件。我已經(jīng)使用了以下示例程序,https://developers.google.com/gmail/api/guides/sending#sending_messages ...該示例有效,但這是基于GoogleAuthorizationCodeFlow的。我只想在服務(wù)器之間進(jìn)行工作,直接調(diào)用,而不是打開瀏覽器來獲取訪問令牌...而我得到了它(訪問令牌),但最終我收到了錯(cuò)誤的請(qǐng)求。...為什么? ?我收到的不僅僅是“錯(cuò)誤的請(qǐng)求”和“前提條件失敗”的更多信息基于此,我執(zhí)行以下步驟:第一步:根據(jù)我的客戶帳戶郵件和p12生成的文件創(chuàng)建一個(gè)GoogleCredential對(duì)象:GoogleCredential credential = new GoogleCredential.Builder().setTransport(new NetHttpTransport()) .setJsonFactory(new JacksonFactory()) .setServiceAccountId(serviceAccountUserEmail) .setServiceAccountScopes(scopes) .setServiceAccountPrivateKeyFromP12File( new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH)) .build();在這里我必須指出,我在使用ClientID而不是ClientMail時(shí)遇到很多問題。它必須使用@ developer.gserviceaccount.com帳戶而不是.apps.googleusercontent.com。如果您沒有發(fā)送此參數(shù)ok,則會(huì)收到“ INVALID GRANT”錯(cuò)誤。對(duì)此進(jìn)行了說明:https : //developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization第二步:根據(jù)憑據(jù)創(chuàng)建Gmail服務(wù):Gmail gmailService = new Gmail.Builder(httpTransport, jsonFactory, credential) .setApplicationName(APP_NAME) .build();第五步:執(zhí)行后獲取結(jié)果...這里我收到異常:線程“主”中的異常com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request{ "code" : 400, "errors" : [ { "domain" : "global", "message" : "Bad Request", "reason" : "failedPrecondition" } ], "message" : "Bad Request"} at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)知道什么是錯(cuò)的或前提條件失敗了嗎?
Gmail REST API:400錯(cuò)誤的請(qǐng)求+失敗的前提條件
桃花長(zhǎng)相依
2019-12-05 15:16:29