Google Calendar

Google API

Google APIs are application programming interfaces developed by Google. They allow communication with various Google services and their integration into other applications.
Examples include APIs for Google Search, Gmail, Google Maps, and Google Calendar1. These APIs enable developers to leverage Google’s powerful services within their own applications,
enhancing functionality and user experience.

PURPOSE OF GOOGLE APIS:

  • Integration: Seamlessly integrate Google services into third-party applications.
  • Functionality: Extend the capabilities of existing services (e.g., adding Google Maps to a website).
  • Data Access: Access and manipulate user data with proper permissions (e.g., uploading file to drive, accessing calendar events).
  • Automation: Automate tasks and workflows using Google’s services (e.g., scheduling events, uploading files to the drive).

Google Calendar

Google Calendar is a time-management and scheduling service developed by Google. It allows users to create, edit, and share events and schedules. Google Calendar is available on the web and as mobile apps for Android and iOS

PURPOSE OF GOOGLE CALENDAR:

  • Scheduling: Create and manage events, appointments, and reminders.
  • Sharing: Share calendars with others to coordinate schedules and plan events.
  • Notifications: Receive reminders and notifications for upcoming events.
  • Integration: Integrate with other Google services (e.g., Gmail) to automatically add events from emails.
  • Accessibility: Access your calendar from any device with internet connectivity.

Google Drive

Google Drive is a cloud-based storage solution developed by Google. It allows users to store files online and access them from any device with an internet connection.

PURPOSE OF GOOGLE DRIVE:

  • Storage: Save various file types (documents, photos, videos, etc.) securely in the cloud.
  • Accessibility: Access files from anywhere, on any device.
  • Collaboration: Share files and collaborate in real-time with others.
  • Backup: Keep a backup of important files to prevent data loss.
  • Integration: Seamlessly integrate with other Google services like Gmail and Google Calendar.

WHY USE GOOGLE DRIVE IN GOOGLE CALENDAR?

In the context of your project, Google Drive is used to manage file attachments for calendar events. By integrating Google Drive with Google Calendar, can streamline the process of sharing and managing event-related documents, making your scheduling and collaboration efforts more efficient. Here’s why it’s beneficial:

  • Centralized Storage: Store all event-related files in one place, making it easy to manage and access them.
  • Sharing and Permissions: Easily share files with event attendees and control their access permissions (e.g., view, edit).
  • Integration: Attach files directly to calendar events, ensuring all necessary documents are available to attendees.
  • Collaboration: Collaborate on attached files in real-time, enhancing productivity and coordination.

STEPS TO CREATE A GOOGLE CALENDAR EVENT

CREATE A NEW PROJECT IN GOOGLE CLOUD CONSOLE

  • Go to the Google Cloud Console. (https://console.cloud.google.com/)
  • Click on the project dropdown and select “New Project.”
  • Enter the project name and click “Create.”

ENABLE GOOGLE CALENDAR API AND GOOGLE DRIVE API

  • In the Google Cloud Console, navigate to “APIs & Services” > “Library.”
  • Search for “Google Calendar API” and click “Enable.”
  • Similarly, search for “Google Drive API” and click “Enable.”

CREATE A SERVICE ACCOUNT AND GENERATE A PRIVATE KEY

  • Go to “APIs & Services” > “Credentials.”
  • Click on “Create Credentials” and select “Service Account.”

  • Fill in the required details and click “Create.”
  • After creating the service account, click on the service account address in Service accounts panel

  • Click on the key tab: “ADD KEY “> “Create new Key “>Key type (Json) name and click “Create.” it to generate a private key (JSON format).it auto downloaded to our folder. Use this Private key for get access token from the google Api (referred as after)

ASSIGN PERMISSIONS IN GOOGLE ADMIN CONSOLE

  • Navigate to “Security” > “Access and data Control” > “API Controls” > “Manage Domain Wide Delegation.”
  • Click Add new to Add the client ID of your service account In Client ID Column and grant the necessary scopes (e.g., https://www.googleapis.com/auth/calendar, https://www.googleapis.com/auth/drive) in OAuth scopes Column. and click “AUTHORIZE.”

NOTES: Admin must have a billing account, then only we can add external attendees. If admin does not have a billing account, we cannot invite external attendees, but we can invite attendees within the organization. Also, users must have the Google Workspace Business Starter License. If users do not have the license, we cannot access the Google Calendar and Google Drive applications.

CREATE JSON FOR THE EVENT

The JSON structure for creating the events in Google Calendar using the Google API's http post method.

JSON Properties:

{

  "CreateGoogleEvent": {

    "calendarId": "myappflowuser@myappflow.com",

    "colorId": null,

    "CreateGoogleDriveFile": {

      "fileDetails": [

        {

          "fileContent": "\\\\hc1-fs1\\\\WebStorage\\\\CadebillCloud\\WebHooks\\\\GoogleCalendar\\\\Google Calendar.txt",

          "fileName": "GoogleCalendar.txt"

        }

      ],

      "folderDetails": {

        "mimeType": "application/vnd.google-apps.folder",

        "name": "Google WorkSpace"

      },

      "folderpermission": {

        "emailAddress": "guna25643@gmail.com",

        "role": "writer",

        "type": "user"

      }

    },

    "dontshare": 1,

    "events": {

      "attendees": [

        {

          "email": "guna25643@gmail.com"

        }

      ],

      "description": "Learning API Advance Process",

      "end": {

        "dateTime": "2024-07-05T16:15:00.000",

        "timeZone": "America/New_York"

      },

      "location": "1010 Linden Lake Plaza Manassas VA 20109",

      "recurrence": {

        "RRULE": null

      },

      "start": {

        "dateTime": "2024-07-05T00:00:00.000",

        "timeZone": "America/New_York"

      },

      "summary": "New API calendar Testing"

    },

    "hasAttachments": true

  }

}

 

Explanation of JSON Properties:

CreateGoogleEvent: 

Main object for creating the event.

calendarId: *

The ID of the calendar where the event will be created. I have to use the login email address of Google Cloud Console, where the project was created. (e.g., myappflowuser@myappflow.com)

colorId: 

Optional property to set the color of the event.represents a specific color(eg:5)

CreateGoogleDriveFile: 

Object for creating a file in Google Drive.

fileDetails:

 Array object of file details to be attached. (add multiple files)

fileContent: *

Path to the file content. (e.g., \\hc1-fs1\\WebStorage\\CadebillCloud\WebHooks\\GoogleCalendar\\Google Calendar.txt)

fileName: *

 Name of the file. (e.g., Google Calendar)

folderDetails: 

Details object of the folder where the file will be stored.

mimeType: *

 MIME type of the folder. (e.g., application/vnd.google-apps.folder)

name: *

 Name of the folder. (e.g. Google WorkSpace)

folderpermission: 

Permissions object for the folder.

emailAddress: *

Email address of the user to share the folder with. (e.g. guna25643@gmail.com)

role: *

Role of the user (e.g., writer).

type: *

Type of user (e.g., user).

dontshare:

 Flag to indicate whether to access the file for the attendees with or without asking permission to the Calendar owner. (e.g. true)

events:

 Details object of the event.

attendees: 

Array object of attendees’ email addresses. (add multiple attendees)

emailaddresses: *

email addresses for the attendees (e.g. guna25643@gmail.com)

description: 

Description of the event. (e.g. Submit your expense report for June by the end of the day)

end:

 End time object of the event.

dateTime: *

End date and time. (e.g. Sat Jul 13 2024 21:00:00 GMT-0400)

timeZone: *

Time zone of the end time. (e.g. America/New_York)

location:

 Location of the event. (e.g. 800 Howard St., San Francisco, CA 94103)

recurrence: 

Recurrence rule object for the event.

RRULE: 

A recurrence rule in Google Calendar specifies how often an event repeats. This can be set to daily, weekly, monthly, yearly, or even custom intervals. (e.g. RRULE: FREQ=WEEKLY; INTERVAL=2; BYDAY=TU, TH)

start: 

Start time object of the event.

dateTime: *

Start date and time. (e.g. Sat Jul 12 2024 21:00:00 GMT-0400)

timeZone: *

Time zone of the start time. (e.g. America/New_York)

summary: 

Summary or title of the event. (e.g. Google Meeting)

hasAttachments: 

Flag to indicate whether the event has attachments. (e.g. true)

AUTHORIZATION AND AUTHENTICATION

An access token is a string of characters that represents the authorization granted to a client application to access specific resources on behalf of a user or service account.

  • Authentication: Access tokens verify the identity of the client application and ensure that it has the right permissions to access the requested resource.
  • Authorization: They specify what actions the client application is allowed to perform on the resource (e.g., read, write, delete).
  • Security: Access tokens help secure APIs by ensuring that only authorized clients can access them. They are usually short-lived, reducing the risk of misuse if intercepted.

GET ACCESS TOKEN

Components:

  • Private Key: A private key is a cryptographic key used to authenticate the service account. It is stored in the JSON key file you downloaded.
  • gsuituser: The G Suite user on whose behalf the service account is acting.
  • Client Email: The email address of the service account, found in the JSON key file.
  • Scopes: Add necessary scopes as our needs.

The above components are In the JSON key file you downloaded. JSON file where get from after create service account in google cloud console we generated the key after that we have the JSON file in our Downloads

Steps:

  • Read the private key file: Load the JSON key file to extract the private key and client email.
  • Parse the JSON: Extract the private key and client email from the JSON.
  • Initialize the service account credential: Use the extracted information to create a ServiceAccountCredential object.
  • Request the access token: Use the credential to request an access token. Using tokenRequestUri: "https://oauth2.googleapis.com/token"

After getting access token response from the credential used it to the Event Create.

CREATE EVENT

Create a new event in the Google Calendar using the access token.

Components:

  • Access Token: A token that grants access to the Google Calendar API.
  • Authorization Header: A header in the HTTP request that includes the "Bearer" access token to authenticate the request.

Steps:

  • Construct the URI: Build the URI for creating an event in the specified calendar.
  • Set the authorization header: Include the access token in the Authorization header of the HTTP request.
  • Post the event data: Send the event data to the Google Calendar API.Using createEventUri: https://www.googleapis.com/calendar/v3/calendars/{googlecreateevent.CalendarId}/events?supportsAttachments={hasAttachments};