Search code examples
javascriptgoogle-apigoogle-calendar-apigoogle-api-js-client

CalendarList: list not returning list of calendars


I am trying to get a list of users calendars from the google api:

async function main() {
    const client = new google.auth.JWT(
        privatekey.client_email,
        null,
        privatekey.private_key,
        ["https://www.googleapis.com/auth/calendar"]
    );

    const url = 'https://www.googleapis.com/calendar/v3/users/me/calendarList';

    const res = await client.request({ url });
    console.log(res.data);
}

I get an empty items array:

info: { kind: 'calendar#calendarList',
  etag: '"p3000"',
  nextSyncToken:
   'CAASPWdvb2dsZWNhbGVuZGFyc2VydmljZUBldmVudGFwaS0yMTkwMTEuaWFtLmdzZXJ2aWNlYWNjb3VudC5jb20=',
  items: [] }
info: { kind: 'calendar#calendarList',
  etag: '"p3000"',
  nextSyncToken:
   'CAASPWdvb2dsZWNhbGVuZGFyc2VydmljZUBldmVudGFwaS0yMTkwMTEuaWFtLmdzZXJ2aWNlYWNjb3VudC5jb20=',
  items: [] }

When I use the api explorer I can get the calendar list I need. Any ideas how I can get a list of calendars?

config:

{
  "type": "service_account",
  "project_id": "xx",
  "private_key_id": "xxx",
  "private_key": "-----BEGIN PRIVATE KEY-----xxxn-----END PRIVATE KEY-----\n",
  "client_email": "googlecalendarservice@eventapi-219011.iam.gserviceaccount.com",
  "client_id": "111757078202416067272",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/googlecalendarservice%40eventapi-219011.iam.gserviceaccount.com"

}


Solution

  • Needed to give service account access to each calendar.