Google Analytics API oauth 异常“invalid

编程入门 行业动态 更新时间:2024-10-16 02:31:15
本文介绍了Google Analytics API oauth 异常“invalid_grant"与服务帐户.两台服务器上的代码相同.只有一个作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-18 15:34:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/942197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   Analytics   Google   API   invalid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!