LinkedIn Integration with Apex Salesforce Using Named Credentials

by Himanshu Goyal
linkedin-integration-with-apex-salesforce-using-named-credentials-techdicer

Hello friends, today we will discuss LinkedIn Integration with Apex Salesforce Using Named Credentials.

To create a connection between Salesforce and LinkedIn to get the LinkedIn data into the salesforce we need to create an API on the LinkedIn platform and then with the Rest API Callouts we can access the info for this there are two approaches one is to go with the custom code for authentication and access token and other is to use the named credential and Auth. provider in salesforce.

Also, check this: What Is Named Credentials, AuthProvider & Use In Apex Salesforce

Highlight Points :

  1. No need to create records for remote settings.
  2. Access token manage by Name credintial so we don’t need to call extra callout for access token.
  3. It has OAuth 2.0 Authentication.
  4. Get person info in this REST API

Keys Steps

Step 1:

Now first we need to create an API on the LinkedIn developer platform with the following steps.

First, go to LinkedIn Developer platform https://www.linkedin.com/developers/

Sign In with your account and click on create App.

linkedin-integration-with-apex-salesforce-using-named-credentials-create-app-techdicer
LinkedIn-integration-with-apex-salesforce-using-named-credentials-create-app-techdicer

Key Details

  1. APP Name – provide any name.
  2. LinkedIn Page – Here you need to provide a public company page’s URL that you have created if not then you can create a new page as well and provide that’s URL for the same.
  3. App Logo – You need to Provide a logo for the App.

then click on create and get the app.

Step 2: Now go to the Auth tab from here you will get the Client Id and Client Secret of your App. Which will be used to create the Authprovider in salesforce.

Check this to create Authprovider: What Is Named Credentials, AuthProvider & Use In Apex Salesforce

LinkedIn-integration-with-apex-salesforce-using-named-credentials-Setup-app-techdicer

So by creating auth provider salesforce will provide us a Callback URL. We will put that URL into the Redirect URL field under the auth tab. Redirect URL we will get when you create Auth Provider in Salesforce.

Step 3: Now go to the Products Tab Here you’ll see different products these are basically the different levels of permission and Access.

LinkedIn-integration-with-apex-salesforce-using-named-credentials-Setup-app-techdicer

here are three different categories for now I’ll select the Sign In with LinkedIn.

we can see the different Endpoints for different types of info by clicking the view endpoints link.

use the named credential and Auth. provider in salesforce.
LinkedIn-integration-with-apex-salesforce-using-named-credential-setup-app-permission-techdicer

for now, I’m going to use “/me” and GET method and r_liteprofile scope as the Endpoint.

Code Samples :

To get the info of my LinkedIn profile into salesforce.

Here is the code Sample to make a call out to the newly created LinkedIn API.

LinkedinIntegration.cls:

public class LinkedinIntegration{
	
	public static void LinkedinAuth(){
		//new request
		HttpRequest request = new HttpRequest();
		request.setEndpoint('callout:linkedInNamedCredential/'+'me');
		request.setMethod('GET');
		//initialize Http
		Http http = new Http();
	  //get response for the request
	   HttpResponse response = http.send(request);
	   System.debug('Statuscode>>'+response.getStatusCode());
	   System.debug('Statuscode>>'+response.getBody());
	   //process the successful response
	   if (response.getStatusCode() == 200){
			//initializing a Map<String,Object> to get Callout response
			Map<String, Object> serializeResponseBody = new Map<String, Object>();
			serializeResponseBody = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
			System.debug('serializeResponseBody>>'+JSON.serializePretty(serializeResponseBody));
		}
	}
}

Output

What’s your Reaction?
+1
4
+1
1
+1
0
+1
0
+1
1
+1
3

You may also like

Leave a Comment