title: How can I set the refresh_token when working with the google api?
body: <p>I'm working off an example trying to learn how to use the google api to change events on a calendar.  The server is the user which will update the calendar based off of information in a database.  No user interaction is actually required.  </p>
  
  <p>The problem is I am having issues getting/using refresh tokens.  I click the "Connect Me" link that gets added to the page and it gives me an error:</p>
  
  <p><code>Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_request", "error_description" : "Missing required parameter: refresh_token" }</code></p>
  
  <p>I have <a href="https://stackoverflow.com/a/15905618/2930045">tried setting the refresh token this way</a>, along with similar methods, but none of them seem to work which makes me think I am implementing them incorrectly.</p>
  
  <p>When I print out the <code>$_SESSION['access_token']</code> variable, it shows no <code>refresh_token</code>:</p>
  
  <p><code>{"access_token":"token","token_type":"Bearer","expires_in":3599,"created":1417534503}</code></p>
  
  <p>Here is the function I am using to authorize the 'user' without the <code>refresh_token</code> portion(based off an example):</p>
  
  <pre><code>function googleAuth(){
  
      $client_id = 'myclientid';
      $client_secret = 'myclientsecret';
      $redirect_uri = 'redirecturi';
      $client = new Google_Client();
      $client-&gt;setAccessType('offline');
      $client-&gt;setClientId($client_id);
      $client-&gt;setClientSecret($client_secret);
      $client-&gt;setRedirectUri($redirect_uri);
      $client-&gt;setScopes('https://www.googleapis.com/auth/calendar');
  
      /************************************************
      If we're logging out we just need to clear our
      local access token in this case
      ************************************************/
      if (isset($_REQUEST['logout'])) {
      unset($_SESSION['access_token']);
      }
      /************************************************
      If we have a code back from the OAuth 2.0 flow,
      we need to exchange that with the authenticate()
      function. We store the resultant access token
      bundle in the session, and redirect to ourself.
      ************************************************/
      if (isset($_GET['code'])) {
          $resp = $client-&gt;authenticate($_GET['code']);
          $_SESSION['access_token'] = $client-&gt;getAccessToken();
  
          $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
          header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  
      }
      /************************************************
      If we have an access token, we can make
      requests, else we generate an authentication URL.
      ************************************************/
      if (isset($_SESSION['access_token']) &amp;&amp; $_SESSION['access_token']) {
          $client-&gt;setAccessToken($_SESSION['access_token']);
      } else {
          $authUrl = $client-&gt;createAuthUrl();
      }
  
      if (isset($authUrl)) {
          echo "&lt;a class='login' href='" . $authUrl . "'&gt;Connect Me!&lt;/a&gt;";
      }
  
      return $client;
  }
  </code></pre>
  
  <p>Here is the code I am using to add the event to the calendar:</p>
  
  <pre><code>function addEvent($title, $location, $startTime, $stopTime, $client){
  
      $event = new Google_Service_Calendar_Event();
      $start = new Google_Service_Calendar_EventDateTime();
  
      $event-&gt;setSummary($title);
      $event-&gt;setLocation($location);
  
      $start-&gt;setDateTime($startTime);
  
      $end = new Google_Service_Calendar_EventDateTime();
  
      $end-&gt;setDateTime($stopTime);
  
      $event-&gt;setStart($start);
      $event-&gt;setEnd($end);
  
      $atendee = new Google_Service_Calendar_EventAttendee();
      $atendee-&gt;setEmail('someGuy@someDomain.com');
  
      $atendees = array($atendee);
  
      $event-&gt;attendees = $atendees;
  
      $service = new Google_Service_Calendar($client);
  
      $event_id = $service-&gt;events-&gt;insert('primary', $event);
  
  }
  </code></pre>
  
  <p>How can I set the missing parameter: "refresh_token"?  Are there issues with the structure of the code?  I have looked through documentation, and I am willing to look some more, but if somebody can lend a hand at explaining how to do this, that would be amazing.  Thanks!</p>