function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sujesh RamachandranSujesh Ramachandran 

Issue : Getting Basic Profile Info through Linkedin API from salesforce

I am trying to get basic profile info from Linkedin API in APEX, i got authorization token and secret token but not able to retrieve basic profile using the token. please find below the code :

 

public void getLinkedinData(){
    List<TestLinkedin__c> tok = [select unAuth_Key__c,unAuth_Token__c from TestLinkedin__c where name='AuthKey' limit 1];  
    oauth_token = tok[0].unAuth_Token__c;
    oauth_token_secret = tok[0].unAuth_Key__c;        

    unAuthorisedToken =  tok[0].unAuth_Token__c;
    unAuthorisedKey = tok[0].unAuth_Key__c;  

    nonceValue  = generateNonce(); 
    timeStamp = generateTimeStamp();     

    string TripItAccessDataUri='https://api.linkedin.com/v1/people/~/format/json';

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setMethod('GET');
    req.setEndpoint(TripItAccessDataUri);
    final String oAUth_BASE_signature = SignatureBaseStringForAccessData(TripItAccessDataUri,unAuthorisedToken,unAuthorisedKey,nonceValue,timeStamp); 
    String signature = SignatureForAccessData(oAUth_BASE_signature ,consumerKey_Secret+'&'+unAuthorisedKey);
    req.setHeader('Authorization','OAuth realm=\"https://api.linkedin.com/\",oauth_consumer_key=\"'+oauth_consumer_key+'\",oauth_token=\"'+unAuthorisedToken+'\",oauth_nonce=\"'+nonceValue+'\",oauth_token_secret=\"'+unAuthorisedKey+'\",oauth_signature_method=\"'+oauth_signature_method+'\",oauth_timestamp=\"'+timeStamp +'\",oauth_version=\"'+oauth_version+'\",oauth_signature=\"'+signature+'\"');  
    reqstr =oAUth_BASE_signature ;
    HttpResponse res = h.send(req);
    resData =  res.getBody();
    system.debug('&&&&&&&&&&&&&&&& Respose' + resData );
}   

public String generateNonce(){
    String nonce;
    String finalNonce ;  
    nonce = String.valueOf(Math.abs(Math.random())); 
    finalNonce = nonce.substring(nonce.indexOf('.')+1,nonce.length());
    System.debug('+++++++++Nonce '+ finalNonce );
    return finalNonce ;
} 
public long generateTimeStamp(){
    long millis = (long) System.currentTimeMillis() ;
    long sysTime = (long) millis / 1000;
    System.debug('+++++++++++++TimeStamp'+ sysTime );
    return sysTime ;
}

public String SignatureBaseStringForAccessData(String TripItAccessDataUri,String unAuthorisedToken,String unAuthorisedKey,String  nonceValue ,Long timeStamp) {
    String httpmethod = 'GET';
    String oAUTH_PARA='oauth_consumer_key='+oauth_consumer_key+'&oauth_nonce='+nonceValue +'&oauth_signature_method='+oauth_signature_method+'&oauth_timestamp='+timeStamp+'&oauth_token='+unAuthorisedToken+'&oauth_token_secret='+unAuthorisedKey+'&oauth_version='+oauth_version+'';
    String BASE_STRING = httpmethod+'&'+EncodingUtil.urlEncode(TripItAccessDataUri,'UTF-8')+'&'+EncodingUtil.urlEncode(oAUTH_PARA,'UTF-8');
    return BASE_STRING;
}

//Signature Generator for Data
public String SignatureForAccessData(String Signature_Base_Value,String Key){
    Blob mac = Crypto.generateMac('HmacSHA1',Blob.valueOf(Signature_Base_Value),Blob.valueOf(Key));
    String macUrl = EncodingUtil.urlEncode(EncodingUtil.base64Encode(mac),'UTF-8');
    System.debug('++++++++Final Signature Is '+macUrl );   
    return macUrl;
}

Response body:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<error>
    <status>401</status>
    <timestamp>1354429511382</timestamp>
    <request-id>GDGNWB2M1V</request-id>
    <error-code>0</error-code>
    <message>[unauthorized].OAU:tylg6udljqe0|e7820b33-6acc-4752-b014-f54d505cf‌8a0|*01|*01:1354376252:vufYE2Zeg9Dg/R1YXO21cRlfmPA=</message>
</error>
Jay EcklesJay Eckles

Some things I don't understand about your code:

 

What is the type for oauth_token and oauth_token_secret?

Where are consumerKey_secret,  oauth_signature_method, and oauth_version defined and set?

Have you tried constructing your various header values as strings before the setHeader method call and printing those values to the debug log?  That would allow you to see if there's something unexpected happening, and it would allow you to test those same values outside APEX.

Sujesh RamachandranSujesh Ramachandran

Finally got the answer.... :)

issue was with the special character '~'

Use the below URl to fetch the profile details by repacing '~' with '%7E'

string AccessDataUri='https://api.linkedin.com/v1/people/%7E';

akash dalvi 12akash dalvi 12

Hi,

I also have the same requirement. I want to fetch the basic profile information of my contacts (standard contact obj in salesforce) fromLinkedIn profile using LinkedIn API in apex

Can you please provide my step by step guide for this implementation?

Thanks