问题描述
限时送ChatGPT账号..我正在通过服务帐户查询 Analytics API.
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 令牌时出错,消息:'{错误":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 草案(https://datatracker.ietf/doc/html/draft-ietf-oauth-v2-31) 关于错误的说明:
The oAuth IETF draft (https://datatracker.ietf/doc/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';
$GA_APP_EMAIL = 'XX@developer.gserviceaccount';
$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/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/forum/?fromgroups#!topic/gs-discussion/3y_2XVE2q7U%5B1-25%5D) 使用 authenticatedRequest() 而不是 Google_AnalyticsService:
I've also tried a solution suggested here (https://groups.google/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
sudo ntpdate pool.ntp
正如@RafaSashi 在下面建议的那样,pool.ntp
服务器更可靠.使用它代替 ntp.ubuntu
(这是我尝试的第一个可行的,因此是最初的选择).
As @RafaSashi suggested below, the pool.ntp
server is more reliable. Use that instead of ntp.ubuntu
(which was the first working one I tried, thus the initial choice).
这篇关于Google Analytics API oauth 异常“invalid_grant"与服务帐户.两台服务器上的代码相同.只有一个作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论