nicmart-dev / linguistnow

LinguistNow simplifies the hassle of finding available linguists for translation projects.
https://linguistnow.netlify.app
1 stars 1 forks source link

Select calendars using Google Calendar API #17

Closed nicmart-dev closed 1 month ago

nicmart-dev commented 1 month ago

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

nicmart-dev commented 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;
};
nicmart-dev commented 1 month ago

@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

nicmart-dev commented 1 month ago

I now do get the access token, swapping after switching GoogleLoginwith useGoogleLogincomponent, and setting up backend server, but I want to use the GoogleLogincomponent 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

nicmart-dev commented 1 month ago

I was able to get combine both GoogleLoginand useGoogleLoginto get access token and refresh token saved to localstorage.

nicmart-dev commented 1 month ago

Enabled Google Calendar API in https://console.cloud.google.com/apis/api/calendar-json.googleapis.com/