+ Start a Discussion
souvik9086souvik9086 

LinkedIN Salesforce Integration

Hello everyone,

 

We are integrating linkedIN in salesforce. We want to retrieve linkedIN profile information within salesforce.

For that we needed to callout from salesforce. We are following the Authentication steps as follows:

 

https://developer.linkedin.com/documents/authentication

 

We successfully reached upto Step 2, point a). But the problem arised in the HTTP POST Request in the point b) in order to retrieve the access token from the response. We are getting the authorization code correct and we are sending that code in the point b) but,  We are repetately getting the following error on that point

 

{"error":"invalid_request","error_description":"missing required parameters, includes an invalid parameter value, parameter more then once. : Unable to retrieve access token : authorization code not found"}

#Note: The authorization code which is coming in the URL is displaying correctly in the debug.

 

If anyone has any suggestion, kindly reply.

 

Thanks in Advance

Best Answer chosen by Admin (Salesforce Developers) 
SarfarajSarfaraj

Hi Souvik

 

This works for me,

 

<!---page : testlogin->
<apex:page controller="testlogin" > <apex:form > <apex:outputLink value="{!authorizationCodeURI}">Click Me</apex:outputLink> </apex:form> <div>body : {!body}</div> </apex:page>

 

public with sharing class testlogin {
    final String redirect_uri = 'https%3A//ap1.visual.force.com/apex/testlogin';
    final String scope = 'r_fullprofile%20r_emailaddress%20r_network';
    final String state = 'asjasjdj32423j324k23j2323jk23j';

    final String client_id = '<Place holder>';
    final String client_secret = '<Place holder>';

    String code;
    String accessTokenURI;
    
    public String body{get;set;}
    public String authorizationCodeURI{get;set;}
    
    public testlogin()
    {
        code = ApexPages.currentPage().getParameters().get('code');
        authorizationCodeURI = 'https://www.linkedin.com/uas/oauth2/authorization?response_type=code'+
                               '&client_id='+client_id+
                               '&state='+state+
                               '&redirect_uri='+redirect_uri;//+
      //                         '&scope=' + scope;
        if(code <> null)
        {
            accessTokenURI = 'https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code'+
                                           '&code=' + code +
                                           '&redirect_uri='+redirect_uri+
                                           '&client_id=' + client_id +
                                           '&client_secret=' + client_secret;
            HttpRequest req = new HttpRequest();
            req.setEndpoint(accessTokenURI);
            req.setMethod('POST');
            Http http = new Http();
            HTTPResponse res = http.send(req);
            body = res.getBody();
        }
    }
}

#Note:

1. redirect_uri should be same for both 'a', 'b'.

2. code from 'a' expires shortly. So you have to execute 'b' asap.

 

All Answers

SarfarajSarfaraj

Hi Souvik

 

This works for me,

 

<!---page : testlogin->
<apex:page controller="testlogin" > <apex:form > <apex:outputLink value="{!authorizationCodeURI}">Click Me</apex:outputLink> </apex:form> <div>body : {!body}</div> </apex:page>

 

public with sharing class testlogin {
    final String redirect_uri = 'https%3A//ap1.visual.force.com/apex/testlogin';
    final String scope = 'r_fullprofile%20r_emailaddress%20r_network';
    final String state = 'asjasjdj32423j324k23j2323jk23j';

    final String client_id = '<Place holder>';
    final String client_secret = '<Place holder>';

    String code;
    String accessTokenURI;
    
    public String body{get;set;}
    public String authorizationCodeURI{get;set;}
    
    public testlogin()
    {
        code = ApexPages.currentPage().getParameters().get('code');
        authorizationCodeURI = 'https://www.linkedin.com/uas/oauth2/authorization?response_type=code'+
                               '&client_id='+client_id+
                               '&state='+state+
                               '&redirect_uri='+redirect_uri;//+
      //                         '&scope=' + scope;
        if(code <> null)
        {
            accessTokenURI = 'https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code'+
                                           '&code=' + code +
                                           '&redirect_uri='+redirect_uri+
                                           '&client_id=' + client_id +
                                           '&client_secret=' + client_secret;
            HttpRequest req = new HttpRequest();
            req.setEndpoint(accessTokenURI);
            req.setMethod('POST');
            Http http = new Http();
            HTTPResponse res = http.send(req);
            body = res.getBody();
        }
    }
}

#Note:

1. redirect_uri should be same for both 'a', 'b'.

2. code from 'a' expires shortly. So you have to execute 'b' asap.

 

This was selected as the best answer
souvik9086souvik9086

Hello Akram,

 

Thanks for your response..

 

We have tested like the same before, but not done everything inside constructor.

 

As per your recommendations we have changed like that, but still same error coming. Executing the authorization code in the same step. But still firing that auth code not found.

 

Strange !!

SarfarajSarfaraj

I was also getting similar error earlier. Are you using same redirect uri in both steps? The code returned from step a is dependent on the uri. Also the app you defined in linkedin should be compatible to the scope you are requesting. Make sure the scope you are sending is enabled for your app. If you are not passing any scope parameter, default is all. In that case everything should be enabled in your app. Please double check these requirements.

souvik9086souvik9086

Thanks Akram. It worked. :)

BrahmeswarBrahmeswar

I tryed this code but i am getting an error like bellow

 

"{"error":"invalid_request","error_description":"missing required parameters, includes an invalid parameter value, parameter more then once. : code"} "

 

please help me in this.Can u please tell me What do we need to give 'code' value in accessTokenURI?

 

thanks

Brahmeswar

SarfarajSarfaraj

Hi Brahmeswar

 

You dont need to provide code value. It comes as a response from linkedin.

BrahmeswarBrahmeswar

HI Akram,

 

Thanks for quick reply. Here i am preparing  accessTokenURI like bellow.

accessTokenURI = 'https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code'+
'&redirect_uri='+redirect_uri+
'&scope='+scope+
'&client_id=' +APiKey +
'&client_secret=' + gkmuEWjcKGRiYsgz';

 "

Plese check this and tell me is there any mistake in the URI preparation.

 

Thanks

Brahmeswar 

  

SarfarajSarfaraj

Sorry I misunderstood your earlier question. Yes you need to provide the value for code in the accessTokenURI. You need to retrieve it from the url parameters and append to the accessTokenURI. Like I did in my code. This line retrieved the parameter,

code = ApexPages.currentPage().getParameters().get('code');

 Please note the following,

1. Provide same redirect_uri in both steps.

2. Scope should be valid.

 

Please post your full code for further reference.

BrahmeswarBrahmeswar

Hi Akram,

 

My problem with code is resolved.Thanks for help

Now i am getting response