google-api-nodejs-client icon indicating copy to clipboard operation
google-api-nodejs-client copied to clipboard

Calendar API doesn't work with service accounts

Open sauravexodus opened this issue 7 years ago • 12 comments

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

sauravexodus avatar Jan 31 '19 13:01 sauravexodus

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?

JustinBeckwith avatar Jan 31 '19 16:01 JustinBeckwith

Yes. It's a gsuite account

sauravexodus avatar Feb 02 '19 09:02 sauravexodus

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

sauravexodus avatar Feb 02 '19 09:02 sauravexodus

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

mharrisweb avatar Feb 22 '19 10:02 mharrisweb

I wrote my own wrappers around the REST API.

sauravexodus avatar Mar 04 '19 07:03 sauravexodus

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.

vishald123 avatar Apr 15 '19 15:04 vishald123

@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?

bcoe avatar Apr 18 '19 00:04 bcoe

I'll try it out. Thanks 👍

sauravexodus avatar Apr 21 '19 17:04 sauravexodus

Please see https://github.com/googleapis/google-auth-library-nodejs#json-web-tokens

Palisand avatar Jun 03 '19 00:06 Palisand

@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.

bcoe avatar Jun 03 '19 12:06 bcoe

@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 ?

Nicolas-vrcc avatar Nov 06 '20 12:11 Nicolas-vrcc

@vishald123 Worked for me as well! thanks!!

joliveirarodrigues avatar Oct 03 '22 22:10 joliveirarodrigues