title: Ongoing Access to Google Calendar API
body: <p>How do I request access to a user's calendar <em>once</em>, then query events without having to request access again? Is there an access key that can grant continuous access as long as it isn't revoked?</p>
  
  <p>I am currently using the <a href="https://github.com/berryboy/google-calendar" rel="nofollow">google-calendar</a> module in a nodejs app running <a href="http://expressjs.com/" rel="nofollow">express</a> + <a href="http://www.mongodb.org/" rel="nofollow">mongodb</a>.</p>
  
  <p>The example below shows partial code for making the OAuth request and redirecting back to the app. This works great. What I want though is once I have access, to be able to query the user's events any time I want. This will be used in a booking app which, once a user registers their public Google Calendar, allows others to see their availability.</p>
  
  <p>I have looked at the <a href="https://developers.google.com/google-apps/calendar/" rel="nofollow">Google Calendar API</a> documentation, but I can't figure out what is needed for the ongoing access I need. </p>
  
  <p><strong>Configuration</strong>:</p>
  
  <pre><code>var GoogleCalendar = require('google-calendar');
  var google_calendar = new GoogleCalendar.GoogleCalendar(config.google.clientId, config.google.clientSecret, 'http://localhost:3000/authentication');
  </code></pre>
  
  <p><strong>Display route</strong>:</p>
  
  <pre><code>app.get('/display', function(req, res) {
  
      // redirect user to Google Authentication
      var access_token = req.session.access_token;
      if(!access_token) {
              req.session.authReturn = req.url;
              return res.redirect('/authentication');
      }
  
      google_calendar.listCalendarList(access_token, function(err, calendarList) {
              // do something with the calendar events...
      }
  
      ...
  </code></pre>
  
  <p><strong>Authenticate route</strong>:</p>
  
  <pre><code>app.all('/authentication', function(req, res){
  
      // Redirect the user to Google's authentication form 
      if(!req.query.code){
  
          google_calendar.getGoogleAuthorizeTokenURL(function(err, redirectUrl) {
              if(err) return res.send(500,err);
              return res.redirect(redirectUrl);
          });
      }
      // Get access_token from the code
      else {
          google_calendar.getGoogleAccessToken(req.query, function(err, access_token, refresh_token) {
  
              if(err) return res.send(500,err);
  
              req.session.access_token = access_token;
              req.session.refresh_token = refresh_token;
  
              return res.redirect(req.session.authReturn);
          });
      }
  });
  </code></pre>