Integration of Salesforce with Google Calendar using APEX

Integration of Salesforce with Google Calendar using APEX

Salesforce Integration with Google Calendar REST API can also be done by using Apex Code.

The following steps show how to integrate Salesforce with Google Calendar REST API:

Advertisement

Step 1: - Create an App on Google Console

Follow the same steps as done in Method-1 > step-1.

Step 2: - Create a Visualforce Page

Since Google API uses OAuth 2.0 authentication so we have to first get an authentication code and then use this authentication code to get the access token and refresh token.

  • First, create a Visualforce page in your Salesforce org. Having two buttons let’s say the first get Auth code and another one is getting access token.
  • In the controller of the First button, set button action for running a URL which is https://accounts.google.com/o/oauth2/v2/auth?
scope=YOUR SCOPE&
access_type=offline&
include_granted_scopes=true&
response_type=code&
redirect_uri=[YOUR VF PAGE URL]&
client_id=client_id
  • After setting the authentication parameter and after successful authentication you will be able to get code from the URL. Write a logic to get this code and pass it to the apex controller of the second button.
  • Write a POST method for getting the access token and refresh token with the click of the second button.

dont miss out iconDon't forget to check out: Get Access Token From Google Calendar API Through Postman | Salesforce Developer Guide

POST /token HTTP/1.1
Host: oauth2.googleapis.com
//Header Content
Content-Type: application/x-www-form-urlencoded
//Body Content
code=[_Authentication Code generated from VF page_]&
client_id=your_client_id&
client_secret=your_client_secret&
redirect_uri=VF Page URL&
grant_type=authorization_code

Note:- In Content-Type: application/x-www-form-URL encoded pass body in string format.

Step 3: - Using Refresh Token to Get Access Token

When an access token is generated, it has some expiration means after some time (1hr or 1.5hr) it will not authenticate. Therefore, we need to update it.

To do so we have to create a batch apex for generating access tokens using refresh token and scheduled it to run in the background after every hour.

  • Create a List type custom setting in store two fields Refresh token and access token.
  • Create Batch Apex. In the start, method fetch the refresh token value and pass it to execute method.
  • In Execute Method Write a POST for getting Access token in exchange for refresh token.
POST /token HTTP/1.1
Host: oauth2.googleapis.com
//Header Content
Content-Type: application/x-www-form-urlencoded
//Body Content
client_id=your_client_id&
client_secret=your_client_secret&
refresh_token=refresh_token&
grant_type=refresh_token

Also, You have to write a logic to update Access Token in Custom Setting every time we run method of getting access token using refresh token.

Step 4: - Create API Key

Before making a callout, we need to set an API key. For this -

  • Go to your https://console.cloud.google.com/
  • In Credentials, go to CREATE CREDENTIAL > API Key
  • Your API Key will be created

dont miss out iconCheck out another amazing blog by Parvez here: RemoteAction Annotation In Apex | Salesforce Apex Developer Guide

We are going to use this API Key during our callout.

Create API KEY

Step 5: - Make Callout

global static void getCalendar() { 
    request = new HttpRequest();
    request.setMethod('GET');
    request.setEndpoint('https://www.googleapis.com/calendar/v3/calendars/primary?key=[API KEY]);
    request.setHeader('Authorization', 'Bearer ACCESS TOKEN Stored in Custom Setting');
    request.setHeader('Accept', 'application/json');
    response = http.send(request);
}

Responses

Popular Salesforce Blogs