Skip to content

Calendar Feature Integration

Stephen Noble edited this page Mar 4, 2018 · 6 revisions

Calendar Feature Integration Using Google Calendar API

Background

The Calendar Feature is now functional because of Google's API. This web application is synced with a Google calendar dedicated for this web application alone. This app contains a unique code (coded in a javascript file) that allows the usage of Google's API.
The problem that we encountered is the lack of integration of this calendar with Google's API.
This wiki page will list the solutions to ensure that the features of this calendar will integrate well with Google's API.

Possible Solution

A possible solution to solve the problem of lacking integration to the rest of the system is by having a local entry to the database and an entry to Google Calendar. These two entries must be in sync to ensure data integrity, and it must follow the CRUD method at the same time.

This local event entry on the database will allow the ability for parents to sign up their children for events by storing the event's objectID into the child's events attribute in the database. Therefore, that child will be signed up for a selected event. Also the local entry of events will have a link to the Event's entry on Google Calendar.

The entry that will be sent to Google will allow the storage of event details, and also allow the end users to see the details of that particular event.

To summarize:

  • Local Entry of Event into the database contains the event's Object ID
  • Google Entry of Event contains all the other details of the event.

To make sure that the insertion of the event remains in sync locally and through Google, a method must be in place to make sure that if any of the two event insertions fail, then the successful insertion should be deleted or undone.

Using Google's API Services

The code block below (written by Google) is the code that is needed to access Google's API. It must be used to Create, Retrieve, Update, and Delete events on Google Calendar.

var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');

// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-nodejs-quickstart.json
var SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
    process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json';

// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
    if (err) {
        console.log('Error loading client secret file: ' + err);
        return;
    }
    // Authorize a client with the loaded credentials, then call the
    // Google Calendar API.
    authorize(JSON.parse(content), listEvents);
});

/**
 * Create an OAuth2 client with the given credentials, and then execute the
 * given callback function.
 *
 * @param {Object} credentials The authorization client credentials.
 * @param {function} callback The callback to call with the authorized client.
 */
function authorize(credentials, callback) {
    var clientSecret = credentials.installed.client_secret;
    var clientId = credentials.installed.client_id;
    var redirectUrl = credentials.installed.redirect_uris[0];
    var auth = new googleAuth();
    var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

    // Check if we have previously stored a token.
    fs.readFile(TOKEN_PATH, function(err, token) {
        if (err) {
            getNewToken(oauth2Client, callback);
        } else {
            oauth2Client.credentials = JSON.parse(token);
            callback(oauth2Client);
        }
    });
}

/**
 * Get and store new token after prompting for user authorization, and then
 * execute the given callback with the authorized OAuth2 client.
 *
 * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
 * @param {getEventsCallback} callback The callback to call with the authorized
 *     client.
 */
function getNewToken(oauth2Client, callback) {
    var authUrl = oauth2Client.generateAuthUrl({
        access_type: 'offline',
        scope: SCOPES
    });
    console.log('Authorize this app by visiting this url: ', authUrl);
    var rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    rl.question('Enter the code from that page here: ', function(code) {
        rl.close();
        oauth2Client.getToken(code, function(err, token) {
            if (err) {
                console.log('Error while trying to retrieve access token', err);
                return;
            }
            oauth2Client.credentials = token;
            storeToken(token);
            callback(oauth2Client);
        });
    });
}

/**
 * Store token to disk be used in later program executions.
 *
 * @param {Object} token The token to store to disk.
 */
function storeToken(token) {
    try {
        fs.mkdirSync(TOKEN_DIR);
    } catch (err) {
        if (err.code != 'EEXIST') {
            throw err;
        }
    }
    fs.writeFile(TOKEN_PATH, JSON.stringify(token));
    console.log('Token stored to ' + TOKEN_PATH);
}

This Link has the list of all the functions that can be used to manage events and calendars.

Since the client keys needed to run the API on the application has already been coded and stored in a JSON file, anyone that is working on this project may use the API. However, not anyone may have full access to Google Developer's Console. Google Developer Console shows the API Keys and other credentials in order to access Google's API. A Google account is needed in order to be a part of the Google Developer Console Access, but it does not seem essential in the process of working on this project.