0
votes

I am trying to access a private Google Calendar, using the NodeJS Google API package, but get the error "Error: Not Found":

Error: Not Found
    at createError (/Users/devacc/myproject/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/Users/devacc/myproject/node_modules/axios/lib/core/settle.js:18:12)
    at Unzip.handleStreamEnd (/Users/devacc/myproject/node_modules/axios/lib/adapters/http.js:201:11)
    at emitNone (events.js:111:20)
    at Unzip.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1056:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

The user that has the 'service to service' key via the Google Developer console has full rights to the private calendar. In the permissions the roles 'project -> viewer' and 'project -> owner' have both been specified. If there is another role to be used, then I did not find it.

The code I am using is as follow, with the 'apiKey' parameter being the parsed JSON file, from creating the 'service to service' account and 'calendarId' being the calendar ID.

function authorize (key) {
    return new Promise((resolve, reject) => {
        var jwtClient = new google.auth.JWT(
            key.client_email,
            null,
            key.private_key,
            ['https://www.googleapis.com/auth/calendar.readonly'],
            null
        );

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

        jwtClient.authorize((err) => {
           if (err) {
               reject(err);
           } else {
               resolve(calendar);
           }
        });
    });
}

function listEvents(calendarId, apiKey, days = 2) {
    return authorize(apiKey)
        .then((calendar) => {
            return new Promise((resolve, reject) => {
                var minTime = moment();
                var maxTime = minTime.clone().add((days ? days : defaultDays), 'days');

                calendar.events.list({
                    calendarId: calendarId,
                    orderBy: 'startTime',
                    showDeleted: 'false',
                    singleEvents: 'true',
                    timeMin: minTime.toISOString(),
                    timeMax: maxTime.toISOString()
                }, (err, response) => {
                    if (err) {
                        reject(err);
                    } else {
                        const data = response.data;
                        resolve(data.items);
                    }
                });
            });
        });
}

I should note that using the above code with a public calendar works fine. It is just the private calendar that is causing me the issue. The calendar is part of the same GSuite organisation as the user with the key.

1

1 Answers

1
votes

Turns out I need to add the service account as a user on the calendar. That is the one of form:

<name>@<project>.iam.gserviceaccount.com