[removed] Google Calender + gapi: service account

2019-08-26 06:13发布

问题:

My app makes use of Firebase to log users in, and they have access to a calendar, which is especially created as a single common calendar to add and remove reservations.

The app has, so to speak, its own gmail address for this purpose. The app is accessed via the google API (gapi client).

In order for the users to be able to interact with the calendar, I have found out that I can only use the service account of the calendar / app.

So far I managed to:

  • create a service key for the service account,
  • share the calendar with the service account "dummy" user,
  • use that service key (certificate) to create a JWT token (using jsrsasign library),
  • make a POST request to get an access token for gapi,
  • initialise the gapi auth and client, and have access to the calendar via gapi

Now when I get to the point of retrieving the google Calendar events, I do get a successful response, but the array of events is empty, even though there are test events available in the shared calendar.

The response looks like this:

{
 "kind": "calendar#events",
 "etag": "\"pqef3g4h5j6j0g\"",
 "summary": "my_app_email@appspot.gserviceaccount.com",
 "updated": "2019-01-15T21:14:05.029Z",
 "timeZone": "UTC",
 "accessRole": "owner",
 "defaultReminders": [],
 "items": []
}

There are a few topics on Stackoverflow regarding this, but none of them have helpful information, or they are for Pythin / PHP.

I am hoping someone can give advice with this for Javascript...

回答1:

I resolved this... The problem was in the gapi request, when fetching the events. I was using the wrong calendarId. I had its value set to the default 'primary', but the actual calendarId to be used, can be found under the Google Calendar Settings >> Integrate Calendar. In my settings, the calendarId was the associated account's email address.

So the gapi request looks like this:

const fetchTimeLimit = new Date(2019, 0, 1).toISOString();

let events = await gapi.client.calendar.events.list({
  calendarId: 'calendar_email@gmail.com',
  timeMin: fetchTimeLimit,
  showDeleted: false,
  singleEvents: true,
  maxResults: 300,
  orderBy: 'startTime'
})
.then(response => {  ........etc