Google Calendar API Node JS, can't add attendees with service account

Viewed 2067

I'm currently trying to develop a simple Node JS program deployed on a GCP Cloud Function, to use Google Calendar API with a Service Account.

The program is simple, i just want to create an event and add attendees.

My code works well, however i can't add attendees with the service account i have this error :

There was an error contacting the Calendar service: Error: Service accounts cannot invite attendees without Domain-Wide Delegation of Authority.

The API are activated and i allowed the calendar scope on the GSuite Admin Security, my service account have the delegation authority checked on GCP.

I search a lot a solution but nothing solve my problem..

This is the code of my module :

const { google } = require('googleapis');
const sheets = google.sheets('v4');

const SCOPES = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/calendar'];
let privatekey = require("../creds.json");

async function getAuthToken() {
    let jwtClient = new google.auth.JWT(
           privatekey.client_email,
           null,
           privatekey.private_key,
           SCOPES);
    //authenticate request
    jwtClient.authorize(function (err, tokens) {
     if (err) {
       console.log(err);
       return;
     } else {
       console.log("Successfully connected!");
     }
    });
    return jwtClient;
}


async function insertEvents(auth,items) {

  const calendar = google.calendar({ version: 'v3', auth });

  var startDate = new Date();
  var endDate = new Date();
  startDate.setHours(14, 0, 0, 0);
  endDate.setHours(15, 0, 0, 0);

    var attendees = [];

    items.forEach(function(item){
       attendees.push({email: item[2]});
    });


  var event = {
    summary: 'Stack Roulette : It\'s Coffee Time =)',
    location: 'Partout, mais avec un café',
    description: "",
    start: {
      dateTime: startDate.toISOString(),
      timeZone: 'Europe/London'
    },
    end: {
      dateTime: endDate.toISOString(),
      timeZone: 'Europe/London'
    },
   attendees: attendees,
    reminders: {
      useDefault: false,
      overrides: [
        { method: 'email', minutes: 24 * 60 },
        { method: 'popup', minutes: 10 }
      ]
    },
    conferenceData: {
    createRequest: {requestId: Math.random().toString(36).substring(2, 10),
      conferenceSolution: {
        key: {
            type: "hangoutsMeet"
        }
      },}
  }
  };



  calendar.events.insert(
    {
      auth: auth,
      calendarId: 'primary',
      resource: event,
      conferenceDataVersion: 1
    },
    function(err, event) {
      if (err) {
        console.log(
          'There was an error contacting the Calendar service: ' + err
        );
        return;
      }
      console.log('Event created: %s', event.data.htmlLink);
    }
  );
}

module.exports = {
  getAuthToken,
  getSpreadSheetValues,
  insertEvents
}

I precise, there is no Frontend for my application, the code run with a cloud Function like an API Endpoint.

PS : it's not a Firebase Cloud Function, but GCP Cloud Function If i remove attendees from event creation, it's work well but can't see the event.

Thx for your help

2 Answers

To set up Domain-Wide Delegation of Authority

  • Visit your cloud console
  • Go to IAM & Admin -> Service Accounts
  • Chose the service account you using in your script
  • Check Enable G Suite Domain-wide Delegation

If you are sure that you already enabled the domain-wide delegation for the correct service account and you encounter issues only when creating events with attendees:

Your issue is likely related to this issue reported on Google's Issue Tracker:

Currently there are restrictions for creation of events with attendees with a service account, especially when the attendees are outside of your domain.

When you create jwtClient, I think you have to put user primary email to JWT method.

Like:

let jwtClient = new google.auth.JWT(
           privatekey.client_email,
           null,
           privatekey.private_key,
           SCOPES,
           "yourUserPrimaryEmail"); // here
Related