【问题标题】:Google API Client refresh OAuth2 tokenGoogle API 客户端刷新 OAuth2 令牌
【发布时间】:2015-10-14 03:44:38
【问题描述】:

我使用 Google API 检索分析数据并将其显示在我的网站上。它工作了一个月,但最后四天我收到了这个错误:

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

代码如下:

<?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();} 
?>

【问题讨论】:

    标签: php oauth-2.0 google-analytics-api


    【解决方案1】:

    问题解决了!

    这是由服务器的时钟引起的,我只是使用以下命令(在我的例子中是 Ubuntu)将它与 NTP 服务器同步:

    sudo ntpdate 3.pool.ntp.org
    

    您会从 public NTP time server list

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 2016-08-03
      • 2013-02-03
      • 2012-03-03
      • 2014-07-18
      • 2017-01-11
      • 2018-04-22
      • 1970-01-01
      • 2016-10-13
      相关资源
      最近更新 更多