I am developing a Node.js application using Express and I am integrating Google Calendar API to enable users to book appointments which will then be added to my calendar. I am using the googleapis npm package for this.
My code is failing with an error: “Error: No access, refresh token or API key is set.” I understand that I need to provide a refresh token, but I’m struggling to figure out how to obtain one. I’ve been able to get a client ID and secret, but the refresh token is proving to be more difficult.
Here's a simplified version of my code:
const { google } = require('googleapis');
const oauth2Client = new google.auth.OAuth2(
"YOUR_CLIENT_ID",
"YOUR_CLIENT_SECRET",
"YOUR_REDIRECT_URL"
);
oauth2Client.setCredentials({ refresh_token: "NEED_REFRESH_TOKEN" });
const calendar = google.calendar({ version: 'v3', auth: oauth2Client });
I understand that I need to run a certain OAuth flow to get this refresh token, but I’m not sure how to do this in my Node.js application. Can someone please guide me on how to obtain a refresh token for use in a server-side application like this?