獲取錯誤 {“error”:“invalid_grant”,“error_description”:“令牌已過期或撤銷。” 來自谷歌 oauth2 API (Getting error { "error" : "invalid_grant", "error_description" : "Token has been expired or revoked." } from Google oauth2 API)


問題描述

獲取錯誤 {“error”:“invalid_grant”,“error_description”:“令牌已過期或撤銷。” 來自谷歌 oauth2 API (Getting error { "error" : "invalid_grant", "error_description" : "Token has been expired or revoked." } from Google oauth2 API)

早在 2016 年,我就在我的谷歌開發者控制台帳戶中創建了一個應用程序 & 我有客戶端 ID、客戶端密碼、訪問令牌和 此應用的刷新令牌。我正在嘗試使用我的 apache camel 應用程序中的 google:mail API 從我的 Gmail 帳戶中讀取電子郵件。直到今天它都工作正常& 突然我收到這個錯誤,上面寫著:

c.g.api.client.auth.oauth2.Credential    : unable to refresh token

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "invalid_grant",
"error_description" : "Bad Request"
}
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:362)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.google.api.client.auth.oauth2.Credential.handleResponse(Credential.java:272)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:999)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at org.apache.camel.component.google.mail.GoogleMailProducer.doInvokeMethod(GoogleMailProducer.java:49)
at org.apache.camel.util.component.AbstractApiProducer$1.run(AbstractApiProducer.java:86)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

2020‑07‑20 11:31:02.689 WARN 20885 ‑‑‑ [CamelGoogleMail] o.a.camel.component.timer.TimerConsumer : Error processing exchange. Exchange[ID‑FVFXN5JKHV29‑51050‑1595224856126‑0‑2]. Caused by: [org.apache.camel.RuntimeCamelException ‑ com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign‑in/web/devconsole‑project.",
"status" : "UNAUTHENTICATED"
}]
</code></pre>

這是因為訪問令牌或刷新令牌在 2016 年之後今天過期了嗎?我應該嘗試從谷歌開發者控制台重置客戶端密碼嗎?

編輯 ‑ 這是我正在使用的一段代碼

public GoogleMailComponent googleMailComponent() {
GoogleMailConfiguration googleMailConfiguration = new GoogleMailConfiguration();
googleMailConfiguration.setClientId(clientId);
googleMailConfiguration.setClientSecret(clientSecret);
googleMailConfiguration.setAccessToken(accessToken);
googleMailConfiguration.setRefreshToken(refreshToken);

    GoogleMailComponent googleMailComponent = new GoogleMailComponent();
    googleMailComponent.setConfiguration(googleMailConfiguration);
    return googleMailComponent;
}

</code></pre>


參考解法

方法 1:

Deleting tokens/StoredCredential file allowed me to solve the issue.

Next time when I ran my Java application which was trying to connect to Google Sheets, it prompted me to

  1. Go to a URL
  2. Allow access to Google API

(by Amey Lokhandesaurabh.in)

參考文件

  1. Getting error { "error" : "invalid_grant", "error_description" : "Token has been expired or revoked." } from Google oauth2 API (CC BY‑SA 2.5/3.0/4.0)

#google-api #gmail-api #google-oauth






相關問題

在 PHP 中壓縮 JSON 字符串並在 Javascript 中解壓縮以進行 Google API 的數據庫查詢 (Compress JSON string in PHP and decompress in Javascript for Database query for Google API)

將 Google Places API 與 MonoTouch 一起使用? (Using Google Places API with MonoTouch?)

如何獲取我的 Gmail 帳戶的個人資料圖片? (How to get the profile picture of my Gmail account?)

google.elements.newsShow 顯示時間不起作用 (google.elements.newsShow display Time not working)

gapi.client.load 未調用回調:console.log 中指定了無效或非法字符串錯誤 (gapi.client.load not calling callback: An invalid or illegal string was specified error in console.log)

Google Developer Console 和已安裝的應用 (Google Developer Console and Installed App)

如何從 Google 自定義搜索 API 獲得 100 多個結果 (How to get more than 100 results from Google Custom Search API)

Pandas / Google Analytics API 身份驗證嘗試給我帶來了一個奇怪的 python 錯誤 (Pandas / Google Analytics API authentication attempt throws me a weird python error)

在 alpha 階段使用谷歌云功能進行生產 (Using a google cloud feature in alpha stage for production)

流量中的 Google Maps Distance Matrix API 持續時間添加返回錯誤結果的所有段 (Google Maps Distance Matrix API duration in traffic adding all segments returning wrong result)

Python 事件集成中的 QML Calendar 和 Google Calendar API (QML Calendar and Google Calendar API in Python events integration)

獲取錯誤 {“error”:“invalid_grant”,“error_description”:“令牌已過期或撤銷。” 來自谷歌 oauth2 API (Getting error { "error" : "invalid_grant", "error_description" : "Token has been expired or revoked." } from Google oauth2 API)







留言討論