Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    • Enhancing Performance with File Compression in Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Tuesday, May 20
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»Integrate Google API with Salesforce

    Integrate Google API with Salesforce

    Dhanik Lal SahniBy Dhanik Lal SahniFebruary 26, 2020Updated:January 15, 202516 Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Integrating Google API with Salesforce
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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+'?'+
                'client_id='+key+
                '&response_type=code'+
                '&scope='+scope+
                '&redirect_uri='+uri+
                '&access_type=offline';
            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.

    References:

    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

    apex google google and Salesforce google api Google API in Salesforce integrate salesforce with google lightning salesforce salesforce google api Salesforcre and Google Salesforcre and Google Integration
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleSalesforce Spring ’20 release features are now available to customers globally
    Next Article Salesforce to Acquire Vlocity for $1.33B
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Dynamic Queueable Chaining in Salesforce Apex

    April 21, 2025
    View 16 Comments

    16 Comments

    1. John Lewis on 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?

      Reply
      • Dhanik Lal Sahni on 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
        http://salesforcecodex.com/

        Reply
        • Surya Kumari M on 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

          Reply
          • Dhanik Lal Sahni on July 9, 2020 1:22 pm

            Hello Surya,

            As per our online session, this issue is resolved.

            Thank You,
            Dhanik

            Reply
            • sri on 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.

              Thanks
              Sri

            • Dhanik Lal Sahni on 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,
              Dhanik

    2. John Lewis on 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.

      Regards

      John Lewis

      Reply
      • Dhanik Lal Sahni on May 13, 2020 1:28 pm

        It looks like proper analysis of error.

        Thank You,
        Dhanik

        Reply
      • Sri on 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.

        Thanks
        Sri

        Reply
        • Dhanik Lal Sahni on 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
          Dhanik

          Reply
    3. Pingback: Logo Recognition using Google Cloud Vision in Salesforce | SalesforceCodex

    4. Pingback: Object Identification using Google Cloud API in Salesforce | SalesforceCodex

    5. Hithesh on 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?

      Reply
      • Dhanik Lal Sahni on 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,
        Dhanik

        Reply
    6. Puneet on 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

      Reply
      • Dhanik Lal Sahni on June 1, 2024 12:43 pm

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

        Thank You,
        Dhanik

        Reply
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.