Google AnalyticsAPI - 刷新令牌的问题

时间:2014-04-04 15:06:15

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

我正在编写一个Wordpress插件,可以在页面上显示访问次数。一切都完成了,它完美地工作了......一个小时。我无法在令牌过期时刷新令牌,因此最终用户无法查看Google Analytics结果。我不知道自己做错了什么,也许我误解了身份验证过程。 这是我管理API的第一个连接的方式:

$client->setClientId(get_option('client_ID'));
$client->setClientSecret(get_option('client_secret'));
$client->setRedirectUri(get_option('redirectURI'));
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
$client->setAccessType('offline');

$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'><button>Authorize Google</button></a>";

然后我这样做:

try {
    if (isset($_GET['code'])) {
          $client->authenticate();
          $_SESSION['token'] = $client->getAccessToken();
          update_option('token', $_SESSION['token']);
          $tok = json_decode($_SESSION['token']);
          update_option('refresh_token', $tok->refresh_token);
          $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
         ?><script type="text/javascript">
            window.location="<?php filter_var($redirect, FILTER_SANITIZE_URL)?>";
        </script><?php    
    }


    if (isset($_SESSION['token'])) {
        update_option('token', $_SESSION['token']);
        $client->setAccessToken($_SESSION['token']);
    }
} catch (Google_AuthException $e){
        return FALSE;
    } 

这很好用。现在这里是部分,它应该使用存储为选项的令牌,如果它已过期,请使用refresh_token自动获取新的令牌。这是代码:

$client = new Google_Client();
$client->setApplicationName('AnalyticsPK Plugin for Wordpress');
$client->setClientId(get_option('client_ID'));
$client->setClientSecret(get_option('client_secret'));
$client->setRedirectUri(get_option('redirectURI')); 
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
$client->setAccessType('offline');
$client->setApprovalPrompt("force");
echo "Token: ".get_option('token')."<br>";
echo "Refresh token: ".get_option('refresh_token')."<br>";
$client->setAccessToken(get_option('token'));
if ($client->isAccessTokenExpired()){
  echo "Token expired <br>";
  $client->refreshToken(get_option('refresh_token'));
  echo "New token: ".$client->getAccessToken();
}


//echo "Token: ".get_option('token');
//echo "Refresh token: ".get_option('refresh_token');
// Magic. Returns objects from the Analytics Service instead of associative arrays.
$client->setUseObjects(true);
$analytics = new Google_AnalyticsService($client);

当令牌过期时间过去后,我收到错误:

  

致命错误:未捕获的异常&#39; Google_AuthException&#39;与消息   &#39;刷新OAuth2令牌时出错,消息:&#39; {&#34;错误&#34; :   &#34; invalid_request&#34;,&#34; error_description&#34; :&#34;客户必须指定   client_id或client_assertion,而不是两者&#34; }&#39;&#39;在   /_wpsu/analytics/wp-content/plugins/AnalyticsPK/google-api-php-client/src/auth/Google_OAuth2.php:288   堆栈跟踪:#0   /_wpsu/analytics/wp-content/plugins/AnalyticsPK/google-api-php-client/src/auth/Google_OAuth2.php(248):   Google_OAuth2-&gt; refreshTokenRequest(数组)#1   /_wpsu/analytics/wp-content/plugins/AnalyticsPK/google-api-php-client/src/Google_Client.php(311):   Google_OAuth2-&gt; refreshToken(&#39; 1 / XCyf8ofh6H3kL ...&#39;)#2   /_wpsu/analytics/wp-content/plugins/AnalyticsPK/AnalyticsPK.php(90):   Google_Client-&gt; refreshToken(&#39; 1 / XCyf8ofh6H3kL ...&#39;)#3   /_wpsu/analytics/wp-includes/widgets.php(182):   AnalyticsPK-&gt; widget(数组,数组)#4 [内部函数]:   WP_Widget-&gt; display_callback(Array,Array)#5   /_wpsu/analytics/wp-includes/widgets.php(895):   call_user_func_array(Array,Array)#6 / _wpsu / analytics / in   /_wpsu/analytics/wp-content/plugins/AnalyticsPK/google-api-php-client/src/auth/Google_OAuth2.php   在第288行

刷新令牌我做错了什么?

0 个答案:

没有答案