Google Analytics API oauth异常“invalid_grant”与服务帐户。两个服务器上的代码相同。只有一件作品 [英] Google Analytics API oauth exception "invalid_grant" with Service Account. Same code on two servers. Only one works

查看:202
本文介绍了Google Analytics API oauth异常“invalid_grant”与服务帐户。两个服务器上的代码相同。只有一件作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过服务帐户

I'm querying the Analytics API via a Service Account.

我已经在开发者服务器上编写了代码,并且没有的问题。
在生产服务器上运行相同的代码时,会抛出以下错误:

I have written the code on the dev server and it works without issues. When running the same code on the production server, it throws this:


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

Google_AuthException: Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'

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

I've tried creating another Service account, and the behavior is the same.

oAuth IETF草案( http://tools.ietf.org/html/draft-ietf-oauth-v2-31 )就这个错误说:

The oAuth IETF draft (http://tools.ietf.org/html/draft-ietf-oauth-v2-31) says this about the error:

     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-讨论/ 3y_2XVE2q7U%5B1-25%5D )使用authenticatedRequest()而不是Google_AnalyticsService:

I've also tried a solution suggested here (https://groups.google.com/forum/?fromgroups#!topic/gs-discussion/3y_2XVE2q7U%5B1-25%5D) using authenticatedRequest() instead of Google_AnalyticsService:

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

此选项也适用于开发人员服务器,但不适用于生产。

This alternative also works on the dev server, but not on the production one.

我完全无所事事。有没有人看到这个/修正了?

I am totally clueless on this one. Has anyone seen this/fixed it?

谢谢!

推荐答案

显然问题是系统关机。通过NTP同步工作:

Apparently the problem was the system time being off. Worked by sync-ing via NTP with:

sudo ntpdate npt.ubuntu.com

sudo ntpdate npt.ubuntu.com

sudo ntpdate pool.ntp.org

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

As @RafaSashi suggested below, the pool.ntp.org server is more reliable. Use that instead of ntp.ubuntu.com (which was the first working one I tried, thus the initial choice).

这篇关于Google Analytics API oauth异常“invalid_grant”与服务帐户。两个服务器上的代码相同。只有一件作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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