Introduction To Named Credential For Google Drive Authorization In Salesforce

Introduction To Named Credential For Google Drive Authorization In Salesforce

Sharing is caring!

Are you looking for an effective solution to expose the URLs to an external system for integration? Are you concerned about sharing the Username / Password to connect to an external network and hide it?  This is where Named Credentials in Salesforce comes into the picture.

Salesforce has introduced Named Credentials in the Salesforce Spring’15 release to make it easier for app developers to integrate into external web services using callouts. In this blog post, we will help you understand what named credentials are and how to configure, maintain, and implement Named Credentials to connect two Salesforce Org.

What Is A Named Credential?

A named credential is a configuration feature that takes the guesswork out of the authentication processes. This out-of-the-box feature enables Salesforce to handle some or most authentications within the organization or other Salesforce org. Named credentials are used to store a variety of callout configurations that includes:-
1. Callout certificates
2. Settings for authentication
3. Authentication protocol, if it’s required or needed.
4. HTTP endpoints

Benefits If Using Named Credentials In Salesforce:

  • Named credentials specify the URL of a callout endpoint.
  • Named credentials also add the authentication parameters in one definition.
  • You don’t need to create the remote site settings for sites defined in the named credential.
  • Named credentials can make a call out to an endpoint without supplying user credentials.
  • By separating the endpoint URL and authentication from the callout definition, named credentials make callouts easier to maintain. For example, if an endpoint URL changes, you update only the named credential. All callouts that reference the named credential simply continue to work.

Why Do We Use Named Credential For Google Drive Authentication?

There are ways to authenticate with google drive:
1. Using User Id & Password: 
For this, first, we hit a ‘https://accounts.google.com/o/oauth2/auth?’ endpoint with client id and client secret that returns refresh redirect Uri. Then we hit another endpoint, ‘https://accounts.google.com/o/oauth2/token’ using redirect Uri that returns Refresh token only in first callout and access token in every callout.

2. Using Refresh token: Get refresh token by following the above steps and store refresh token.
Use the refresh token in this ‘https://accounts.google.com/o/oauth2/token’ endpoint callout to get the access token for authorization.

To skip the above steps, we use a named credential that requires a one-time setup and automatically authenticates for all the future google drive callouts.

Steps To Setup Named Credential In Salesforce Org

Step 1: Create a Google project which will provide the client id and client secret.
-> Login into the google account in which you want to integrate with salesforce.
->https://console.developers.google.com/projectselector/apis/library?supportedpurview=project%20
-> Click on Select a project dropdown and click on create a new project.
-> Login in to your salesforce org.
-> Click on Setup –> Auth. Provider → Click on New and select Google from the dropdown and click on next.
-> FiIl the following details as shown in the below screenshot:

-> Click on Save and Copy the URL and navigate ‘Web Client 1’ OAuth client created in Google project and paste in on ‘Authorized redirect URIs.’

Step 2: Create Named Credential.
-> Search Named Credential in Quick find box and then click on New.
-> Fill in the details as shown in the below screenshot.

-> Click on save, and it will be navigated to the Google login page.
-> Enter your Google account credentials and click on the ‘Allow Access’ button.
-> This will complete the Named credentials setup.

How Will We Use Named Credentials In Code?

I’ve simply used the named credentials to create a new folder inside a folder, 
as shown below code.

String boundary = '----------9889464542212';
String delimiter = '\r\n--' + boundary + '\r\n';
String close_delim = '\r\n--' + boundary + '--';
String body = delimiter + 'Content-Type: application/json\r\n\r\n' + '{ "name" : 
"Test2", "parents": ["194yQc9FJU7NYNjgWsuFKKP02ENNUGfT-"]' + ', "mimeType" :
"application/vnd.google-apps.folder" }' + close_delim;

Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Google_Drive_Credential/files?uploadType=multipart');
req.setHeader('Content-Type', 'multipart/mixed; boundary="' + boundary + '"');
req.setHeader('Content-length', String.valueOf(body.length()));
req.setBody(body);
req.setMethod('POST');
req.setTimeout(60 * 1000);
HttpResponse res = http.send(req);
String resp = res.getBody();
System.debug('the resp 1'+ resp  );

So this way, we can use the named credential and remove the callout required for authentication.

Processing authentication in your code can be more complex and less secure, especially for OAuth implementations. Meanwhile, Salesforce manages the authentication for any callouts with a named credential specified as the callout endpoint. Named credentials are easier to manage because you can update the endpoint URL, and any callouts referencing the named credential will continue to work.

Are you looking for the best, result-oriented Salesforce consulting services to add value to your business by generating revenue and bringing value to the clients? Our certified CRM experts at Cloud Analogy have deep expertise in diverse Salesforce projects that can help your teams achieve organizational goals.

Akshay

Akshay Dhiman

Chief Technical Officer
Akshay Dhiman, the CTO of Cloud Analogy, has been a standout and successful Salesforce Platform Developer for years. He has a rich experience in Salesforce Integration, JavaScript, APEX, VisualForce, Force.com Sites, Batch Processing, Lightning, PHP, C++, Java, NodeJs, ReactJs, Angular 8, GraphQL, React Native, Web Technology, and jQuery.

Hire the best Salesforce Development Company. Choose certified Salesforce Developers from Cloud Analogy now.

× How can I help you?