我正在尝试从 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-f54d505cf8a0|*01|*01:1354376252:vufYE2Zeg9Dg/R1YXO21cRlfmPA=</message>
</error>