简体   繁体   English

Google API客户端刷新OAuth2令牌

[英]Google API Client refresh OAuth2 token

I use Google API to retrieve analytics data and display them on my website. 我使用Google API检索分析数据并将其显示在我的网站上。 it worked fine for a month, but the last four days I get this error : 它可以正常工作一个月,但最近四天出现此错误:

Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'

Here is the code : 这是代码:

<?php
require_once ('src/Google/autoload.php');
$client = new Google_Client();
$client->setApplicationName( 'GAFetchData' );

$client->setAssertionCredentials(
  new Google_Auth_AssertionCredentials( 
    // Google client ID email address
    'xxxxx-xxxxxxxxxx@developer.gserviceaccount.com',
    array('https://www.googleapis.com/auth/analytics.readonly'),

    file_get_contents( $Path.'gapii/'.'GAFetchData-xxxxxxxx.p12' )
  )
);

$client->setClientId( 'xxxxx-xxxxxxxxxx.apps.googleusercontent.com' );
$client->setAccessType( 'offline_access' );  
$analytics = new Google_Service_Analytics( $client );
$analytics_id = 'ga:xxxxxx';

try {
  $optParams = array();
  $metrics    = 'ga:pageviews';
  $start_date = '7daysAgo';
  $end_date   = 'today';

   $optParams['dimensions']  = 'ga:pagePath';
   $optParams['filters']      = 'ga:pagePath!@=rech;';           
   $optParams['max-results'] = '10000';

  $result = $analytics->data_ga->get( $analytics_id, $start_date, $end_date, $metrics, $optParams);

  if( $result->getRows() ) { $items = $result->getRows(); }
} 
catch(Exception $e) { echo $e->getMessage();} 
?>

Issue solved ! 问题解决了!

It was caused by the server's clock, I just synchronise it with a NTP server using the following command (Ubuntu in my case) : 这是由服务器的时钟引起的,我只是使用以下命令(在我的情况下为Ubuntu)将其与NTP服务器同步:

sudo ntpdate 3.pool.ntp.org

You will find a list of time servers from the public NTP time server list 您将从公共NTP时间服务器列表中找到时间服务器列表

hope that helps ! 希望能有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM