0

我正在尝试从 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>
4

2 回答 2

1

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

问题出在特殊字符“~”上

使用以下 URl 通过将 '~' 替换为 '%7E' 来获取配置文件详细信息

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

于 2013-02-12T07:48:20.827 回答
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 配置文件字段

于 2012-12-05T10:11:03.857 回答