Closed sauravexodus closed 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?
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 = '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.
@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 ?
@vishald123 Worked for me as well! thanks!!
I am trying to use the calendar API to create secondary calendars and access calendar events:
Code
This throws an error saying:
Scopes:
https://www.googleapis.com/auth/calendar