# README
calendarmod
Table of Contents
Overview
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
- Subscirbe user to a dynamic Google calendar
- Subscirbe a group of users to a dynamic Google calendar
Version Update
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
Getting started
Prerequisite
- 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.
Installation
- move to the directory of your Go Project in the terminal
- run the command
go get github.com/oss/calendarmod
Usage
Subscribe User to a Calendar in Go Project
-
Import the package
import ( "github.com/oss/calendarmod" )
-
Create a authentification client based on the service account
client := calendarmod.SetUpSVAClient(serviceAccountJSON, true)
-
Subscribe the user to the calendars
success := client.SubscribeUserToCalendar(calendarid, userEmail)
Subscribe Group to a Calendar in Go Project
-
Import the package
import ( "github.com/oss/calendarmod" )
-
Create a authentification client based on the service account
client := calendarmod.SetUpSVAClient(serviceAccountJSON, true)
-
Subscribe the user to the calendars
success := client.SubscribeGroupToCalendar(calendarid, userlist)
Functions
func SetUpSVAClient
func SetUpSVAClient(serviceAccountJSON []byte, useCalendar bool) *CalendarClient
SetUpSVAClient initialize authentification client with service account and returns CalendarClient with context and config.
- useCalendar should always to be set to true to use Calendar services.
- To authentificate, 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)
}
auth := calendarmod.SetUpSVAAuth(serviceAccountJSON, true)
func SubscribeUserToCalendar
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 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 := calendarmod.SubscribeUserToCalendar(calendarID, userEmail)
func UnsubscribeUserFromCalendar
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 := calendarmod.UnsubscribeUserFromCalendar(calendarID, userEmail)
func SubscribeGroupToCalendar
func (c *CalendarClient) SubscribeGroupToCalendar(calendarID string, userlist []string) bool
SubscribeUserToCalendar subscribes a groups 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 UnsubscribeGroupFromCalendar
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)
Google Cloud API
Set up Google Cloud API Access
- Log in to Google Cloud Console 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 Cloud 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)s
- 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.
- Delegating domain-wide authority to the service account
- For OAuth scopes, enter: ttps://www.googleapis.com/auth/calendar
- Click into the service account => KEYS => Add KEY => Create new key
- Create private key for "calendar service test", choose "JSON" for key type
- Save the JSON file in the projecr directory for authentification to the Service Account.
Common Questions
About Google Cloud API
-
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. If need to change access role, refer to Manage project members or change project ownership
Troubleshoot
-
Error: Subscription: Authentification passed, couldn't fetch token
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
- Make sure to enable all APIs included in the scope.
Support
For assistance, please contact us at [email protected].
Last updated by Seoli Kim on July 23, 2024.