Google-api-nodejs-client: Calendar API doesn't work with service accounts

Created on 31 Jan 2019  路  11Comments  路  Source: googleapis/google-api-nodejs-client

I am trying to use the calendar API to create secondary calendars and access calendar events:

Code

  const token = await jwtAuth()
  const api = google.calendar({ version: 'v3', auth: token })
  const calendarId = '[email protected]'
  const response = await api.calendars.get({ calendarId, auth: token })

This throws an error saying:

UnhandledPromiseRejectionWarning: Error: Login Required
warning.js:18
    at Gaxios.<anonymous> (/Users/sauravchandra/Documents/NodeProjects/CalendarApi/node_modules/gaxios/build/src/gaxios.js:64:27)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/sauravchandra/Documents/NodeProjects/CalendarApi/node_modules/gaxios/build/src/gaxios.js:16:58)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)

Scopes: https://www.googleapis.com/auth/calendar

needs more info question

Most helpful comment

Hi @sauravexodus, I tried the following way using a service account.

var {google} = require('googleapis');

var key = require('./[YOUR_SERVICE_ACCOUNT_FILE].json');

const SCOPES = 'https://www.googleapis.com/auth/calendar';

var auth = new google.auth.JWT(
    key.client_email,
    null,
    key.private_key,
    SCOPES,
    'YOUR_PRIMARY_EMAIL_ID'
);

const api = google.calendar({version : "v3", auth : auth});
const calendarId = '[email protected]';

//Returns metadata for a calendar.
api.calendars.get({calendarId : calendarId}
    , function (err, resp) {
        if (err) {
            console.log(err);
        } else {
            console.log(resp);
        }
    })

//Creates a secondary calendar       
api.calendars.insert({requestBody : { summary : "test2"}},
     function (err, res) {
         if(err) {
             console.log(err);
         } else {
             console.log(res);
         }
     })

// Make an authorized request to list Calendar events.
    api.events.list({
        calendarId: calendarId
    }, function (err, resp) {
        if (err) {
            console.log(err)
        } else {
            console.log(resp.data.items);
        }
    });

Let me know if it works for you.

All 11 comments

Greetings! Calendar only supports OAuth2 as far as I know:
https://developers.google.com/calendar/auth

There is a blurb down there about domain wide delegation for gsuite customers. Are you trying to do this with a gsuite account?

Yes. It's a gsuite account

I also tried the REST API. The same token works there.

I get a 401 login required error, when I try and use a service account to access the api. Did you find a solution?

I wrote my own wrappers around the REST API.

Hi @sauravexodus, I tried the following way using a service account.

var {google} = require('googleapis');

var key = require('./[YOUR_SERVICE_ACCOUNT_FILE].json');

const SCOPES = 'https://www.googleapis.com/auth/calendar';

var auth = new google.auth.JWT(
    key.client_email,
    null,
    key.private_key,
    SCOPES,
    'YOUR_PRIMARY_EMAIL_ID'
);

const api = google.calendar({version : "v3", auth : auth});
const calendarId = '[email protected]';

//Returns metadata for a calendar.
api.calendars.get({calendarId : calendarId}
    , function (err, resp) {
        if (err) {
            console.log(err);
        } else {
            console.log(resp);
        }
    })

//Creates a secondary calendar       
api.calendars.insert({requestBody : { summary : "test2"}},
     function (err, res) {
         if(err) {
             console.log(err);
         } else {
             console.log(res);
         }
     })

// Make an authorized request to list Calendar events.
    api.events.list({
        calendarId: calendarId
    }, function (err, resp) {
        if (err) {
            console.log(err)
        } else {
            console.log(resp.data.items);
        }
    });

Let me know if it works for you.

@sauravexodus if an approach like @vishald123's doesn't work for you, could you please provide a gist or small GitHub repo that demonstrates the approach you're taking?

I'll try it out. Thanks 馃憤

@sauravexodus since we haven't heard back from you in a bit, I'm going to close this issue (in the hopes that @Palisand or @vishald123's solution worked for you).

If you're continuing to bump into issues, please feel free to reopen this issue and we can keep digging.

@vishald123
Thanks a lot, it seems to work for me.
However I have a 401 error when running it and don't know why.
Also what is the 'YOUR_PRIMARY_EMAIL_ID' supposed to be here ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eduDorus picture eduDorus  路  3Comments

streamnsight picture streamnsight  路  4Comments

ashishbajaj99 picture ashishbajaj99  路  3Comments

leecheve picture leecheve  路  3Comments

ACMerriman picture ACMerriman  路  3Comments