logo

NJP

Synchronize meeting events from your custom calendar in ServiceNow to Google Calendar using a widget

Import · Dec 08, 2021 · article

Step 1: Register your app in GCP(Google cloud Platform) and obtain client ID and Client Secret

1.1 Login to console.developers.google.com with your existing google account.

1.2 Create a new Project "Google Calendar Integration"

image

image

1.3 Select the Project you have just created and now you need to enable the API's you want to use, in this case google calendar.

image

1.4 From the API Library search for Google Calendar and click enable

image

1.5 Now you need to create credentials and configure the consent screen. For that first step is to click create credentials

image

1.6 First Configure the Consent screen by clicking configure consent screen

image

1.7 Provide the app name "My Calendar" and set user type as 'external' and click create

image

image

1.8 Now click create credentials and select OAuth client ID

image

1.9 Select application type as "Web Application" and give a name for your credentials. Once saved your client ID and client secret will be generated

image

image

Step 2: Register the application with the instance to participate in OAuth authorization.

2.1 Create an application registry entry by clicking new

image

2.2 Select "Connect to a third party OAuth Provider"

image

2.3 Provide the below details

name =googleCalendar

Client Id and client Secret - received in step 1.9

Authorization URL - https://accounts.google.com/o/oauth2/auth

Token URL - https://oauth2.googleapis.com/token

Default grant type - authorization code

image

2.4 Once you save the application registry a default "OAuth Entity Profiles" will be created

image

2.5 You need to define the 'OAuth Entity Scopes' as described below based on the type of actions you are going to perform. For create/update/delete of an event the below two scopes are sufficient

Calendar : https://www.googleapis.com/auth/calendar

Event : https://www.googleapis.com/auth/calendar.events

image

2.6 Now open the OAuth Entity profile created in step 2.4 and add the scopes created in step 2.5

image

Step 3: We will create a REST message and its HTTP methods to create/update/delete an event

3.1 Create a new rest message as described below

name = GoogleCalendar

endpoint = https://www.googleapis.com/calendar/v3

OAuth Profile - googleCalendar default_profile (created in step 2.6)

image

3.2 Create a new HTTP method "create". This will trigger API call to create a meeting event

image

3.3 Create the below HTTP headers for the HTTP method

image

3.4 Click auto generate variables

image

3.5 Repeat the procedure to create HTTP methods for update and delete as below

imageimage

imageimage

Step 4: Create a widget "sync_calendar". This widget opens the OAuth screen from google in a popup and will ask you to select a google account and then asks for consent.

HTML Code

Client Script

api.controller=function($window,$uibModal,$scope) { /* widget controller */ var c = this; c.sync_calendar = function()

{

var windowLocation = $window.open('https://*****.service-now.com/oauth_initiator.do?oauth_requestor_context=sys_rest_message&oauth_requestor=***rest_mesaage_sys_id***&oauth_provider_profile=***oauth_entity_profile sys_id****&response_type=code','popup','width=600,height=600');

var timer = setInterval(checkWindow, 1000);

function checkWindow() {

var ur = windowLocation.location.href; if (ur.indexOf('code') != -1 && ur.indexOf("oauth_redirect") != -1) { clearInterval(timer); windowLocation.close();

$window.open(url,"_self");

}

}

};

};

Step 5: We will create a scripted rest API which will act as the redirect URL after OAuth code is obtained from google. The code to trigger rest API calls will be defined here

4.1 Create a scripted rest API with name "Google calendar"image

4.2 Create a new resource from the related list "Scripted rest resource" with relative path "/oauth_redirect"

image

4.3 Update the redirect URL on OAuth Application registry

https://*******.service-now.com/api/*******/google_calendar/oauth_redirect

image

4.4 Update the redirect URL on your client credential in the GCP

https://*******.service-now.com/api/*******/google_calendar/oauth_redirect

image

4.5 Define the script to obtain token and trigger the API calls in the scripted REST Resource

//Fetching the code from URL var authCode = request.queryParams.code;

var authorizationCode = authCode.toString();

//REQUESTING TOKEN FROM THE CODE var oAuthClient = new sn_auth.GlideOAuthClient(); var params = { 'grant_type': "authorization_code", 'redirect_uri': 'https://******.service-now.com/api/*********/google_calendar/oauth_redirect', 'scope': 'https://www.googleapis.com/auth/calendar', 'code': authorizationCode

};

var text = JSON.stringify(params);

var tokenResponse = oAuthClient.requestToken('googleCalendar', text); // Name of our application registry created in step 2.3

var token = tokenResponse.getToken();

//Create the body of your rest message create/update event

var session = gs.getSession();

var zoneName = session.getTimeZoneName();

var content = { "summary": "Technical review meeting", "location": "", "description": "You are requested to attend a meeting

Meeting Details:

Platform Link: Click Here


Video Conference Join Link: Click Here


Meeting Agenda Items:

  • Showcase new technical functionality
  • Provide constructive feedback
  • Update weekly technical plan
  • Weekly highlighted issues

If you have any further questions regarding the above meeting please contact me.

Best regards,


Pragya Agarwal


", "start": { "dateTime": "2021-12-10T22:00:00", "timeZone": "Asia/Kolkata" }, "end": { "dateTime": "2021-12-10T22:30:00", "timeZone": "Asia/Kolkata" }, "attendees": [ { "id": "dc9811601ba0*************", "email": "*****@gmail.com", "displayName": "Pragya Agarwal", "responseStatus": "needsAction", "comment": "" }, { "id": "a43f46cd1b68b0*******************", "email": "************@gmail.com", "displayName": "Manas Agarwal", "responseStatus": "accepted", "comment": "" } ]

}

//Triggering the create event API var r = new sn_ws.RESTMessageV2('******.GoogleCalendar', 'create event'); r.setRequestHeader("Authorization","Bearer "+token.getAccessToken()); r.setRequestBody(JSON.stringify(content)); var response1 = r.execute(); var responseBody = JSON.parse(response1.getBody());

var httpStatus = response1.getStatusCode();

//Triggering the update event API

var r1 = new sn_ws.RESTMessageV2('*****.GoogleCalendar', 'Update'); r1.setRequestHeader("Authorization","Bearer "+token.getAccessToken()); r1.setStringParameterNoEscape("eventId",google_meeting_identifier); r1.setRequestBody(JSON.stringify(content)); var response2 = r1.execute(); var responseBody1 = response2.getBody();

var httpStatus1 = response2.getStatusCode();

//Trigger delete API call var r2 = new sn_ws.RESTMessageV2('****.GoogleCalendar', 'delete'); r2.setRequestHeader("Authorization","Bearer "+token.getAccessToken()); r2.setStringParameterNoEscape("eventId",google_meeting_identifier); var response3 = r2.execute(); var responseBody2 = response3.getBody();

var httpStatus2 = response3.getStatusCode();

image

View original source

https://www.servicenow.com/community/now-platform-articles/synchronize-meeting-events-from-your-custom-calendar-in/ta-p/2320701