从salesforce通过Linkedin API获取基本配置文件信息

时间:2012-12-01 10:03:41

标签: api rest salesforce linkedin apex-code

我正在尝试从APEX中的Linkedin API获取基本配置文件信息,我获得了授权令牌和秘密令牌,但无法使用令牌检索基本配置文件。请在下面找到代码:

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;
}

回复机构:

<?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>

2 个答案:

答案 0 :(得分:1)

终于得到了答案......

问题是特殊字符'〜'

使用以下URl通过将'〜'与'%7E'重新混合来获取配置文件详细信息

字符串LinkedInAccessDataUri ='https://api.linkedin.com/v1/people/%7E';

答案 1 :(得分:0)

您需要在身份验证期间请求r_basicprofile的权限:

休息样本请求:

https://api.linkedin.com/uas/oauth/requestToken?scope=r_basicprofile

Javascript示例请求:

<script type="text/javascript" src="http://platform.linkedin.com/in.js">
    api_key: your_api_key
    scope: r_basicprofile
</script>

以下是所有字段的列表及其所需权限:

Linkedin API profile fields