Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add events to a user's google calendar (via my server) after some event in my application?

I am trying to figure out if it is possible to add events to a user's google calendar server side via Firebase.

I have read this and this which seems to be what I am trying to achieve but it explains that the user which I'd like to add events to their calendar should share their calendar with the account I create for my application.

Is that true or am I misunderstanding something?

I also appreciate it if there is any guide for JavaScript/NodeJS.

like image 557
TheeBen Avatar asked Sep 10 '25 00:09

TheeBen


1 Answers

No. Firebase does not have any built in functionality for adding events to Google calendars. But it's not particularly difficult to connect the two APIs. Below are a couple additional ideas.

Use Functions

One elegant solution would be to use Functions to achieve this by triggering a Functions event by any means (HTTP, a database write, etc) and calling the Calendar API accordingly.

The steps would look something like the following:

  1. When authenticating to Google OAuth on the client, add the calendar scope ('https://www.googleapis.com/auth/calendar')
  2. When triggering the function, send the calendar payload and the Google OAuth token

Inside Cloud Functions for Firebase, your trigger would look something like this:

// Example combined from these docs:
//  https://developers.google.com/calendar/v3/reference/events/insert#examples
//  https://cloud.google.com/solutions/authentication-in-http-cloud-functions#writing_the_cloud_function
//  https://firebase.google.com/docs/functions/http-events

//using another name other than "google" seems to cause error!!
const {google} = require('googleapis');
const calendar = google.calendar('v3');
const functions = require('firebase-functions');

// This example assumes an HTTP call
exports.addToCalendar = functions.https.onRequest((req, res) => {
  const eventData = req.query.eventData;
  const accessToken = getAccessToken(req);
  return addToCalendar(eventData, accessToken).then(() => {
     res.stats(200).send('yay');
  }).catch(e => res.status(e.code).send({error: e.message}));
});

function addEventToGoogleCalendar(eventData, accessToken) {
  const authClient = getOauthClient(accessToken);
  return new Promise((resolve, reject) => {
    calendar.events.insert({
      auth: authClient,
      calendarId: 'primary',
      resource: eventData,
    }, function(err, event) {
      if (err) {
        console.error(err);
        reject(err);
      }
      else {
        resolve();
      }
    });
  });
}

function getOauthClient(accessToken) {
  var oauth = new google.auth.OAuth2();
  oauth.setCredentials({access_token: accessToken});
  return oauth;
}

function getAccessToken(req) {
  const header = req.get('Authorization');
  if (header) {
      var match = header.match(/^Bearer\s+([^\s]+)$/);
      if (match) {
          return match[1];
      }
  }
  return null;
}

And here's some alternative Functions triggers for Realtime Database and Firestore:

// Alternative: Realtime DB trigger
exports.addToCalendar = functions.database.ref('/addToCalendar/{pushId}')
  .onWrite((event) => {
    const data = event.data.val();
    return addToCalendar(data.eventData, data.token)
      // clear from queue after write
      //.then(() => event.ref().remove());
   });

// Alternative: Firestore DB trigger
exports.addToCalendar = functions.firestore.document('addToCalendar/{pushId}')
  .onCreate((event) => {
    const data = event.data.data();
    return addTocalendar(data.eventData, data.token)
      // clear from queue after write
      //.then(() => event.data.ref.remove());
  }); 

An example eventData object would look something like this:

var event = {
  'summary': 'Google I/O 2015',
  'location': '800 Howard St., San Francisco, CA 94103',
  'description': 'A chance to hear more about Google\'s developer products.',
  'start': {
    'dateTime': '2015-05-28T09:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'end': {
    'dateTime': '2015-05-28T17:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'recurrence': [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  'attendees': [
    {'email': '[email protected]'},
    {'email': '[email protected]'},
  ],
  'reminders': {
    'useDefault': false,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10},
    ],
  },
};

Use Zapier

Zapier provides a trigger for integrating Firebase and Google Calendar: https://zapier.com/apps/firebase/integrations/google-calendar

like image 144
Kato Avatar answered Sep 12 '25 19:09

Kato