calendarmod is a Go Package designed to facilitate Google Calendar services for Rutgers University. This module includes the following core functionality:
- Authenticate Client for Google API using a service account
- Subscribe/unsubscribe user to a dynamic Google calendar
- Subscribe/unsubscribe a group of users to a dynamic Google calendar
Make sure you are using one of the stable version. Update to the newest minor version.
- v0.1.18: Stable version
- Add unsubscription features for individual user and group
- v0.1.15: Stable version
- Obtain a Google Service Account Key json file and enable the necessary Google API permissions for the Google Service Account.
- For detailed instructions, refer to the section Set up Google Cloud API Access.
- Set up a Go project to utilize this package.
- move to the directory of your Go Project in the terminal
- run the command
go get github.com/oss/calendarmod
-
Import the package
import ( "github.com/oss/calendarmod" )
-
Create an authentification client with the service account JSON file
client := calendarmod.SetUpSVAClient(serviceAccountJSON, true)
-
Subscribe the user to the calendar
calendarID:= "c_d3e80545746779e9e3957248314356fe4d9e1dcc27c2259b8c029ad5ee6f9cdf@group.calendar.google.com" userEmail:= "[email protected]" success := client.SubscribeUserToCalendar(calendarid, userEmail)
-
Import the package
import ( "github.com/oss/calendarmod" )
-
Create an authentification client with the service account JSON file
client := calendarmod.SetUpSVAClient(serviceAccountJSON, true)
-
Subscribe the group to the calendar
calendarID:= "c_d3e80545746779e9e3957248314356fe4d9e1dcc27c2259b8c029ad5ee6f9cdf@group.calendar.google.com" userlist:= ["[email protected]", "[email protected]", "[email protected]"] result := client.SubscribeGroupToCalendar(calendarID, userlist) // result includes a list of successful cases and a list of failed cases
func SetUpSVAClient(serviceAccountJSON []byte, useCalendar bool) *CalendarClient
SetUpSVAClient initializes an authentification client with the service account and returns CalendarClient with context and config.
- useCalendar should always be set to true to use Calendar services.
- To authenticate, make sure to set up all necessary permissions for the Google Service account.
Exp:
// set up Service Account authentification client
serviceAccountJSON, err := os.ReadFile("SERVICE_ACCOUNT_PATH")
if err != nil {
log.Fatalf("Could not read service account credentials file, %s => {%s}", sap, err)
}
calendarClient:= calendarmod.SetUpSVAAuth(serviceAccountJSON, true)
func (c *CalendarClient) SubscribeUserToCalendar(user string, calendarID string) bool
SubscribeUserToCalendar subscribes user to a dynamic Google Calendar. Call the function with the Calendar Client created from func SetUpSVAClient
- user must be a valid Google email address under the same domain as the Service Account client.
- calendarID can be retrived from Google Calendar => Calendar settings.
Exp:
calendarID:= "c_d3e80545746779e9e3957248314356fe4d9e1dcc27c2259b8c029ad5ee6f9cdf@group.calendar.google.com"
userEmail:= "[email protected]"
success := calendarClient.SubscribeUserToCalendar(calendarID, userEmail)
func (c *CalendarClient) UnsubscribeUserFromCalendar(user string, calendarID string) bool
UnsubscribeUserFromCalendar unsubscribes user from a dynamic Google Calendar. Call the function with the Calendar Client created from func SetUpSVAClient.
If a user is not subscribed to the calendar in the first place, no operation would be done and the function would return true.
- user must be a a valid google email address under the same domain of the Service Account client.
- calendarID can be retrived from Google Calendar => Calendar settings.
Exp:
calendarID:= "c_d3e80545746779e9e3957248314356fe4d9e1dcc27c2259b8c029ad5ee6f9cdf@group.calendar.google.com"
userEmail:= "[email protected]"
success := calendarClient.UnsubscribeUserFromCalendar(calendarID, userEmail)
func (c *CalendarClient) SubscribeGroupToCalendar(calendarID string, userlist []string) bool
SubscribeUserToCalendar subscribes a group of users to a dynamic Google Calendar. Call the function with the Calendar Client created from func SetUpSVAClient.
This function produces two lists of user cases documenting the outcomes of user calendar subscription attempts.
- A list of successful subscription cases
- A list of failed subscription attempts
- calendarID can be retrived from Google Calendar => Calendar settings.
- userlist is list of valid Google emails of targeted users, should be in email format
Exp:
userlist:= ["[email protected]", "[email protected]", "[email protected]"]
calendarID:= "c_d3e80545746779e9e3957248314356fe4d9e1dcc27c2259b8c029ad5ee6f9cdf@group.calendar.google.com"
result := calendarClient.SubscribeGroupToCalendar(calendarID, userlist)
func (c *CalendarClient) UnsubscribeGroupFromCalendar(calendarID string, userlist []string) bool
UnsubscribeGroupFromCalendar unsubscribes a groups of users from a dynamic Google Calendar. Call the function with the Calendar Client created from func SetUpSVAClient.
If a user is not subscribed to the calendar in the first place, no operation would be done and the user would be added to successful user list.
This function produces two lists of user cases documenting the outcomes of user calendar subscription attempts.
- A list of successful unsubscription cases
- A list of failed unsubscription attempts
- calendarID can be retrived from Google Calendar => Calendar settings.
- userlist is list of valid Google emails of targeted users, should be in email format
Exp:
userlist:= ["[email protected]", "[email protected]", "[email protected]"]
calendarID:= "c_d3e80545746779e9e3957248314356fe4d9e1dcc27c2259b8c029ad5ee6f9cdf@group.calendar.google.com"
result := calendarClient.UnsubscribeGroupFromCalendar(calendarID, userlist)
- Log in to Google Cloud Console
- Select the corresponding Google Cloud Project
- If the project isn't created yet, Click New Project to create one.
- Project existed but doesn't show up.
- Search it in the search bar
- Couldn't find it. Refer to Google API Common Question
- Open the console left side menu => APIs & Services => Enable APIs & Services
- Enable required API
- Google Calender API (scope: https://www.googleapis.com/auth/calendar)
- Open the console left side menu => IAM & Admin => Service accounts
- Select the corresponding Service Account
- If the Service Account isn't created yet, Click + create a service account to create one.
- For service account: Choose the role Project > owner.
- Follow the official guide from Google to delegate domain-wide authority to the service account
- For this project, in OAuth scopes, enter: https://www.googleapis.com/auth/calendar
- Click into the service account => KEYS => Add KEY => Create new key
- Create a private key for "calendar service test", choose "JSON" for the key type
- Save the JSON file in the project directory for Service Account authentification.
- Log in to Google Cloud Console
- Select the corresponding Google Cloud Project
- Open the console left side menu => IAM & Admin => IAM
- Use Grant Access to add member and Remove Access to delete member
- New Principle: email address of the user
- Roles: assign desired role
Q: Admin has created the project but I couldn't find it in my console.
- A: Check your access role to the project. Make sure you are a owner.
- To change access role to owner, ask the current owner of the project to add you to the project.
- For details, refer to section Manage project members.
Error: Subscription: Authentification passed, couldn't fetch token
-
Example error message:
Authentification sets up Subscribe User To Calendar... Calendar ID: c_b14ec2724ff6a0cac6558c74bebb0e8b986b4084cf059c68bb2d5b9b070d71ed@group.calendar.google.com user: [email protected] Post "https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json&prettyPrint=false": oauth2: cannot fetch token: 401 Unauthorized Response: { "error": "unauthorized_client", "error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested." } panic: Post "https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json&prettyPrint=false": oauth2: cannot fetch token: 401 Unauthorized Response: { "error": "unauthorized_client", "error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested." }
-
Solution:
- Make sure to enable all APIs included in the scope.
- Check access to "calendarservices.SetUpSVAAuth()", make sure all required scope has enabled
To enable API: Open the console left side menu => APIs & Services => Enable APIs & Services
- Check access to "calendarservices.SetUpSVAAuth()", make sure all required scope has enabled
- Make sure to enable all APIs included in the scope.
For assistance, please contact us at [email protected].
Last updated by Seoli Kim on July 26, 2024.