Google Calendar API Service Account Error

Google service account unable to list calendars?

From your explanation and the returned value, I'm worried that in your situation, you might have never inserted the shared Calendar with the service account. If my understanding is correct, how about the following modification?

Modification points:

  1. Insert the shared Calendar to the service account.

    • In this case, please modify the scope https://www.googleapis.com/auth/calendar.readonly to https://www.googleapis.com/auth/calendar.

        async init () {
      // loads the configuration from our app's configuration file
      this.keyConfig = getKeyConfig('google-calendar');

      const calendarApi = await this.getCalendarApi();
      const response = await calendarApi.calendarList.insert({resource: {id: "###@group.calendar.google.com"}}); // Please set the Calendar ID here. Or {requestBody: {id: "###@group.calendar.google.com"}}
      console.log(JSON.stringify(response.data, undefined, 2));
      }
  2. Retrieve the calendar list using your script.

    • After the Calendar was inserted to the service account using the above script, you can obtain the shared Calendar in the Calendar list.

Reference:

  • CalendarList: insert

Cannot get access to Google Calendar API via Service Account

So I managed to figure this one out myself. Seems that my service account was setup correctly, but the permissions I added were not. For the record, here's what I wanted:

  1. Get read-only access to a calendar.
  2. Be able to retrieve the calendar of any user in my organization.

I had created the service account, made sure it had Domain Wide Delegation authorization, added the Calendar and Admin APIs to my project that holds the service account, but I couldn't get it to work without adding my service account to the calendar per @Alex Baban's message above.

I went back to the drawing board as adding this to a few dozen accounts wasn't an ideal solution, and then I found it. I had to add the following authorizations for my service account:

  • https://www.googleapis.com/auth/admin.directory.resource.calendar
  • https://www.googleapis.com/auth/calendar.events.readonly
  • https://www.googleapis.com/auth/calendar.readonly

Hopefully this helps someone else out in the future.

Google Calendar API and Service Account permission error

How about this modification?

I think that in your script, the endpoint and/or scope might be not correct.

Pattern 1:

In this pattern, your endpoint of https://dns.googleapis.com/dns/v1/projects/${keys.project_id} is used.

Modified script:

const { JWT } = require("google-auth-library");
const keys = require("###"); // Please set the filename of credential file of the service account.

async function main() {
const calendarId = "ip15lduoirvpitbgc4ppm777ag@group.calendar.google.com";
const client = new JWT(keys.client_email, null, keys.private_key, [
'https://www.googleapis.com/auth/cloud-platform' // <--- Modified
]);
const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
const res = await client.request({ url });
console.log(res.data);
}

main().catch(console.error);
  • In this case, it is required to enable Cloud DNS API at API console. And it is required to pay. Please be careful with this.
    • I thought that the reason of your error message of Insufficient Permission might be this.

Pattern 2:

In this pattern, as a sample situation, the event list is retrieved from the calendar shared with the service account. If the calendar can be used with the service account, the event list is returned. By this, I think that you can confirm whether the script works.

Modified script:

const { JWT } = require("google-auth-library");
const keys = require("###"); // Please set the filename of credential file of the service account.

async function main() {
const calendarId = "###"; // Please set the calendar ID.

const client = new JWT(keys.client_email, null, keys.private_key, [
"https://www.googleapis.com/auth/calendar"
]);
const url = `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`; // <--- Modified
const res = await client.request({ url });
console.log(res.data);
}

main().catch(console.error);

Note:

  • This modified script supposes that you are using google-auth-library-nodejs of the latest version.

Reference:

  • JSON Web Tokens in google-auth-library-nodejs

Google Calendar API HttpError 404 when using service account

CalendarList is the list on the bottom left of the google calendar web application.

Unless you have inserted the calendar into the service accounts calendarlist using clanedarlist.insert, its not going to show up in that list.

Once you have shared a calendar with the service account just do a calendar.get on the calendar id you shared and you will be able to access it. You can then do a calendarlist.insert if you really want it in the calendar list.

Cannot create events in google calendar with service account

You appear to have forgotten to specify which user you want the service account to delegate to.

from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/calendar']
SERVICE_ACCOUNT_FILE = '/path/to/service.json'

credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)

delegated_credentials = credentials.with_subject('user@example.org')


Related Topics



Leave a reply



Submit