Simple node module that supports Google Calendar Events API
This module does server to server authentication with Google APIs without any users being involved. When using Google APIs from the server (or any non-browser based application), authentication is performed through a Service Account, which is a special account representing your application.
Find out more about preparations needed to setting up the service account, grant calendar access, auth key to google and the configurations needed to start using node-google-calendar.
First, install the package with: npm i node-google-calendar
.
Provide in the settings.js
config file with serviceAcctId, calendarIds, timezone & keyfile location.
Check out preparations needed if you have trouble supplying these configurations. Sample config file here.
Your config file should look something like this:
const SERVICE_ACCT_ID = '<your service account id>';
const CALENDAR_ID = {
'primary': '',
'calendar-1': '[email protected]',
'calendar-2': '[email protected]'
};
const TIMEZONE = 'UTC+08:00';
const KEYFILE = '<yourpem.pem>';
module.exports.serviceAcctId = SERVICE_ACCT_ID;
module.exports.calendarId = CALENDAR_ID;
module.exports.keyfile = KEYFILE; //or if using json keys - module.exports.key = key;
module.exports.timezone = TIMEZONE;
To use, require this module in your project and pass in the configurations file.
const CONFIG = require('./config/Settings');
const CalendarAPI = require('node-google-calendar');
let cal = new CalendarAPI(CONFIG);
You should now be able to query your specified calendar and try out the following examples.
Google Calendar APIs v3 supported includes APIs in resource types of Events, FreeBusy & Settings. Some examples are as follows:
Events Examples
Events.list - To Get a promise of all single events in calendar within a time period.
let params = {
timeMin: '2017-05-20T06:00:00+08:00',
timeMax: '2017-05-25T22:00:00+08:00',
q: 'query term',
singleEvents: true,
orderBy: 'startTime'
}; //Optional query parameters referencing google APIs
cal.Events.list(calendarId, params)
.then(json => {
//Success
console.log('List of events on calendar within time-range:');
console.log(json);
}).catch(err => {
//Error
console.log('Error: listSingleEvents -' + err);
});
Events.insert - Insert an event on a specified calendar. Returns promise of details of new event.
let event = {
'start': { 'dateTime': '2017-05-20T07:00:00+08:00' },
'end': { 'dateTime': '2017-05-20T08:00:00+08:00' },
'location': 'Coffeeshop',
'summary': 'Breakfast',
'status': 'confirmed',
'description': '',
'colorId': 1
};
cal.Events.insert(calendarId, event)
.then(resp => {
console.log('inserted event:');
console.log(resp);
})
.catch(err => {
console.log('Error: insertEvent-' + err);
});
Events.delete - Deletes an Event on a specified Calendar with EventId. Returns promise of results.
let params = {
sendNotifications: true
};
cal.Events.delete(calendarId, eventId, params)
.then(function(jsonResults) {
console.log('delete Event:' + JSON.stringify(jsonResults));
}, function(err) {
console.log('Error deleteEvent: ' + JSON.stringify(err));
});
FreeBusy Examples
FreeBusy.query - Checks if queried calendar slot is busy during selected period. Returns promise of list of events at specified slot.
let params = {
"timeMin": '2017-05-20T08:00:00+08:00',
"timeMax": '2017-05-20T09:00:00+08:00',
"items": [{ "id": calendarId }]
};
cal.FreeBusy.query(calendarId, params)
.then(resp => {
console.log('List of busy timings with events within defined time range: ');
console.log(resp);
})
.catch(err => {
console.log('Error: checkBusy -' + err);
});
Settings Examples
Settings.list - List user settings
let params = {};
cal.Settings.list(params)
.then(resp => {
console.log('List settings with settingID: ' + settingId);
console.log(resp);
})
.catch(err => {
console.log('Error: getSettings -' + err);
});
More examples here.