+ Start a Discussion
Nguyễn Văn TiếnNguyễn Văn Tiến 

Get Basic Profile from LinkedIn using REST API

Hi everyone,

Right now, I have connected my salesforce user to LinkedIn using OAuth. But now I want to get the basic information from LinkedIn user using Apex.
Linked In has provided 2 pages for this but to be honest, I'm clueless about OAuth or Rest API.

I have try to authenticate to OAuth using the code below but it always said "No_Oauth_State: State was not valid linkedin". Can anyone help me to fix this bug and get the profile data using REST API after that:

public class LinkedInAuth {
    
    
    public string theCode {get;set;}
    public string theTokenResponse {get;set;}
    
    /*******************************************
    *
    *   Get Authorization Code
    *
    *******************************************/
    
    public pagereference auth_Step_1(){
        
        String auth_url = 'https://www.linkedin.com/oauth/v2/authorization';
        String params =  
            '?response_type=code' +
            '&client_id=*************' +
            '&redirect_uri=https://login.salesforce.com/services/authcallback/00D7F000000opivUAA/DemoSync' + 
            //                            '&prompt=consent' + 
            '&scope=' + encodingUtil.URLEncode('r_basicprofile w_share','UTF-8') +
            '&state=DCEeFWf45A53sdfKef424';
        pageReference pr = New PageReference(auth_url + params);            
        return pr;
    }
    
    /*******************************************
    *
    *   Get Access Token
    *
    *******************************************/
    
    
    public pagereference auth_Step_2(){
        
        if(apexPages.currentPage().getParameters().get('state') != 'DCEeFWf45A53sdfKef424')
            return null;
        
        theCode = apexPages.currentPage().getParameters().get('code');
        
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        
        String auth_url = 'https://www.linkedin.com/oauth/v2/accessToken';
        String params =  
            '?code=' + apexPages.currentPage().getParameters().get('code') +
            '&grant_type=authorization_code' + 
            '&client_id=************' +
            '&client_secret=***************' + 
            '&redirect_uri=https://login.salesforce.com/services/authcallback/00D7F000000opivUAA/DemoSync';
        
        req.setMethod('POST');
        req.setEndpoint(auth_url + params);
        req.setHeader('Content-Type','application/x-www-form-urlencoded');
        req.setHeader('Content-Length',string.valueOf(req.getBody().length()));
        
        HTTPResponse resp = http.send(req);
        
        
        theTokenResponse = resp.getBody();
        
        return null;
        
    }
}
NagendraNagendra (Salesforce Developers) 
Hi,

Please refer below post Getting Basic Profile Info through Linkedin API from salesforce. Hope this helps you!

Please mark it as solved if this helps you so that it will make available for other as a proper solution.

Regards,
Nagendra
 
Nguyễn Văn TiếnNguyễn Văn Tiến
Thanks Nagendra,

The link https://www.sundoginteractive.com/blog/integrating-salesforce-and-linkedin really helps. But I still don't understand how to get the linkedInKey thing. What is it? How to get it?

And the link https://developer.salesforce.com/forums/?id=906F000000092EqIAI is a mess. I hardly understand his code.
Divya GhodasaraDivya Ghodasara
Hi Nagendra,
I have created a visualforce page and controller for LinkedIn integration. I have got basic information for the profile. Now I will get post information in visualforce page. Is this possible?