0
votes

I tried to get events from Google Calendar API in Node.js, but I was unable to understand how OAuth2 works.

I prepared callback into router to get code and it worked well:

router.get('/oauth2callback', function(req, res) {

   // store code into database with user id.
});

And fetched events from a user:

user.findById(req.session.user_id)
    .then(function (data) {

        auth.setCredentials(tokens);
            user.getCalendars(req.session.user_id)
                .then(function (data) {

                    var promises = [];

                    for (var i = 0; i < data.length; i++)
                        promises.push(createPromise(data[i].cal_id));

                    promise.all(promises)
                           .then(function (data) {

                               res.json(data);
                           })
                           .catch(function (err) {

                               res.json(err);
                           });
                })
                .catch(function (err) {

                    res.json(err);
                });
        });

function createPromise(calendarId) {

    return new promise(function (resolve, reject) {

        calendar.events.list({ calendarId : calendarId, key : '{MY_API_KEY}' }, { auth: auth }, function (err, result) {

            resolve(result);
        });
    });
}

But it shows public calendars. I want to get all events, including private calendars.

I found it would be possible to fetch all events if I could get Bearer Header for Authorization. How can I get it?

What codes will be needed to access all events from private calendars?

1

1 Answers

0
votes

To access the private calendar, you need to be authenticated to see/view this calendar. You can use an OAuth or Service account here because every request your application sends to the Google Calendar API must include an authorization token. The token also identifies your application to Google. To understand more about this, check this documentation.

I recommend you to check this Github to understand more on how to use Calendar API in Nodejs including on how to use OAuth here.

For more information, check these related SO questions.