Salesforce Integration With Google



Salesforce Integration With Google

This blog will provide the information to integrate Google drive with the simplest way without any complex apex code We have used Auth. Provider and Named credentials to handle authentication for Google Account. That's store the ‘Google Secret ID’ and ‘Google Client ID’ and Named Credential utilizes the Auth. Provider while making callout in custom Apex code. Advantage using Auth. Provider and Named Credential is that we do not need to handle authentication each and every time we make callout. Authentication will be done once we create the Named Credential associated with the Auth. Provider.
Let’s do implement and enjoy this magic!!

Step to Implements for whole Process

Creating a Google Project for App Config

  • Login into the google account for integrate with salesforce
  • Navigate to https://console.developers.google.com/projectselector/apis/library?supportedpurview=project
  • From the project drop-down create a Create a new project.
  • Provide the Project name like ‘Salesforce Project’ , ‘Salesforce Connect’ etc
  • Create Credentials button, and select ‘OAuth consent screen’ tab
  • After saving click on ‘Credentials’ button and select ‘OAuth Client ID’/li>
  • ‘Application Type’ should be Web Application and click on ‘Create’ button.
  • Note: We will be update ‘Authorized redirect URIs’ when Auth. Provider is setup
  • Auth. Provider is used client Id and client secret.
Once a Google App is configured, we will get a client Id and client secret for Auth. Provider.

Create a Auth. Provider is Salesforce

  • Log into your Salesforce org for integrating with Google Account
  • Setup > Quick Search Box > Auth. Provider
  • Auth. Providers > ‘New’ button
  • Fill information accordingly below values in the input fields
  • Provider Type— Open ID Connect
  • Name— ‘GoogleDrive’ (Any Value)
  • URL Suffix— auto populated as per Name
  • Consumer Key—Provide By Google Project as client_Id
  • Consumer Secret—Provide By Google Project as client_secret
  • Authorize Endpoint URL—https://accounts.google.com/o/oauth2/auth
  • Token Endpoint URL—https://accounts.google.com/o/oauth2/token
  • Default Scopes—openid
Once Auth Provider is ‘Save’ button ‘Callback URL’ field under Salesforce Configuration automatically populated.
Copy the URL and navigate ‘Web Client 1’ OAuth client created in Google project and paste in on ‘Authorized redirect URIs’

Create Named Credential in Salesforce

  • Create Named Credential in a same Org (Auth Provider org)
  • Setup > Quick Search Box > Named Credential
  • Named Credential > ‘New’ button
  • Fill information accordingly below values in the input fields
  • Label— GoogleDrive
  • Name— auto populated as per Name
  • URL— https://www.googleapis.com/drive/v2/files
  • Identity Type—Named Principal
  • Authentication Protocol—OAuth 2.0
  • Authenticator Provider—Auth. Provider selected above one
  • Scope—https://www.googleapis.com/auth/drive
Once "Named Credential" is save its redirect to Google account for authenticate. We can utilize the named credentials directly in the callout.

Below is the code snippet which is user "Named Credential" and get response from Google Account.



public class GoogleDriveIntegration{    
    public static void getGoogleDriveFiles() {
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint('callout:GoogleDrive/drive/v3/files/'); 
        Http http = new Http();
        HttpResponse res = http.send(req);
        system.debug(res.getBody());
    }
}



Happy Sharing...

Everyone has their own favorites, so please feel free to share yours in the comments below!

Comments

Post a Comment

Popular posts from this blog

Salesforce LWC : Compact Layout on Hover

Salesforce LWC With Third Party JS : D3

User Data Privacy