0.0.1 • Published 11 years ago

google-oauth-serviceaccount v0.0.1

Weekly downloads
4
License
-
Repository
github
Last release
11 years ago

google-oauth-serviceaccount

Motivation:

Wanted to accesss my personal calendar, NOT the one of Google Apps, and found no good example for node.js with service account without any interaction.

Install:

npm install google-oauth-serviceaccount

Preparation:

  1. Create your service account and enable calendar access(thanks to this post):
  2. Store the p12 file into pem with openssl(thank this)

    $ openssl pkcs12 -in yourkey.p12 -out yourkey.pem -nodes type "notasecret" for password.

  3. Share the calendar with your developer account

    Calendar "Settings" -> "Share this calendar", at "Share with specific people", put the email address of developer account choosing a permission you want, then "add person". The address should be like "yourProjectId@developer.gserviceaccount.com".

  4. Edit oahtu-config.json and place key.pem which generated in step 2.

  5. Test with test.js.

    if successfull, you'll see an access token:

  6. Enjoy with calendar APIs. You may try listEvents.js.

Usage example:

const request = require('request');
const gaccount = require('./google-oauth-serviceaccount');

const calRoot = "https://www.googleapis.com/calendar/v3";

gaccount.auth(function(err, access_token){

    var token = "?access_token="+access_token;

    request.get({
        url:calRoot+"/users/me/calendarList"+token,
        json:true
        }, function(err, res, body){

            for(var c=0; c<body.items.length; c++){
                console.log(body.items[c]);

                request.get({
                    url:calRoot+"/calendars/"+body.items[c].id+"/events"+token,
                    json:true
                    }, function(err, res, cal){
                        console.log(cal);
                });
            }
    })

});