1. Have a way to dynamically create Google OAuth tokens on the fly
First, create a function to generate Google OAuth tokens and ensure they have the proper scopes (for Google Calendar).
2. Configure Google Workspace correctly
If you are using Google Workspace and you want invitees to receive calendar invitations, you must (as an Admin) navigate to the below menu:
Apps > Google Workspace > Calendar > Sharing Settings
And disable the setting “Warn users when inviting guests outside of domain.”
This took me forever to figure out but was the only way I could actually generate notifications for the target email (with it enabled, the API call will still generate an event on their calendar, strangely).
Get google_calendar_id
This is relatively straightforward and can be done from your Google Calendar UI.
Get google_calendar_event_id
This is trickier. ChatGPT told me that you can get this from the Google Calendar user interface but that is not correct. What you need to do is execute a GET request against the existing google_calendar_id, sort through the responses, and get the event, e.g.:
GET https://www.googleapis.com/calendar/v3/calendars/«google_calendar_id»/events
You can then access the items
array in the response to find your event’s id:
Build function stack
Alright, now you are ready to build your function stack. Below is the full thing but I break down each part in the subsequent sections.
Conditional logic for getting OAuth token
This setup allows me to default to using whichever OAuth token is passed to the function, usually dynamically generated immediate before using the token generation function I built previously. But because I generally store the Google OAuth token as an environment variable, I want to allow pulling it from there if no value is passed to help save time in development.
Create authorization string
I use this to generate a variable that I can plug directly into the headers for the API call, but it isn’t strictly necessary. Note that there is a space after the word “Bearer”.
Get existing attendees
I could not find a way to simply update the list of attendees, even when using a PATCH request. Thus, unless you want wholesale replace any existing attendee(s) with another one, you’ll need to GET the existing attendees.
Generate URL string
Similar to the authorization string, I create the target URL string in a separate variable just for ease of use and readability.
GET API request
Create an object in which you set the email to be added
set responseStatus
to needsAction
so that it requires response from the invitee.
Extract attendee emails
This just pulls out existing attendees as an array.
Conditional logic to check if attendee list is empty
If the current_attendee_emails
is empty, you’ll want to create an empty array to which you can add the new_attendee_object
. Otherwise current_attendee_emails
would be null if there are no existing attendees, which would throw an error.
Update current_attendee_emails
Add new_attendee_object
to the current_attendee_emails array.
Rebuild the url_string
Append ?sendUpdates=all
to send updates to everyone, but you can customize based on the Google API documentation.