Google Apps Script OAuth2 Service Account Authentication with Google Workspace Calendar and Admin APIs
This script is a Google Apps Script designed for authenticating a Google Workspace service account using OAuth2. It allows for event and group management on Google Calendar and Google Admin APIs.
- Authenticate using a Google Workspace service account
- Access Google Calendar events of multiple users in the organization
- Add guests to calendar events
- Retrieve group members from Google Workspace Admin
- Go to Google Cloud Console.
- Select your project (or create a new one).
- Navigate to IAM & Admin > Service Accounts.
- Click on Create Service Account and provide a name and description.
- Click Done to finish creating the service account.
- Under Service Accounts, locate your newly created account.
- Click Actions (⋮) > Manage keys.
- Add a new key, selecting JSON format, and download it.
- Copy the
private_key
andclient_email
from this JSON file for later use in your Google Apps Script.
- In the APIs & Services > Library, enable:
- Google Calendar API
- Admin SDK API
-
Under IAM & Admin > Service Accounts, select your service account.
-
Click Edit and enable Domain-wide Delegation.
-
Copy the Client ID.
-
Navigate to Security > API controls > Domain-wide delegation.
-
Add a new API client:
- Client ID: Paste the client ID from your service account.
- OAuth Scopes: Add the required scopes:
https://www.googleapis.com/auth/calendar
https://www.googleapis.com/auth/admin.directory.group.member.readonly
- Go to Google Apps Script.
- Create a new project and paste the code provided above.
- OAuth2 Library: In Apps Script, click on Extensions > Libraries.
- Add the OAuth2 Library using the Script ID:
1B1MJKqwZLR3T7p0C9Z6zaJuZCWqZUKGJmSaNG82hrMoy3KxMysKxiEZW
. - Choose the latest version and click Add.
Replace placeholders with actual values from your service account:
privateKey
: Service account private key from your JSON file.serviceAccountEmail
: Service account email.userEmail
: Email of the user to impersonate in your organization.
- In the Google Apps Script editor, go to Triggers (clock icon in left sidebar).
- Create a new trigger:
- Function:
onCalendarEventCreated
- Event source: Select Time-driven
- Type of time-based trigger: Set the frequency as required, e.g., Every hour.
- Function:
- Run the
testAuth()
function to confirm successful authentication. - Check Logs (View > Logs) to view authentication success or failure messages.
getServiceAccountAuth()
: Authenticates the service account, using domain-wide delegation to impersonate a user.getUsersFromGroup()
: Retrieves emails of members in a specific Google Workspace group.onCalendarEventCreated(e)
: Triggered on calendar event creation. It checks if the current user is in the monitored list and processes relevant events.processEvent()
: Adds a guest to the event if not already present.checkAndUpdateRecentEvents()
: Checks for recent calendar events created by monitored users and processes them.
- Ensure the service account has permissions set correctly in the Google Workspace Admin Console.
- For detailed usage of each function, check the inline documentation within the script.
- Review logging information in Google Apps Script logs for troubleshooting.
For more details on setting up Google Workspace APIs, refer to Google's OAuth2 Documentation.