php – Google Analytics API oauth异常“invalid_grant”与服务帐户.两个服务器上的代码相同.只有一件作品

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Google Analytics API oauth异常“invalid_grant”与服务帐户.两个服务器上的代码相同.只有一件作品脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我通过服务帐户查询Google Analytics(分析)API.

我已经在开发服务器上编写了代码,它的工作没有问题.
在生产服务器上运行相同的代码时,会抛出:

我尝试创建另一个服务帐户,行为是一样的.

oAuth IETF草案(http://tools.ietf.org/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(这是我尝试的第一个工作,因此是初始选择).

脚本宝典总结

以上是脚本宝典为你收集整理的php – Google Analytics API oauth异常“invalid_grant”与服务帐户.两个服务器上的代码相同.只有一件作品全部内容,希望文章能够帮你解决php – Google Analytics API oauth异常“invalid_grant”与服务帐户.两个服务器上的代码相同.只有一件作品所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。