title: OAuth with Google Analytics API reports invalid token
body: <p>the following code runs through well, but some times reports "Invalid token". It happens, when I have foreign website in my ID list. But when I log in with email/password pair, it runs well till the end. Any idea what is wrong?</p>
  
  <p>Cheers:</p>
  
  <h2>Bob</h2>
  
  <pre><code>&lt;?php  
  /*  
  API ACCESS PANEL:  
  https://code.google.com/apis/console/  
  */  
  session_start();  #the includes are as follows, using also gapi.class.php  
  require_once 'src/apiClient.php';  
  require_once 'src/contrib/apiAnalyticsService.php';  
  ini_set('display_errors', '1');  
  $client = new apiClient();  
  $client-&gt;setApplicationName("Google Analytics PHP Starter Application");  
  /*  
  Visit https://code.google.com/apis/console?api=analytics to generate your client id, client secret, and to register your redirect uri.  
  */  
  $client-&gt;setClientId('');# MUST SET UP!  
  $client-&gt;setClientSecret('');# MUST SET UP!  
  $client-&gt;setRedirectUri('http://localhost/analytics/chart_server.php');# MUST SET UP!  
  $client-&gt;setDeveloperKey('');# MUST SET UP!  
  
   // requestReportData first parameter, an Analytics Profil ID must defined here:  
  $ga_profile_id='';# MUST SET UP!  
  //////////////////////////////////////////////////////////////////////////////  
  
  $service = new apiAnalyticsService($client);  
  
  // logout  
  if (isset($_GET['logout'])) {  
      unset($_SESSION['token']);  
      exit;  
  }  
  
  // get the tooken from Google, store into the session, call back this script with header-location  
  if (isset($_GET['code'])) {  
      $client-&gt;authenticate();  
      $_SESSION['token'] = $client-&gt;getAccessToken();  
      header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);  
      exit;  
  }  
  
  // if we got token in the session-ben, passing to $client (this is the OAuth client)  
  if (isset($_SESSION['token'])) {  
      $client-&gt;setAccessToken($_SESSION['token']);  
  }  
  
  // if the token valid, then we have access:  
  if ($client-&gt;getAccessToken()){  
      #$props = $service-&gt;management_webproperties-&gt;listManagementWebproperties("~all");  
      /*  
      print "Web Properties " . print_r($props, true);  
      */  
      print("auth OK");//ok  
      print_r($client-&gt;getAccessToken()); // print out for test  
      $_SESSION['token'] = $client-&gt;getAccessToken();// stor eback to the session  
  } else { // Authorize with OAuth cause we have no valid access token  
      #https://www.google.com/analytics/feeds/  
      #https://www.googleapis.com/auth/analytics.readonly  
      $scope="https://www.google.com/analytics/feeds/"; // we'd like to access this  
      $authUrl = $client-&gt;createAuthUrl($scope);  
      header("Location: ".$authUrl); /* Redirect browser */  
      #  print "&lt;a class='login' href='$authUrl'&gt;Connect Me!&lt;/a&gt;";  
      exit;  
  }  
  
  // test printout, OAuth account object:  
  $accounts = $service-&gt;management_accounts-&gt;listManagementAccounts();  
  print "List of accounts" . print_r($accounts, true) .;  
  
  
  //////////////////////////////////////////////////////////////////////////////////////////////  
  // Here is the Google Analytics API call, and the "TOKEN INVALID 401" error message         //  
  //////////////////////////////////////////////////////////////////////////////////////////////  
  
  require 'gapi.class.php';  
  $ga = new gapi(null,null,$_SESSION['token']); // access token instead of mail, password pair  
  #$token=$ga-&gt;getAuthToken();# $_SESSION['token']  
  
  
  // http://code.google.com/p/gapi-google-analytics-php-interface/wiki/GAPIDocumentation  
  // requestReportData($report_id, $dimensions, $metrics, $sort_metric=null, $filter=null, $start_date=null, $end_date=null, $start_index=1, $max_results=30)  
  $dimensions=array('browser','date');// x coord  
  $metrics=array('visits','pageLoadTime','uniquePageviews'); // y coord  
  $sort_metric="-date"; #descending order  
  $filter = '';  
  $startDate = '2012-04-01';  
  $endDate = '2012-04-12';  
  $start_index=1;  
  $max_results=50;  
  
  #$ga-&gt;requestReportData($accProfiles[$profileNum]-&gt;getProfileId(),$dimensions,$metrics,$sort_metric,$filter, $start_date, $end_date, $start_index, $max_results );  
  $ga-&gt;requestReportData($ga_profile_id, $dimensions, $metrics, $sort_metric, $filter, $startDate, $endDate, $start_index, $max_results );  
  
  $results=$ga-&gt;getResults();  
  // test:  
  echo "Google Analytics data ";print_r($results);  
  #echo "#####".$ga-&gt;getMetrics();  
  
  // Set the JSON header  
  //header("Content-type: text/json");  
  #echo json_encode( array($dim, $met1, $met2, $met3 ) );  
  
  
  ?&gt;  
  </code></pre>