Google api 从Google oauth2 API获取错误{quot;error";:“invalid”u grant";,“error”description:“Token已过期或吊销。”

Google api 从Google oauth2 API获取错误{quot;error";:“invalid”u grant";,“error”description:“Token已过期或吊销。”,google-api,gmail-api,google-oauth,Google Api,Gmail Api,Google Oauth,2016年,我在我的google开发者控制台帐户中创建了一个应用程序&我拥有该应用程序的客户端ID、客户端密码、访问令牌和刷新令牌。我正在尝试使用apache camel应用程序中的google:mail API从我的Gmail帐户读取电子邮件。直到今天,它一直运行良好&突然我发现这个错误,它说: c.g.api.client.auth.oauth2.Credential : unable to refresh token com.google.api.client.auth.oauth

2016年,我在我的google开发者控制台帐户中创建了一个应用程序&我拥有该应用程序的客户端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"
}]
这是因为访问令牌或刷新令牌在2016年后的今天已过期吗?我是否应该尝试从google开发者控制台重置客户端密码

编辑-这是我正在使用的一段代码

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;
    }

删除tokens/StoredCredential文件使我能够解决这个问题

下一次当我运行Java应用程序试图连接到GoogleSheets时,它提示我

  • 转到URL
  • 允许访问谷歌API

  • 你能出示你的密码吗?还有:你在使用服务帐户吗?嗨@ziganotschka,我想我没有使用服务帐户。你能告诉我在哪里可以查清楚吗?谢谢我这样问是因为在使用服务帐户时,您的错误很常见,但如果您使用了服务帐户,您可能会知道。有关服务帐户的信息,请参阅。