Categorygithub.com/oss/calendarmod
modulepackage
0.1.18
Repository: https://github.com/oss/calendarmod.git
Documentation: pkg.go.dev

# README

calendarmod

Table of Contents

  1. Overview
    1. Version Update
  2. Getting started
  3. Usage
    1. Subscribe User to a Calendar in Go Project
    2. Subscribe Group to a Calendar in Go Project
  4. Functions
    1. SetUpSVAAuth
    2. SubscribeUserToCalendar
  5. Google Cloud API
    1. Set up Google Cloud API Access
  6. Common Questions
  7. Troubleshoot
  8. Support


Overview

calendarmod is a Go Package designed to facilitate Google Calendar services for Rutgers University. This module includes the following core functionality:

  1. Authenticate Client for Google API using a service account
  2. Subscirbe user to a dynamic Google calendar
  3. 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
    1. 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.
  • Set up a Go project to utilize this package.

Installation

  1. move to the directory of your Go Project in the terminal
  2. run the command
    go get github.com/oss/calendarmod
    



Usage

Subscribe User to a Calendar in Go Project

  1. Import the package

    import (
        "github.com/oss/calendarmod"
    )
    
  2. Create a authentification client based on the service account

    client := calendarmod.SetUpSVAClient(serviceAccountJSON, true)
    
  3. Subscribe the user to the calendars

    success := client.SubscribeUserToCalendar(calendarid, userEmail)
    

Subscribe Group to a Calendar in Go Project

  1. Import the package

    import (
        "github.com/oss/calendarmod"
    )
    
  2. Create a authentification client based on the service account

    client := calendarmod.SetUpSVAClient(serviceAccountJSON, true)
    
  3. 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.

  1. A list of successful subscription cases
  2. 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.

  1. A list of successful unsubscription cases
  2. 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

  1. Log in to Google Cloud Console Google Cloud Console
  2. Select the corresponding Google Cloud Project
    1. If the project isn't created yet, Click New Project to create one.
    2. Project existed but doesn't show up.
      1. Search it in the search bar
      2. Couldn't find it. Refer to Google Cloud API Common Question
  3. Open the console left side menu => APIs & Services => Enable APIs & Services
  4. Enable required API
  5. Open the console left side menu => IAM & Admin => Service accounts
  6. 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.
  7. Delegating domain-wide authority to the service account
  8. Click into the service account => KEYS => Add KEY => Create new key
  9. 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




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:

    1. 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


Support

For assistance, please contact us at [email protected].

Last updated by Seoli Kim on July 23, 2024.

# Functions

Get CalendarListEntry from Calendar ID.
Initialize authentification client with service account to access Google API @return {*CalendarClient} @param {bool} useCalendar- true if need to use Google Calendar API, should always be true to use subscription service.
Initiate a service access with service account's impersonation of the user This service is for all functionalities to Google Calendar API @return {*calendar.Service} if success @param {string} user - impersanation of the user.

# Structs

Client based on Service Account to connect to Google API.
No description provided by the author
No description provided by the author