13

For some reason, I am having a really hard time accessing Google calendar.

I want to be able to add and remove events in a calendar from my Node.js server.

I am finding really conflicting information from the documents.

I followed - https://developers.google.com/identity/protocols/OAuth2ServiceAccount which gives a good guide on how to get an acccess token but then at the end it appears it is only for accessing Drive.

I then followed Google Calendar API v3 Access Not Configured which states you only need an API key but this appears as though it is all done from client side so maybe it's different?

I looked at https://developers.google.com/google-apps/calendar/quickstart/nodejs as well but that seems very complicated just to make a simple API call to a calendar. The sample code references files which isn't clear where they come from or how to structure them. E.g. var TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json';

I simple guide on how to achieve this would be hugely appreciated.

Thanks

Stretch0
  • 8,362
  • 13
  • 71
  • 133

2 Answers2

27

I was in the same situation as you. Google does not have documentation for server-to-server authentication for nodejs client API. Ridiculous. Finally I found the solution here. Basically you need a service account key (JSON file usually) and google.auth.JWT server-to-server OAuth 2.0 client.

let google = require('googleapis');
let privatekey = require("./privatekey.json");
// configure a JWT auth client
let jwtClient = new google.auth.JWT(
       privatekey.client_email,
       null,
       privatekey.private_key,
       ['https://www.googleapis.com/auth/calendar']);
//authenticate request
jwtClient.authorize(function (err, tokens) {
 if (err) {
   console.log(err);
   return;
 } else {
   console.log("Successfully connected!");
 }
});

Now just call calendar API like that:

let calendar = google.calendar('v3');
calendar.events.list({
   auth: jwtClient,
   calendarId: 'primary'//whatever
}, function (err, response) {

});
ZuzEL
  • 12,768
  • 8
  • 47
  • 68
  • 2
    Wow, the ability to better understand how to just obtain events from the calendar API through this response over Google's own documentation is ridiculous, thanks for this. – Josh Valdivieso Jun 14 '18 at 19:04
  • You save my day :) – Timothy Lee Jun 15 '18 at 03:22
  • @zuzel, how could your application privatekey be able to access the data on a different person's calendar (calendarId: 'primary')? Am I missing anything here? – chen Oct 30 '19 at 00:31
8

I believe you want to add and remove events in a calendar using Node.js.

About quickstart.js for using calendar API, in order to use calendar API, at first, users have to retrieve client_secret.json with client ID, client secret and so on, and enable calendar API at API console.

As a next step, access token and refresh token have to be retrieved from Google using client_secret.json. Most of quickstart.js in Quickstart is used to retrieve them. var TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json'; includes the access token and refresh token retrieved using client_secret.json. The access token which has the expiration time can be retrieved from the refresh token which doesn't have the expiration time. At quickstart.js, access token is retrieved every running the script using the refresh token.

Functions except for listEvents(auth) in the quickstart.js are used for the authorization. At listEvents(auth), calendar API can be used by using the access token retrieved by the authorization.

Sample script

Following sample script is for adding and removing events. This supposes that Step 1 and Step 2 in Quickstart have already finished and quickstart.js is used.

For Node.js Quickstart sample, it modified listEvents(). When you use this sample script, please copy and paste Node.js Quickstart sample and change listEvents() as follows, and add following addEvents() and removeEvents().

function listEvents(auth) {
  var calendar = google.calendar('v3');

  addEvents(auth, calendar); // Add events
  removeEvents(auth, calendar); // Remove events
}

1. Add events

Detail information is https://developers.google.com/google-apps/calendar/v3/reference/events/insert.

function addEvents(auth, calendar){
  calendar.events.insert({
    auth: auth,
    calendarId: 'primary',
    resource: {
      'summary': 'Sample Event',
      'description': 'Sample description',
      'start': {
        'dateTime': '2017-01-01T00:00:00',
        'timeZone': 'GMT',
      },
      'end': {
        'dateTime': '2017-01-01T01:00:00',
        'timeZone': 'GMT',
      },
    },
  }, function(err, res) {
    if (err) {
      console.log('Error: ' + err);
      return;
    }
    console.log(res);
  });
}

2. Remove events

Detail information is https://developers.google.com/google-apps/calendar/v3/reference/events/delete.

function removeEvents(auth, calendar){
  calendar.events.delete({
    auth: auth,
    calendarId: 'primary',
    eventId: "#####",
  }, function(err) {
    if (err) {
      console.log('Error: ' + err);
      return;
    }
    console.log("Removed");
  });
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Tanaike
  • 181,128
  • 11
  • 97
  • 165