title: How to get a refresh_token with an auto-approved response?
body: <p>I have the following code:</p>
  
  <pre><code>if (isset($_REQUEST['logout']))
  {
    unset($_SESSION['upload_token ']);
  }
  
  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())
    {
      echo "The access token is expired.&lt;br&gt;"; // Debug
      $client-&gt;refreshToken(json_decode($_SESSION['upload_token']));
      unset($_SESSION['upload_token']);
    }
  }
  else
  {
    $authUrl = $client-&gt;createAuthUrl();
  }
  </code></pre>
  
  <p>and am receiving the following error:</p>
  
  <pre><code>Uncaught exception 'Google_Auth_Exception' with message 'The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved.'
  </code></pre>
  
  <p>I am assuming I am getting this error because the response was auto-approved. </p>
  
  <p>What should be changed?</p>
  
  <p>UPDATE: I've tried adding this to my code:</p>
  
  <pre><code>$client-&gt;setAccessType("online");
  $client-&gt;setApprovalPrompt("auto");
  </code></pre>
  
  <p>Based on <a href="https://stackoverflow.com/questions/10101551/google-oauth-api-store-and-retrieve-tokens-after-first-login-and-authorization">this question</a>.  I still am receiving the same error of missing refresh token.</p>
  
  <p>UPDATE:  After kroikie's update, my code looks like the following:</p>
  
  <pre><code>$client = new Google_Client();
  $client-&gt;setClientId($client_id);
  $client-&gt;setClientSecret($client_secret);
  $client-&gt;setRedirectUri($redirect_uri);
  $client-&gt;addScope("https://www.googleapis.com/auth/drive");
  $client-&gt;setAccessType("offline");
  $client-&gt;setApprovalPrompt("auto");
  $client-&gt;setApplicationName("Appraisal App");
  $service = new Google_Service_Drive($client);
  
  if (isset($_REQUEST['logout']))
  {
    unset($_SESSION['upload_token ']);
  }
  
  if (isset($_GET['code']))
  {
    $resp = $client-&gt;authenticate($_GET['code']);
    $_SESSION['upload_token'] = $client-&gt;getAccessToken();
    $array = get_object_vars(json_decode($resp));
    // store and use $refreshToken to get new access tokens
    $refreshToken = $array['refreshToken'];
    $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())
    {
      echo "The access token is expired.  Let Raph know that you saw this.&lt;br&gt;";
      $client-&gt;refreshToken($refreshToken);
      unset($_SESSION['upload_token']);
    }
  }
  else
  {
    $authUrl = $client-&gt;createAuthUrl();
  }
  </code></pre>
  
  <p>Unfortunately, I still receive the same fatal error when the refresh token is needed.</p>