Google Analytics API 使用服务帐户的异常“invalid_grant”.两台服务器上的代码相同.只有


Google Analytics API oauth exception "invalid_grant" with Service Account. Same code on two servers. Only one works

我正在通过服务帐户查询分析 API。

我已经在开发服务器上编写了代码,它可以正常工作。在生产服务器上运行相同的代码时,它会抛出以下内容:

Google_AuthException:刷新 OAuth2 令牌时出错,消息:"{"错误" : "invalid_grant" }'

我尝试创建另一个服务帐户,行为相同。

oAuth IETF 草案 (https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-31) 对错误是这样说的:

     invalid_grant
           The provided authorization grant (e.g. authorization
           code, resource owner credentials) or refresh token is
           invalid, expired, revoked, does not match the redirection
           URI used in the authorization request, or was issued to
           another client.

这是我编写的代码:

$GA_CLIENT_ID = 'XX.apps.googleusercontent.com';
$GA_APP_EMAIL = 'XX@developer.gserviceaccount.com';
$GA_APP_NAME = 'XX';
$GA_KEY_FILE = 'XX';
// create client object and set app name
$client = new Google_Client();
$client->setApplicationName($GA_APP_NAME); // name of your app
// set assertion credentials
$client->setAssertionCredentials(
        new Google_AssertionCredentials(
            $GA_APP_EMAIL, // email you added to GA
            array('https://www.googleapis.com/auth/analytics.readonly'),
            file_get_contents($GA_KEY_FILE)  // keyfile you downloaded
            ));
// other settings
$client->setClientId($GA_CLIENT_ID);           // from API console
$client->setAccessType('offline_access');  // this may be unnecessary?
// create service and get data
$service = new Google_AnalyticsService($client);
$result = $service->data_ga->get($ids, $startDate, $endDate, $metrics, $optParams);
return $result;

我还尝试了这里建议的解决方案(https://groups.google.com/forum/?fromgroups#!topic/gs-discussion/3y_2XVE2q7U%5B1-25%5D),使用authenticatedRequest()而不是Google_AnalyticsService:

$req = new Google_HttpRequest($apiUrl);
$resp = $client::getIo()->authenticatedRequest($req);
$result = json_decode($resp->getResponseBody(), true);

此替代方法也适用于开发服务器,但不适用于生产服务器。

我对此完全一无所知。有没有人看到这个/修复它?

谢谢!

显然问题是系统时间关闭。 通过 NTP 与以下人员同步

sudo ntpdate npt.ubuntu.com

sudo ntpdate pool.ntp.org

编辑

如下@RafaSashi建议,pool.ntp.org服务器更可靠。使用它而不是ntp.ubuntu.com(这是我尝试的第一个工作,因此是最初的选择)。

如果您使用了错误的"服务帐户 ID",也可能导致无效的授权。它应该是与 Google API 访问权限页面中服务帐号客户端 ID 中的客户端 ID 关联的电子邮件。您还必须将此用户添加到您计划访问的Google Analytics帐户中。

这让我感到困惑,因为我认为他们所指的电子邮件地址是我的谷歌帐户的电子邮件地址,因为我使用相同的谷歌帐户来获得 api 访问权限,就像我为谷歌分析所做的那样。我知道 Vir 已经弄清楚了他的问题,只是想我会添加这个,以防其他人遇到同样的问题,并且像我一样,他们的计算机似乎与 NTP 同步。

除了Valer的回答:

首先,如果尚未安装 NTP,则需要安装它。对于 Debian 或 Ubuntu,这将是这个命令:

sudo apt-get install ntp

对于 Redhat 或 CentOS,你需要使用这个:

yum install ntp

如果通过npt.ubuntu.com同步不起作用,请尝试:

sudo ntpdate pool.ntp.org

资源

  • http://www.howtogeek.com/tips/how-to-sync-your-linux-server-time-with-network-time-servers-ntp/

  • https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-ubuntu-12-04

invalid_grant错误有两个主要原因,在刷新令牌和访问令牌的 POST 请求之前,您必须小心。

  1. 请求标头必须包含"内容类型:application/x-www-form-urlencoded"
  2. 您的请求有效负载应该是 url 编码的表单数据,不要作为 json 对象发送。

RFC 6749 OAuth 2.0 invalid_grant定义为:提供的授权授予(例如,授权代码、资源所有者凭据)或刷新令牌无效、已过期、已吊销、与授权请求中使用的重定向 URI 不匹配,或已颁发给其他客户端。

我找到了另一篇好文章,在这里你会发现此错误的许多其他原因。

https://blog.timekit.io/google-oauth-invalid-grant-nightmare-and-how-to-fix-it-9f4efaf1da35

谷歌游乐场是帮助您如何发送请求的最佳工具。https://developers.google.com/oauthplayground