Getting error when token expired - problems refreshing token

179 views
Skip to first unread message

Patryk Kormański

unread,
Apr 7, 2014, 9:09:39 AM4/7/14
to google-analytics...@googlegroups.com

I'm writing a Wordpress plugin which displays visits on a page. Everything's done and it works perfectly... for an hour. I can't refresh the token when it expires, so the end-user can't see Analytics results. I don't know what am I doing wrong, maybe I misunderstood the authentication process. This is how I manage the first connection to the 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>";

And then i do this:

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;
    } 

This works fine. Now here's the part, where it should use the token thats stored as an option and if it expired, use refresh_token to get a new one automatically. Here's the code:

$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);

When the token expiration time passes, I get an error:

Fatal error: Uncaught exception 'Google_AuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_request", "error_description" : "Client must specify either client_id or client_assertion, not both" }'' in /_wpsu/analytics/wp-content/plugins/AnalyticsPK/google-api-php-client/src/auth/Google_OAuth2.php:288 Stack trace: #0 /_wpsu/analytics/wp-content/plugins/AnalyticsPK/google-api-php-client/src/auth/Google_OAuth2.php(248): Google_OAuth2->refreshTokenRequest(Array) #1 /_wpsu/analytics/wp-content/plugins/AnalyticsPK/google-api-php-client/src/Google_Client.php(311): Google_OAuth2->refreshToken('1/XCyf8ofh6H3kL...') #2 /_wpsu/analytics/wp-content/plugins/AnalyticsPK/AnalyticsPK.php(90): Google_Client->refreshToken('1/XCyf8ofh6H3kL...') #3 /_wpsu/analytics/wp-includes/widgets.php(182): AnalyticsPK->widget(Array, Array) #4 [internal function]: WP_Widget->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 on line 288

I am using the newest library version from GitHub. What am I doing wrong with refreshing the token? 
Reply all
Reply to author
Forward
0 new messages