访问 Google API 时授权无效 [英] Invalid grant when accessing Google API

查看:23
本文介绍了访问 Google API 时授权无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用服务帐户"授权访问来调用任何 Google API.我已经下载了.pk2"文件并在 Google API 控制台的服务"选项卡中激活了URL Shortener API".每当我尝试调用任何 API(URL 缩短器或 Adsense)时.我有以下异常 -

I'm trying to invoke any of the Google API using "Service account" authorization access. I have downloaded ".pk2" file and activated "URL Shortener API" in Services tab of Google API console. Whenever I try to invoke any API (URL shortener or Adsense). I've got following exception -

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_grant"
}
    at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:303)
    at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:323)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:345)
    at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:526)
    at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:287)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:836)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)

下面是代码片段 -

    HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    JsonFactory JSON_FACTORY = new JacksonFactory();
    File privateKey = new File(ReportAdsense.class.getResource("mykey.p12").toURI());
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId("my_valid_account_id@developer.gserviceaccount.com")
            .setServiceAccountScopes(UrlshortenerScopes.URLSHORTENER)
            .setServiceAccountPrivateKeyFromP12File(privateKey)
            .build();
     Urlshortener service = new Urlshortener.Builder(new NetHttpTransport(), JSON_FACTORY, null).setHttpRequestInitializer(credential).build();
     UrlHistory history = service.url().list().execute();

推荐答案

首先,服务帐户"不适用于 Adsense,因为它需要用户授权.因此,对于 Adsense,您应该使用 Oauth 2.0.当您第一次使用 URL https://accounts.google.com/o/oauth2/token 获得授权时,复制粘贴并硬编码您的刷新令牌.您可以使用它来获取访问令牌,指定 client_id、client_secret 和您的 refresh_token 以获取新的访问令牌.现在可以在您的应用程序中使用访问令牌.

First of all "Service account" will not work for Adsense, since it requires user authorization. Hence for Adsense you should use Oauth 2.0. When you are authorized first time using URL https://accounts.google.com/o/oauth2/token, copy-paste and hardcode your refresh token. Than you can use it to get access token, specify client_id, client_secret and your refresh_token to get new access token. Now access token can be used in your application.

关于您的错误,我遇到了类似的问题,并花了很多时间来解决它.首先,确保您使用的是有效的 ServiceAccountId - 它应该指向以developer.gserviceaccount.com"结尾的电子邮件.确保您在 Google Console API 中指定了帐户范围和激活的服务.

Regarding your error, I have faced with similar issue and spent plenty of time to resolve it. First of all, make sure that you are using valid ServiceAccountId - it should point to email which finishes with "developer.gserviceaccount.com". Make sure, that you specified account scopes and activated services in Google Console API.

我通过同步机器中的系统时钟解决了这个问题.

I fixed this issue by synchronizing system clock in my machine.

有很多类似错误的主题没有答案.更重要的是,有些人说,有时它有效,有时它返回无效的授权.它可以在一台机器上工作,而在另一台机器上失败.我不知道这是否是系统时钟问题,但我会避免使用服务帐户 API,因为看起来存在错误并且支持对您没有帮助

There are a lot of topics with similar error without answers. Even more, some people says, that sometimes it works, sometimes it returns invalid grant. It could work on one machine and fail on another. I don't know if it is system clock issue, but I would avoid using Service Account API, since looks like there are bugs and support would not help you

这篇关于访问 Google API 时授权无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆