0
votes

I'm using the Google Calendar API in my Angular 9 Web Application and trying to create events on a Google Calendar within my G Suite domain

In the G Suite Admin Console, I've given the service account permissions for the scopes https://www.googleapis.com/auth/calendar and https://www.googleapis.com/auth/calendar.events

I created the calendar itself under my account and have added the service account with "Make changes to events" permissions

NOTE: I tried the normal gmail account. It is working. But when I try the google business account, I have above problem(Like writer access error when setting up Google calendar event create).

I am using this code for saving a calendar event:

Please find below Screen Shot.

const jwtClient = new google.auth.JWT(
    GOOGLE_CLIENT_EMAIL,
    null,
    GOOGLE_PRIVATE_KEY,
    ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.events']
);
const calendar = google.calendar({
    version: 'v3',
    project: GOOGLE_PROJECT_NUMBER,
    auth: jwtClient
});
var appointment_data = {
    summary: 'xyz',
    location: 'Location info',
    description: 'Some description',
    start: {
        'dateTime': dataObj.appt_startdate_time,
        'timeZone': dataObj.appt_timeZone,
    },
    end: {
        'dateTime': dataObj.appt_enddate_time,
        'timeZone': dataObj.appt_timeZone,
    },
    attendees: [
        {
            "displayName": 'Xyz',
            "email": '[email protected]'
        }
    ]
}
calendar.events.insert({
    calendarId: GOOGLE_CALENDAR_ID,
    resource: appointment_data,
}, function (err, event) {
    if (err) {
        console.log('There was an error contacting the Calendar service: ' + err);
        return;
    }
})

oo

1
Please add the code you are using to create the events and authenticating the service account.Alessandro
const jwtClient = new google.auth.JWT( GOOGLE_CLIENT_EMAIL, null, GOOGLE_PRIVATE_KEY, ['googleapis.com/auth/calendar', 'googleapis.com/auth/calendar.events'] ); const calendar = google.calendar({ version: 'v3', project: GOOGLE_PROJECT_NUMBER, auth: jwtClient });dara naga anjani prasad
var appointment_data = { summary: 'xyz', location: 'Location info', description: 'Some description', start: { 'dateTime': dataObj.appt_startdate_time, 'timeZone': dataObj.appt_timeZone, }, end: { 'dateTime': dataObj.appt_enddate_time, 'timeZone': dataObj.appt_timeZone, }, attendees: [ { "displayName": 'Xyz', "email": '[email protected]' } ] }dara naga anjani prasad
calendar.events.insert({ calendarId: GOOGLE_CALENDAR_ID, resource: appointment_data, }, function (err, event) { if (err) { console.log('There was an error contacting the Calendar service: ' + err); return; } })dara naga anjani prasad
Please add it in the question. The comments are really hard to read and understand. Thank youAlessandro

1 Answers

0
votes

Solution

You are not adding the impersonating email address to the JWT constructor method.

Proposed modification

You'll have to add the email address of your domain user you would like to impersonate while creating the events:

var key = require('/path/to/service-account-credentials.json');

const SCOPES = ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.events'];

var jwtClient = new google.auth.JWT(
    key.client_email,
    null,
    key.private_key,
    SCOPES,
    'impersonating-email-address' // Add this parameter
);

Now if you followed correctly these instructions it should work perfectly.

References

Calendary API Service Accounts