2012-04-12 97 views
1

有一些問題想換我的REST OAuth憑證JSAPI令牌(https://developer.linkedin.com/documents/exchange-jsapi-tokens-rest-api-oauth-tokensLinkedIn的Oauth問題 - oauth_verifier

我使用這個庫 - http://code.google.com/p/oauth-php/ - 而不是PECL擴展因爲我無法安裝擴展到服務器上。

似乎有成爲很多類似的問題,但沒有這實際上回答的問題 - 如何使用上述庫LinkedIn進行身份驗證。

我的代碼如下:

$cookie_name  = "linkedin_oauth_" . $this->_c->linkedin_api_key; 
    $credentials_json = stripslashes($_COOKIE[$cookie_name]); 
    $credentials  = json_decode($credentials_json); 

    // Get the Access Token + Secret 
    $access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken'; 

    OAuthStore::instance("2Leg", array(
     'consumer_key'  => $this->_c->linkedin_api_key, 
     'consumer_secret' => $this->_c->linkedin_api_secret 
    )); 

    try { 

     $request = new OAuthRequester($access_token_url, 'POST', array(
      'xoauth_oauth2_access_token' => $credentials->access_token 
     )); 

     $result = $request->doRequest(); 

    } catch(OAuthException2 $e) { 

     print_r($e->getMessage()); 

    } 

catch語句輸出:

Request failed with code 400: oauth_problem=parameter_absent&oauth_parameters_absent=oauth_verifier

我如何獲得這個oauth_verifier?這是我的理解,我並不需要它,如果我經過xoauth_oauth2_access_token了嗎?

我已經檢查了所有的變量即$憑證和$ this - > _ c並且所有變量都正確傳遞。

回答

2

這實際上是在OAuth的PHP庫中的缺陷。圖書館是正確處理這些前綴xoauth_參數*和對待他們它處理oauth_ *參數相同的方式。這是違反OAuth規範和大多數(全部?)其他OAuth庫可沒有這個問題。解決方法是做到以下幾點:

裏面的文件OAuthRequestSigner.php,找到如下:

1)getAuthorizationHeader函數內部,發現其內容的行:

if (strncmp($name, 'oauth_', 6) == 0 || strncmp($name, 'xoauth_', 7) == 0) 

,並改變它爲:

if (strncmp($name, 'oauth_', 6) == 0) 

2)裏面的功能getQueryString,發現它讀取行:

|| (strncmp($name, 'oauth_', 6) != 0 && strncmp($name, 'xoauth_', 7) != 0)) 

,並更改爲:

|| (strncmp($name, 'oauth_', 6) != 0) 

之後,所有你需要做的基本上是因爲你已經在做的一樣,這是如下:

try { 

    $request = new OAuthRequester($access_token_url, "POST", array('xoauth_oauth2_access_token' => $access_token)); 

    $result = $request->doRequest(); 

    var_dump($result); 

} catch(OAuthException2 $e) { 

    print_r($e->getMessage()); 

} 

你應該全部設置。如果您還有其他問題,請不要猶豫,聯繫我們developer forums,我自己或團隊中的其他人都樂意提供幫助。

享受!

-Jeremy

+1

我還提交了[錯誤](http://code.google.com/p/oauth-php/issues/detail?id=124)至封裝的維護者爲好。 – 2012-04-13 01:29:48

+0

哇。謝謝你,傑里米..這是排序問題! – 2012-04-13 09:22:21

+0

我的榮幸!花了一段時間來跟蹤這個問題,即使我有關於這個問題的經驗,所以我可以想象它引起他人的挫折感。 – 2012-04-16 16:59:20