2
votes

How do I request access to a user's calendar once, 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?

I am currently using the google-calendar module in a nodejs app running express + mongodb.

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.

I have looked at the Google Calendar API documentation, but I can't figure out what is needed for the ongoing access I need.

Configuration:

var GoogleCalendar = require('google-calendar');
var google_calendar = new GoogleCalendar.GoogleCalendar(config.google.clientId, config.google.clientSecret, 'http://localhost:3000/authentication');

Display route:

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...
    }

    ...

Authenticate route:

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);
        });
    }
});
1

1 Answers

2
votes

Use the Oauth 2.0 offline access parameter. See details at:

https://developers.google.com/accounts/docs/OAuth2WebServer#offline