I'm using Google calendar nodejs api to create a calendar invite for an app that connects doctors with patients. Here's my code:
const defer = Q.defer();
oauth2Client.setCredentials({
refresh_token: options.refreshToken,
});
let calendar = google.calendar({
version: "v3",
auth: oauth2Client,
});
calendar.events.insert(
{
auth: oauth2Client,
singleEvents: true,
calendarId: "primary",
resource: {
start: {
dateTime: new Date(options.startDate),
timeZone: "utc",
},
end: {
dateTime: new Date(options.endDate),
timeZone: "utc",
},
attendees: [
{
email: options.user.email,
},
{
email: options.mentor.email,
},
],
reminders: {
useDefault: false,
overrides: [
{
method: "email",
minutes: 15,
},
{
method: "email",
minutes: 60,
},
{
method: "popup",
minutes: 10,
},
]
},
colorId: 4,
sendUpdates: "all",
status: "confirmed",
},
},
(err, res) => {
if (err) {
console.dir("Error " + err);
defer.reject(err);
} else {
defer.resolve(res.data);
}
}
);
return defer.promise;
I had the doctor go through oauth2 to get access to his Google account.
After a while, I get the error "invalid_grant" when I try to run the code above. I'm guessing the token expired, but that can't be because I'm using the refresh token in the request above not the access token and the user hasn't revoked access.
Am I supposed to refresh the token after some time? What I'm I doing wrong?