Closed nicmart-dev closed 1 month ago
After successful authentication, use the access token to call the Google Calendar API.
import { google } from 'googleapis';
const getCalendarEvents = async (accessToken) => {
const calendar = google.calendar({ version: 'v3', auth: accessToken });
const events = await calendar.events.list({
calendarId: 'primary',
timeMin: (new Date()).toISOString(),
showDeleted: false,
singleEvents: true,
maxResults: 10,
orderBy: 'startTime',
});
return events.data.items;
};
const listCalendars = async (accessToken) => {
const calendar = google.calendar({ version: 'v3', auth: accessToken });
const calendarList = await calendar.calendarList.list();
return calendarList.data.items;
};
@react-oauth/google
only gave an ID token (JWT), but I need an access token to get the list of calendars using Google Calendar API, so I have to refactor this per https://react-oauth.vercel.app/ to use google-auth-library
on the backend, following steps in https://github.com/MomenSherif/react-oauth/issues/12#issuecomment-1131408898
I now do get the access token, swapping after switching GoogleLogin
with useGoogleLogin
component, and setting up backend server, but I want to use the GoogleLogin
component for the standard Google sign-in button while still obtaining the access token for the Google Calendar API.
I will investigate how I can get from an id token to an access token by looking at verifyIdToken function in google-auth-library
https://github.com/googleapis/google-auth-library-nodejs/blob/main/samples/verifyIdToken.js
I was able to get combine both GoogleLogin
and useGoogleLogin
to get access token and refresh token saved to localstorage.
Enabled Google Calendar API in https://console.cloud.google.com/apis/api/calendar-json.googleapis.com/
Once authenticated with #16, you need to request access to the user's Google Calendar data. Users should grant permission for your application to view their calendar events.
Display the list of available calendars to the user and allow them to select the ones they are interested in