1. Create developer account in linkedin Account.
2. Create connected application in linkedin
3. develop the code in salesforce environment
i) create a custom object to store the token information
ii) add remote site settings
iii) test the integration
Create developer account in linkedin Account:
Type https://developer.linkedin.com in URL and click enter you will redirect to below page
Note: Create a visualforce page for redirect URI and save in connected app like https://munna550-dev-ed--c.ap5.visual.force.com/apex/LinkedinResponse
<apex:page controller="Rest_OAuth_LinkedIN" action="{!readcode}">
{!code}
</apex:page>
Creating a custom object in salesforce and clientID, clientsecret and redirect uri in that object as shown below.
Request for code from Linkedin:
public class Rest_OAuth_LinkedIN {
public string accestoken {set; get;}
public string code {set; get;}
public Serverdata__c data {set; get;}
public Rest_OAuth_LinkedIN(){
data = [select Client_ID__c, Client_Secret__c, Redirect_URL__c from Serverdata__c where Name = 'LinkedIN'];
}
/* Request for code
* endpoint url : https://www.linkedin.com/oauth/v2/authorization
* Method : GET
* parameters : response_type = code
* : client_id = connected app clientid
* : redirect_uri = connected app redirecturi
* : state = any random string
*/
public void readcode(){
code=Apexpages.currentPage().getparameters().get('code');
}
public PageReference requestcode(){
string endpoint='https://www.linkedin.com/oauth/v2/authorization?';
endpoint=endpoint+'response_type=code';
endpoint=endpoint+'&client_id='+data.Client_ID__c;
endpoint=endpoint+'&redirect_uri='+data.Redirect_URL__c;
endpoint=endpoint+'&state=shaik';
PageReference p = new PageReference(endpoint);
return p;
}
}
Create a visualforce home page to send a request through button to linkedin, we will get code as shown below:
<apex:page controller="Rest_OAuth_LinkedIN">
<apex:form>
<apex:commandButton value="code" action="{!requestcode}"/>
</apex:form>
</apex:page>
Request for Access Token:
public class Rest_OAuth_LinkedIN {
public string accesstoken {set; get;}
public string code {set; get;}
public Serverdata__c data {set; get;}
public Rest_OAuth_LinkedIN(){
data = [select Client_ID__c, Client_Secret__c, Redirect_URL__c from Serverdata__c where Name = 'LinkedIN'];
}
/* Request for code
* endpoint url : https://www.linkedin.com/oauth/v2/authorization
* Method : GET
* parameters : response_type = code
* : client_id = connected app clientid
* : redirect_uri = connected app redirecturi
* : state = any random string
*/
public void readcode(){
code=Apexpages.currentPage().getparameters().get('code');
}
public PageReference requestcode(){
string endpoint='https://www.linkedin.com/oauth/v2/authorization?';
endpoint=endpoint+'response_type=code';
endpoint=endpoint+'&client_id='+data.Client_ID__c;
endpoint=endpoint+'&redirect_uri='+data.Redirect_URL__c;
endpoint=endpoint+'&state=shaik';
PageReference p = new PageReference(endpoint);
return p;
}
/* Request for acesstoken
* endpoint url : https://www.linkedin.com/oauth/v2/accessToken
* Method : POST
* Parameters : grant_type=authorization_code
* : code=The authorization code you received from above
* : redirect_uri = from connected app
* : client_id = from connected app
* : client_secret = from connected app
*/
public void requestToken(){
string endpoint='https://www.linkedin.com/oauth/v2/accessToken';
string body='grant_type=authorization_code';
body=body+'&code='+code;
body=body+'&redirect_uri='+data.Redirect_URL__c;
body=body+'&client_id='+data.Client_ID__c;
body=body+'&client_secret='+data.Client_Secret__c;
Http p = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(endpoint);
request.setMethod('POST');
request.setBody(body);
HttpResponse response = p.send(request);
string result = response.getBody();
System.JSONParser JP = JSON.createParser(result);
while(JP.nextToken()!=NUll){
if(JP.getText()=='access_token'){
JP.nextToken();
accesstoken = JP.getText();
}
}
}
}
<apex:page controller="Rest_OAuth_LinkedIN" action="{!readcode}">
{!code}
<apex:form>
<apex:commandButton value="gettoken" action="{!requestToken}"/>
</apex:form>
{!accesstoken}
</apex:page>
Once we get access token, we supposed to get the details of the profile from LinkedIN:
public class Rest_OAuth_LinkedIN {
public string accesstoken {set; get;}
public string code {set; get;}
public string result {set; get;}
public Serverdata__c data {set; get;}
public Rest_OAuth_LinkedIN(){
data = [select Client_ID__c, Client_Secret__c, Redirect_URL__c from Serverdata__c where Name = 'LinkedIN'];
}
/* Request for code
* endpoint url : https://www.linkedin.com/oauth/v2/authorization
* Method : GET
* parameters : response_type = code
* : client_id = connected app clientid
* : redirect_uri = connected app redirecturi
* : state = any random string
*/
public void readcode(){
code=Apexpages.currentPage().getparameters().get('code');
}
public PageReference requestcode(){
string endpoint='https://www.linkedin.com/oauth/v2/authorization?';
endpoint=endpoint+'response_type=code';
endpoint=endpoint+'&client_id='+data.Client_ID__c;
endpoint=endpoint+'&redirect_uri='+data.Redirect_URL__c;
endpoint=endpoint+'&state=shaik';
PageReference p = new PageReference(endpoint);
return p;
}
/* Request for acesstoken
* endpoint url : https://www.linkedin.com/oauth/v2/accessToken
* Method : POST
* Parameters : grant_type=authorization_code
* : code=The authorization code you received from above
* : redirect_uri = from connected app
* : client_id = from connected app
* : client_secret = from connected app
*/
public void requestToken(){
string endpoint='https://www.linkedin.com/oauth/v2/accessToken';
string body='grant_type=authorization_code';
body=body+'&code='+code;
body=body+'&redirect_uri='+data.Redirect_URL__c;
body=body+'&client_id='+data.Client_ID__c;
body=body+'&client_secret='+data.Client_Secret__c;
Http p = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(endpoint);
request.setMethod('POST');
request.setBody(body);
HttpResponse response = p.send(request);
string result = response.getBody();
System.JSONParser JP = JSON.createParser(result);
while(JP.nextToken()!=NUll){
if(JP.getText()=='access_token'){
JP.nextToken();
accesstoken = JP.getText();
}
}
getprofile();
}
public void getprofile(){
string endpoint = 'https://api.linkedin.com/vi/people/~?format=json';
Http p = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(endpoint);
request.setMethod('GET');
request.setHeader('Authorization','Bearer '+ accesstoken);
HttpResponse response = p.send(request);
result = response.getBody();
}
}
<apex:page controller="Rest_OAuth_LinkedIN" action="{!readcode}">
{!code}
<apex:form>
<apex:commandButton value="gettoken" action="{!requestToken}"/>
</apex:form>
{!result}
</apex:page>
2. Create connected application in linkedin
3. develop the code in salesforce environment
i) create a custom object to store the token information
ii) add remote site settings
iii) test the integration
Create developer account in linkedin Account:
Type https://developer.linkedin.com in URL and click enter you will redirect to below page
Note: Create a visualforce page for redirect URI and save in connected app like https://munna550-dev-ed--c.ap5.visual.force.com/apex/LinkedinResponse
<apex:page controller="Rest_OAuth_LinkedIN" action="{!readcode}">
{!code}
</apex:page>
Creating a custom object in salesforce and clientID, clientsecret and redirect uri in that object as shown below.
Request for code from Linkedin:
public class Rest_OAuth_LinkedIN {
public string accestoken {set; get;}
public string code {set; get;}
public Serverdata__c data {set; get;}
public Rest_OAuth_LinkedIN(){
data = [select Client_ID__c, Client_Secret__c, Redirect_URL__c from Serverdata__c where Name = 'LinkedIN'];
}
/* Request for code
* endpoint url : https://www.linkedin.com/oauth/v2/authorization
* Method : GET
* parameters : response_type = code
* : client_id = connected app clientid
* : redirect_uri = connected app redirecturi
* : state = any random string
*/
public void readcode(){
code=Apexpages.currentPage().getparameters().get('code');
}
public PageReference requestcode(){
string endpoint='https://www.linkedin.com/oauth/v2/authorization?';
endpoint=endpoint+'response_type=code';
endpoint=endpoint+'&client_id='+data.Client_ID__c;
endpoint=endpoint+'&redirect_uri='+data.Redirect_URL__c;
endpoint=endpoint+'&state=shaik';
PageReference p = new PageReference(endpoint);
return p;
}
}
Create a visualforce home page to send a request through button to linkedin, we will get code as shown below:
<apex:page controller="Rest_OAuth_LinkedIN">
<apex:form>
<apex:commandButton value="code" action="{!requestcode}"/>
</apex:form>
</apex:page>
Request for Access Token:
public class Rest_OAuth_LinkedIN {
public string accesstoken {set; get;}
public string code {set; get;}
public Serverdata__c data {set; get;}
public Rest_OAuth_LinkedIN(){
data = [select Client_ID__c, Client_Secret__c, Redirect_URL__c from Serverdata__c where Name = 'LinkedIN'];
}
/* Request for code
* endpoint url : https://www.linkedin.com/oauth/v2/authorization
* Method : GET
* parameters : response_type = code
* : client_id = connected app clientid
* : redirect_uri = connected app redirecturi
* : state = any random string
*/
public void readcode(){
code=Apexpages.currentPage().getparameters().get('code');
}
public PageReference requestcode(){
string endpoint='https://www.linkedin.com/oauth/v2/authorization?';
endpoint=endpoint+'response_type=code';
endpoint=endpoint+'&client_id='+data.Client_ID__c;
endpoint=endpoint+'&redirect_uri='+data.Redirect_URL__c;
endpoint=endpoint+'&state=shaik';
PageReference p = new PageReference(endpoint);
return p;
}
/* Request for acesstoken
* endpoint url : https://www.linkedin.com/oauth/v2/accessToken
* Method : POST
* Parameters : grant_type=authorization_code
* : code=The authorization code you received from above
* : redirect_uri = from connected app
* : client_id = from connected app
* : client_secret = from connected app
*/
public void requestToken(){
string endpoint='https://www.linkedin.com/oauth/v2/accessToken';
string body='grant_type=authorization_code';
body=body+'&code='+code;
body=body+'&redirect_uri='+data.Redirect_URL__c;
body=body+'&client_id='+data.Client_ID__c;
body=body+'&client_secret='+data.Client_Secret__c;
Http p = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(endpoint);
request.setMethod('POST');
request.setBody(body);
HttpResponse response = p.send(request);
string result = response.getBody();
System.JSONParser JP = JSON.createParser(result);
while(JP.nextToken()!=NUll){
if(JP.getText()=='access_token'){
JP.nextToken();
accesstoken = JP.getText();
}
}
}
}
<apex:page controller="Rest_OAuth_LinkedIN" action="{!readcode}">
{!code}
<apex:form>
<apex:commandButton value="gettoken" action="{!requestToken}"/>
</apex:form>
{!accesstoken}
</apex:page>
Once we get access token, we supposed to get the details of the profile from LinkedIN:
public class Rest_OAuth_LinkedIN {
public string accesstoken {set; get;}
public string code {set; get;}
public string result {set; get;}
public Serverdata__c data {set; get;}
public Rest_OAuth_LinkedIN(){
data = [select Client_ID__c, Client_Secret__c, Redirect_URL__c from Serverdata__c where Name = 'LinkedIN'];
}
/* Request for code
* endpoint url : https://www.linkedin.com/oauth/v2/authorization
* Method : GET
* parameters : response_type = code
* : client_id = connected app clientid
* : redirect_uri = connected app redirecturi
* : state = any random string
*/
public void readcode(){
code=Apexpages.currentPage().getparameters().get('code');
}
public PageReference requestcode(){
string endpoint='https://www.linkedin.com/oauth/v2/authorization?';
endpoint=endpoint+'response_type=code';
endpoint=endpoint+'&client_id='+data.Client_ID__c;
endpoint=endpoint+'&redirect_uri='+data.Redirect_URL__c;
endpoint=endpoint+'&state=shaik';
PageReference p = new PageReference(endpoint);
return p;
}
/* Request for acesstoken
* endpoint url : https://www.linkedin.com/oauth/v2/accessToken
* Method : POST
* Parameters : grant_type=authorization_code
* : code=The authorization code you received from above
* : redirect_uri = from connected app
* : client_id = from connected app
* : client_secret = from connected app
*/
public void requestToken(){
string endpoint='https://www.linkedin.com/oauth/v2/accessToken';
string body='grant_type=authorization_code';
body=body+'&code='+code;
body=body+'&redirect_uri='+data.Redirect_URL__c;
body=body+'&client_id='+data.Client_ID__c;
body=body+'&client_secret='+data.Client_Secret__c;
Http p = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(endpoint);
request.setMethod('POST');
request.setBody(body);
HttpResponse response = p.send(request);
string result = response.getBody();
System.JSONParser JP = JSON.createParser(result);
while(JP.nextToken()!=NUll){
if(JP.getText()=='access_token'){
JP.nextToken();
accesstoken = JP.getText();
}
}
getprofile();
}
public void getprofile(){
string endpoint = 'https://api.linkedin.com/vi/people/~?format=json';
Http p = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(endpoint);
request.setMethod('GET');
request.setHeader('Authorization','Bearer '+ accesstoken);
HttpResponse response = p.send(request);
result = response.getBody();
}
}
<apex:page controller="Rest_OAuth_LinkedIN" action="{!readcode}">
{!code}
<apex:form>
<apex:commandButton value="gettoken" action="{!requestToken}"/>
</apex:form>
{!result}
</apex:page>
No comments:
Post a Comment