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

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

问题描述

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

  com.google.api.client.auth.oauth2.TokenResponseException:400错误请求
{
error:invalid_grant
}
,位于com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:303)
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)
位于com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:287)
位于com.google.api.client.http.HttpRequest。执行(HttpRequest.java:836)
,位于com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
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凭证=新的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来获得新的访问令牌。现在访问令牌可以在您的应用程序中使用。



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

我通过在机器中同步系统时钟来解决此问题。

有很多没有答案的类似错误的主题。更有甚者,有人说,有时候它有效,有时会返回无效的赠款。它可以在一台机器上运行并在另一台机器上运行。我不知道这是否是系统时钟问题,但我会避免使用服务帐户API,因为看起来像有错误和支持不会帮助您


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)

Below is code snippet -

    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();

解决方案

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.

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.

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天全站免登陆