- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 12-08-2021 02:06 AM
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"
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.
1.4 From the API Library search for Google Calendar and click enable
1.5 Now you need to create credentials and configure the consent screen. For that first step is to click create credentials
1.6 First Configure the Consent screen by clicking configure consent screen
1.7 Provide the app name "My Calendar" and set user type as 'external' and click create
1.8 Now click create credentials and select OAuth client ID
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
Step 2: Register the application with the instance to participate in OAuth authorization.
2.1 Create an application registry entry by clicking new
2.2 Select "Connect to a third party OAuth Provider"
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
2.4 Once you save the application registry a default "OAuth Entity Profiles" will be created
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
2.6 Now open the OAuth Entity profile created in step 2.4 and add the scopes created in step 2.5
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)
3.2 Create a new HTTP method "create". This will trigger API call to create a meeting event
3.3 Create the below HTTP headers for the HTTP method
3.4 Click auto generate variables
3.5 Repeat the procedure to create HTTP methods for update and delete as below
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
<div ng-init="c.sync_calendar()">
</div>
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"
4.2 Create a new resource from the related list "Scripted rest resource" with relative path "/oauth_redirect"
4.3 Update the redirect URL on OAuth Application registry
https://*******.service-now.com/api/*******/google_calendar/oauth_redirect
4.4 Update the redirect URL on your client credential in the GCP
https://*******.service-now.com/api/*******/google_calendar/oauth_redirect
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</br><h2>Meeting Details:</h2><h3>Platform Link: <a href=https://******.service-now.com/****>Click Here</a></h5></br><h3>Video Conference Join Link: <a href=https://us02web.zoom.us/j/********>Click Here</a></h3></br><h2>Meeting Agenda Items:</h2><ul><li>Showcase new technical functionality</li><li>Provide constructive feedback</li><li>Update weekly technical plan</li><li>Weekly highlighted issues</li></ul></br>If you have any further questions regarding the above meeting please contact me.</br></br><h3>Best regards,</h3></br><h3>Pragya Agarwal</h3></br>",
"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();