Accessing Google Calendar API from Node server

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) {

});

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");
  });
}