Val*_*ler 9 php api exception oauth google-analytics-api
我通过服务帐户查询Analytics API .
我已经在开发服务器上编写了代码,它没有问题.在生产服务器上运行相同的代码时,它会抛出:
Google_AuthException:刷新OAuth2令牌时出错,消息:'{"error":"invalid_grant"}'
我尝试过创建另一个服务帐户,行为是一样的.
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.
Run Code Online (Sandbox Code Playgroud)
这是我写的代码:
$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;
Run Code Online (Sandbox Code Playgroud)
我还尝试了使用authenticatedRequest()而不是Google_AnalyticsService 建议的解决方案(https://groups.google.com/forum/?fromgroups#!topic/gs-discussion/3y_2XVE2q7U%5B1-25%5D):
$req = new Google_HttpRequest($apiUrl);
$resp = $client::getIo()->authenticatedRequest($req);
$result = json_decode($resp->getResponseBody(), true);
Run Code Online (Sandbox Code Playgroud)
此替代方案也适用于开发服务器,但不适用于生产服务器.
我对这一点完全无能为力.有谁见过这个/修好了吗?
谢谢!
Val*_*ler 24
显然问题是系统时间关闭.通过NTP同步:
sudo ntpdate npt.ubuntu.com
sudo ntpdate pool.ntp.org
正如下面提到的@RafaSashi,pool.ntp.org服务器更可靠.使用它代替ntp.ubuntu.com (这是我尝试的第一个工作,因此是最初的选择).
| 归档时间: |
|
| 查看次数: |
11548 次 |
| 最近记录: |