Chuck Dries
Chuck Dries

Reputation: 1662

Authenticating googleapi node library with passport

I'm using passport for google oauth, I want to use it to make API calls as well. All of the googleapi documentation revolves around using google's own authentication library instead.

In particular, what is contained in the auth object, as seen in the calednar.events.list call at the end of the quickstart, and how do I get it out of passport?

https://developers.google.com/google-apps/calendar/quickstart/nodejs

Upvotes: 1

Views: 665

Answers (2)

Bertrand Martel
Bertrand Martel

Reputation: 45372

auth in this context is OAuth2 object. You can see how it is handled in each request from the googleapis source code. For instance, you could have set in at the context scope like the following :

google.calendar({
    version: 'v3',
    auth: oauth2Client
})

or in each request like in the getting started sample code.


As you want to use it for passport, I guess you will have something like the following, assuming /auth/google is the auth endpoint :

function userLogged(req, res, next) {
    if (req.isAuthenticated())
        return next();
    res.redirect('/auth/google');
}

app.get('/calendarList', userLogged, function(req, res) {

    // req.user is the login user

    var oauth2Client = new OAuth2(
        config.clientID,
        config.clientSecret,
        config.callbackURL
    );

    oauth2Client.credentials = {
        access_token: req.user.access_token,
        refresh_token: req.user.refresh_token
    };

    var calendar = google.calendar('v3');
    calendar.events.list({
        auth: oauth2Client,
        calendarId: 'primary',
        timeMin: (new Date()).toISOString(),
        maxResults: 10,
        singleEvents: true,
        orderBy: 'startTime'
    }, function(err, response) {
        // process result
    });
});

Upvotes: 1

jwilleke
jwilleke

Reputation: 10986

You only need to add the desired scopes for access to the Google Calendar API.

-jim

Upvotes: 0

Related Questions