googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.28k stars 1.91k forks source link

Calendar API doesn't work with service accounts #1574

Closed sauravexodus closed 5 years ago

sauravexodus commented 5 years ago

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 = 'someexample_rd59mrlsetc9dp2snre44r54bo@group.calendar.google.com'
  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

JustinBeckwith commented 5 years ago

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?

sauravexodus commented 5 years ago

Yes. It's a gsuite account

sauravexodus commented 5 years ago

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

mharrisweb commented 5 years ago

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

sauravexodus commented 5 years ago

I wrote my own wrappers around the REST API.

vishald123 commented 5 years ago

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 = 'someexample_rd59mrlsetc9dp2snre44r54bo@group.calendar.google.com';

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

bcoe commented 5 years ago

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

sauravexodus commented 5 years ago

I'll try it out. Thanks 👍

Palisand commented 5 years ago

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

bcoe commented 5 years ago

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

Nicolas-vrcc commented 3 years ago

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

joliveirarodrigues commented 1 year ago

@vishald123 Worked for me as well! thanks!!