title: Refresh Tokens Google OAuth 2.0 PHP
body: <p>Using the Google OAuth 2.0 code from this site <a href="https://github.com/google/google-api-php-client" rel="nofollow">https://github.com/google/google-api-php-client</a></p>
  
  <p>I need help coding the refresh token into the following code, I am not sure on how to do it, there are a lot of resources out there, but I can't find any that helps me incorporate it into my code. Current problem now is if I let the token expire, it gives me an error saying I don't have a refresh token and I need it because I don't want to use the force option for the accesstype. I am using the php client:</p>
  
  <pre><code>    //include google api files
      require_once 'src/Google/Client.php';
      require_once 'src/Google/Service/Oauth2.php';
  
      //start session
      $client_id = 'xxxx';
      $client_secret = 'xxxx';
      $redirect_uri = 'xxxxx';
  
      $client = new Google_Client();
      $client-&gt;setApplicationName("Backpack Em");
      $client-&gt;setClientId($client_id);
      $client-&gt;setClientSecret($client_secret);
      $client-&gt;setRedirectUri($redirect_uri);
      $client-&gt;setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'));
      $client-&gt;setAccessType('offline');
      $service = new Google_Service_Oauth2 ($client);
  
   if (isset($_REQUEST['logout'])) {
    unset($_SESSION['upload_token']);
    $client-&gt;revokeToken();   //added
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));     //redirect user back to page
  }
  
  if (isset($_GET['code'])) {
    $client-&gt;authenticate($_GET['code']);
    $_SESSION['upload_token'] = $client-&gt;getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];       header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  }
  
  if (isset($_SESSION['upload_token']) &amp;&amp; $_SESSION['upload_token']) {
    $client-&gt;setAccessToken($_SESSION['upload_token']);
    if ($client-&gt;isAccessTokenExpired()) {
      unset($_SESSION['upload_token']);
    }
  }
  
  if ($client-&gt;getAccessToken()) 
    {
      //For logged in user, get details from google using access token
      $user           = $service-&gt;userinfo-&gt;get($params);
      //$user_id          = filter_var($user['id'],FILTER_SANITIZE_SPECIAL_CHARS);
      $user_name          = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
      $first_name         = filter_var($user['given_name'], FILTER_SANITIZE_SPECIAL_CHARS);
      $last_name          = filter_var($user['family_name'], FILTER_SANITIZE_SPECIAL_CHARS);
      $email          = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
      $profile_url        = filter_var($user['link'], FILTER_VALIDATE_URL);
      $profile_image_url      = filter_var($user['picture'], FILTER_VALIDATE_URL);
      $gender         = filter_var($user['gender'], FILTER_SANITIZE_SPECIAL_CHARS);
      $personMarkup       = "$email&lt;div&gt;&lt;img src='$profile_image_url?sz=50'&gt;&lt;/div&gt;";
      $_SESSION['upload_token']   = $client-&gt;getAccessToken();
      $_SESSION['upload_token']   = $client-&gt;getRefreshToken();
  
    }
  
  else
    {
    $authUrl = $client-&gt;createAuthUrl();
    }
  ?&gt;
  </code></pre>