title: Google Analytics access token expired after 3600 s (1 hour)
body: <p>I'm using Google analytics API (PHP) to retrieve my account info, I retrieved the refrech token using :</p>
  
  <pre><code>if (isset($_SESSION['token'])) {
  
  // echo $_SESSION['token'].'&lt;br /&gt;';
  
  $authObj = json_decode($_SESSION['token']);
  $accessToken = $authObj-&gt;access_token;
  $refreshToken = $authObj-&gt;refresh_token;
  $tokenType = $authObj-&gt;token_type;
  $expiresIn = $authObj-&gt;expires_in;
  
  echo 'access_token = ' . $accessToken;
  echo '&lt;br /&gt;';
  echo 'refresh_token = ' . $refreshToken;
  echo '&lt;br /&gt;';
  echo 'token_type = ' . $tokenType;
  echo '&lt;br /&gt;';
  echo 'expires_in = ' . $expiresIn;
  echo '&lt;br /&gt;';
  }
  </code></pre>
  
  <p>after that I copied the value returned by <code>$refreshToken</code>, I comment the previous function and I call the method as following:</p>
  
  <pre><code>$refreshToken  = '1/YZiV9J********vMjey8*****************';
  $client-&gt;refreshToken($refreshToken);
  </code></pre>
  
  <p>This works perfect, but it through an error after an hour telling me that the access token has been exprired.</p>
  
  <p>How can I know that the access token is expired, I found this <a href="https://stackoverflow.com/questions/10882294/refresh-a-access-token-with-google-analytics-api">post</a>, the guy solve his problem but he didn't post the answer. 
  and how can I generate a new one using the same refrech token without going through the authorization process again because I'm running my code through the terminal. </p>
  
  <p>I posted bellow a snippet from my project.</p>
  
  <p>Thanks!</p>
  
  <pre><code>require_once 'google-api-php-client/src/Google_Client.php';
  require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
  $scriptUri = 'http://domainname.com';
  
  session_start();
  
  $client = new Google_Client();
  $client-&gt;setApplicationName('Google Analytics project');
  
  $client-&gt;setClientId('************.apps.googleusercontent.com');
  $client-&gt;setClientSecret('S**********************1T');
  
  $client-&gt;setRedirectUri($scriptUri);
  
  $client-&gt;setDeveloperKey('**************************-1c');
  $client-&gt;setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
  
  $client-&gt;setAccessType('offline'); 
  $client-&gt;setUseObjects(true);
  
  
  if (isset($_GET['code'])) {  
  $client-&gt;authenticate();  
  $_SESSION['token'] = $client-&gt;getAccessToken(); 
  
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];  
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  }
  
  if (isset($_SESSION['token'])) {    
  $authObj = json_decode($_SESSION['token']);
  $accessToken = $authObj-&gt;access_token;
  $refreshToken = $authObj-&gt;refresh_token;
  $tokenType = $authObj-&gt;token_type;
  $expiresIn = $authObj-&gt;expires_in;
  }
  
  
  if (!$client-&gt;getAccessToken()) {  
  
  $authUrl = $client-&gt;createAuthUrl();  
  print "&lt;a class='login' href='$authUrl'&gt;Connect Me!&lt;/a&gt;";
  } else {  
  // Create analytics service object. See next step below.
  $analytics = new Google_AnalyticsService($client);
  runMainDemo($analytics);
  }
  
  
  
  
  function runMainDemo(&amp;$analytics) {  
  
  try {    
  // Step 2. Get the user's first profile ID.    
  $profileId = getProfileId($analytics);    
  if (isset($profileId)) {      
      // Step 3. Query the Core Reporting API.      
      $results = getResults($analytics, $profileId);      
      // Step 4. Output the results.      
      printResults($results);    
      }  
  } 
  catch (apiServiceException $e) {    
  // Error from the API.    
  print 'There was an API error : ' . $e-&gt;getCode() . ' : ' . $e-&gt;getMessage();  
  } 
  catch (Exception $e) {    
  print 'There wan a general error : ' . $e-&gt;getMessage();  
  }
  }
  
  
  
  function getProfileId(&amp;$analytics) {
      //My code to retrieve data
  }
  </code></pre>