Home SalesforceApex Integrate Google API with Salesforce

Integrate Google API with Salesforce

by Dhanik Lal Sahni
Integrating Google API with Salesforce

This blog will give step by step process of getting a refresh token and access token from Google API. This access token and refresh token can be used to Integrate Google API with Salesforce.

  1. Create a user in Google Console
  2. Create Apex/Lightning component for authorization

Create a user in Google Console

      1. Create a user in Google Cloud at https://console.developers.google.com . We can also use any existing Gmail user.
      2. Create a project in Google ConsoleGoogle New Project -Integrate Google API with Salesforce
      3. Once the project is created, go to the API library at https://console.developers.google.com/apis/library?project=stellar-day-269401. Select any Google API which needs to be integrated.
      4. Select API and enable it for use.
      5. Create a Credential for API to be used. This credential will be used in Apex. Credentials will be created from https://console.developers.google.com/apis/credentials/wizard?project=stellar-day-269401 after the selection of the project.Integrate Google API with Salesforce
      6. Authorize domain in OAuth Consent Screen. Use the Lightning URL of your org without HTTPS. You can also use the lightning community URL here.Integrate Google API with Salesforce
      7. We need to set a redirect URL which will be a lighting component URL for authenticating and getting access tokens.
        Integrate Salesforce with Google API
      8. Add Google API https://www.googleapis.com in the Remote Site setting.

Create Apex/Lightning component for authorization

1. Apex:

Now we have set up all pre-requisite in the Google console. Let us create a Lightning component to be used for authentication.

The below code is used to get the Google Authentication screen. Use any Gmail account for this.

public static String createAuthURL() {
        String key = EncodingUtil.urlEncode(key,'UTF-8');
        String uri = EncodingUtil.urlEncode(redirect_uri,'UTF-8');
        String authuri = '';
        authuri = authUrl+'?'+
        return authuri;

getAccessToken is used to get access tokens and refresh tokens from Google for specific APIs.

2. Create custom metadata to store the access token

Create a custom metadata type to store access tokens for subsequent use. You can use custom settings, platform caching or custom objects based on the use case. For this blog, I have used custom metadata.

Create Custom metadata and add filed AccessToken in that to store value.

3. Lightning Component:

We have to create a page where we can redirect users after authentication. We can create a Lightning component tab or a Lighting community page for this.

This lightning component will show a Google authentication screen and get an access token with a button click.

We can use this component for all Google API authentication with salesforce. Only change the scope and redirect the URL in GoogleAuthService.


Google API Library

Related Posts:

Object Identification using Google Cloud Vision in Salesforce

Logo Recognition using Google Cloud Vision in Salesforce

Extract Text From Images using Google Cloud Vision

Google FitBit Integration with Salesforce

 Audio to Text by Google Speech API in Salesforce Lightning

You may also like


John Lewis May 11, 2020 - 6:31 pm

Hello, I’ve implemented your example on my developer Org, but I’m finding a problem when the browser is re-directed back to the page of the Lightning Component. Specifically, the ‘code’ parameter appears to be automatically removed from the Query String of the URL and is, therefore, not accessible to the logic in the doInit() function.

Is this related to the issue described here? https://releasenotes.docs.salesforce.com/en-us/spring19/release-notes/rn_forcecom_general_namespace_prefix_cruc_reminder.htm

Do you know any work-around?

Dhanik Lal Sahni May 11, 2020 - 8:38 pm

Hello John,

I have tested example in CRM as well as Community. It is working well. Have you setup proper url in redirect_uri of GoogleAuthService.apxc?

Let us connect over meeting to check your issue.

Thank You,
Dhanik Sahni

Surya Kumari M July 6, 2020 - 3:43 am

Hello Dhanik,
I am also facing the same issue. Could you please help us to resolve this. Let me know how can i reach out to you show u my issue.
We are implementing POC to get fitness information into salesforce health cloud. Kindly respond and help on this

Dhanik Lal Sahni July 9, 2020 - 1:22 pm

Hello Surya,

As per our online session, this issue is resolved.

Thank You,

sri September 12, 2021 - 10:07 am

HI Dhanik,

Can you please explain how this issue is resolved? I followed all the steps specified and unable to fetch ‘Code’ value from lightning URL.


Dhanik Lal Sahni September 15, 2021 - 12:43 pm

Hello Sri,
I have checked again and able to get code in lighting component. I have tried today and it is working fine in one of my lighting community.
Google Code

Are you giving consent for additional access to your Google Account?

Thank You,

John Lewis May 12, 2020 - 3:18 pm

Dhanik Sahni,

yes, what I had done is created a Lightning Component Tab which means that the component is accessible by the URL https://xxxx.lightning.force.com/lightning/n/Google_Auth, where ‘xxxx’ is the domain name of my Org. The ‘redirect_uri’ value in class ‘GoogleAuthService’ was then set to ‘https://xxxx.lightning.force.com/lightning/n/Google_Auth’.

With this setup, the component appears to work as intended: it first re-directs the browser to https://accounts.google.com where I choose my Google account from a list, then I confirm the choices by clicking button ‘Allow’ and Google’s page re-directs my browser back to https://xxxx.lightning.force.com/lightning/n/Google_Auth with query string parameters including code=yyyy and scope=zzzz. However salesforce.com then appears to instantly remove the query string parameters so they are not received by the Aura component’s JS controller method ‘doInit()’. Consequently the authorization code is NOT received and can’t be passed to Apex method ‘getAccessToken()’.

(I also found that with the lightning:usUrlAddressable interface implemented, the component can be accessed via its own URL: https://xxxx.lightning.force.com/lightning/cmp/c__GoogleAuthCmp, but the same issue occurs for query string parameters which are not prefixed with ‘c__’).

This issue described at https://releasenotes.docs.salesforce.com/en-us/spring19/release-notes/rn_forcecom_general_namespace_prefix_cruc_reminder.htm appears to imply that query string parameters without a namespace prefix (‘c__’ for the default namespace) are automatically removed to prevent conflicts so is it likely that this is the reason the Aura component is not receiving the ‘code’ and ‘scope’ parameters passed by Google?

I HAVE found that by using a Visualforce page instead of an Aura component, the ‘code’ and ‘scope’ parameters ARE received, however.


John Lewis

Dhanik Lal Sahni May 13, 2020 - 1:28 pm

It looks like proper analysis of error.

Thank You,

Sri September 12, 2021 - 10:08 am

Hi John,

Can you please explain how did you achieve the functionality in lightning? It is working good in visualforce but still unable to fetch ‘Code’ value in lightning component.


Dhanik Lal Sahni September 15, 2021 - 12:48 pm

Hello Sri,
I have checked once again and it is giving code in lightning component. Are you giving proper consent when google asking for approving it after selecting/entering google account detail.

Let me know, if you want to connect and resolve your issue.

Thank You

Hithesh July 26, 2021 - 8:31 pm

Hi Dhanik,
I am also facing the same issue as John Lewis faced. After authenticating from google, salesforce is truncating the code and because of it we are not able to get it in UI. I tried with LWC,AURA and visualforce page. I am getting the same in all. Not sure any settings to do in salesforce side. Can you please help?

Dhanik Lal Sahni December 7, 2021 - 1:02 pm

Hey Hithesh,

This will work in the community portal very well. Check this in the portal once.

Thank You,

Puneet May 29, 2024 - 12:59 pm

Sir I am also facing a some issue like FATAL_ERROR System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://accounts.google.com/o/oauth2/token how to resolve it

Dhanik Lal Sahni June 1, 2024 - 12:43 pm

Hello Puneet,
Please add url in Remote Site Setting. This will resolve your issue.

Thank You,


Leave a Comment