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?