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:

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
redirect_uri=[YOUR VF PAGE URL]&
  • 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
//Header Content
Content-Type: application/x-www-form-urlencoded
//Body Content
code=[_Authentication Code generated from VF page_]&
redirect_uri=VF Page URL&

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
//Header Content
Content-Type: application/x-www-form-urlencoded
//Body Content

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
  • 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.setEndpoint('[API KEY]);
    request.setHeader('Authorization', 'Bearer ACCESS TOKEN Stored in Custom Setting');
    request.setHeader('Accept', 'application/json');
    response = http.send(request);


Popular Salesforce Blogs