谷歌Analytics(分析)API的OAuth例外" invalid_grant"与服务帐户。同样的code对两台服务器。只有一个作品 [英] Google Analytics API oauth exception "invalid_grant" with Service Account. Same code on two servers. Only one works
问题描述
我通过服务帐户查询分析API
我已经写dev的服务器上的code和它的工作原理没有问题。
当运行在生产服务器上同一code,它抛出这样的:
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令牌,留言:{
错误: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草案(<一个href=\"http://tools.ietf.org/html/draft-ietf-oauth-v2-31\">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.
下面是code我已经写了:
Here is the code I've written:
$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;
我也试了一个解决方案建议在这里(<一个href=\"https://groups.google.com/forum/?fromgroups#!topic/gs-discussion/3y_2XVE2q7U%5B1-25%5D\">https://groups.google.com/forum/?fromgroups#!topic/gs-discussion/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通过同步ING曾与:
Apparently the problem was the system time being off. Worked by sync-ing via NTP with:
<击> 须藤的ntpdate npt.ubuntu.com
击>
须藤的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).
这篇关于谷歌Analytics(分析)API的OAuth例外&QUOT; invalid_grant&QUOT;与服务帐户。同样的code对两台服务器。只有一个作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!