question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calendar API doesn't work with service accounts

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

23reactions
vishald123commented, Apr 15, 2019

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.

4reactions
sauravexoduscommented, Mar 4, 2019

I wrote my own wrappers around the REST API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google Calendar API Service Account Error - Stack Overflow
1 - Provide calendar scopes to your service account. Go to https://admin.google.com/ and login with G Suite account. · 2 - Your user...
Read more >
Can't create an event when using service account credentials
According to the reply from Google on the issue tracker, either your service account has to create the calendar in question, or you...
Read more >
Google API and Service Accounts: Get Up and Running in 30 ...
A comprehensive, step-by-step guide to help you set up Salesforce and Google in order to make authenticated callouts to Google APIs.
Read more >
Service account is unable to invite calendar ... - Issue Tracker
A short description of the issue: We use the Ruby client to request Google Calendar API using the service account. ... The issue...
Read more >
Accessing calendars from service account - Google Groups
Do I need to manually add the service account email to those calendars or ... documentation that says that the calendar API does...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found