title: Google Analytics API - problems with refreshing token
body: <p>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:</p>
  
  <pre><code>$client-&gt;setClientId(get_option('client_ID'));
  $client-&gt;setClientSecret(get_option('client_secret'));
  $client-&gt;setRedirectUri(get_option('redirectURI'));
  $client-&gt;setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
  $client-&gt;setAccessType('offline');
  
  $authUrl = $client-&gt;createAuthUrl();
  print "&lt;a class='login' href='$authUrl'&gt;&lt;button&gt;Authorize Google&lt;/button&gt;&lt;/a&gt;";
  </code></pre>
  
  <p>And then i do this:</p>
  
  <pre><code>try {
      if (isset($_GET['code'])) {
            $client-&gt;authenticate();
            $_SESSION['token'] = $client-&gt;getAccessToken();
            update_option('token', $_SESSION['token']);
            $tok = json_decode($_SESSION['token']);
            update_option('refresh_token', $tok-&gt;refresh_token);
            $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
           ?&gt;&lt;script type="text/javascript"&gt;
              window.location="&lt;?php filter_var($redirect, FILTER_SANITIZE_URL)?&gt;";
          &lt;/script&gt;&lt;?php    
      }
  
  
      if (isset($_SESSION['token'])) {
          update_option('token', $_SESSION['token']);
          $client-&gt;setAccessToken($_SESSION['token']);
      }
  } catch (Google_AuthException $e){
          return FALSE;
      } 
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>$client = new Google_Client();
  $client-&gt;setApplicationName('AnalyticsPK Plugin for Wordpress');
  $client-&gt;setClientId(get_option('client_ID'));
  $client-&gt;setClientSecret(get_option('client_secret'));
  $client-&gt;setRedirectUri(get_option('redirectURI')); 
  $client-&gt;setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
  $client-&gt;setAccessType('offline');
  $client-&gt;setApprovalPrompt("force");
  echo "Token: ".get_option('token')."&lt;br&gt;";
  echo "Refresh token: ".get_option('refresh_token')."&lt;br&gt;";
  $client-&gt;setAccessToken(get_option('token'));
  if ($client-&gt;isAccessTokenExpired()){
    echo "Token expired &lt;br&gt;";
    $client-&gt;refreshToken(get_option('refresh_token'));
    echo "New token: ".$client-&gt;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-&gt;setUseObjects(true);
  $analytics = new Google_AnalyticsService($client);
  </code></pre>
  
  <p>When the token expiration time passes, I get an error:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>What am I doing wrong with refreshing the token?</p>