嘿,我正在嘗試在PHP中使用LinkedIn的OAuth。我被困在獲取請求令牌的第一步。我所知道的是你發佈一些值到他們的服務器,並讓你的令牌回來。因此,我將記錄的參數發佈到'https://api.linkedin.com/uas/oauth/requestToken',我得到了一個400錯誤。如何獲得LinkedIn請求令牌?
這裏的要求:
$postArr = array();
//$postArr["oauth_callback"] = ""; idk they said this was optional...
$postArr["oauth_consumer_key"] = "ForBritishEyesOnly"; //is this the application secret key or the api key?
$postArr["oauth_nonce"] = "UltraRandomNonceFTW";
$postArr["oauth_timestamp"] = time();
$postArr["oauth_signature_method"] = "HMAC-SHA1"; //lolwut
$postArr["oauth_version"] = "1.0";
$params = array('http'=>array('method'=>'post','content'=>http_build_query($postArr)));
$context = stream_context_create($params);
$stream = file_get_contents('https://api.linkedin.com/uas/oauth/requestToken', false, $context);
我不認爲我的帖子ARG遊戲正確的,但任何幫助是非常讚賞 - 我只是不想再打用別人的庫來解決這個問題。
-------編輯:嘗試2元詹姆斯的輸入---------
確定,所以在這裏進行即時給你送我的測試環節的呼叫。我是,實際上能夠得到迴應,但它不喜歡我的簽名(大驚喜,我知道)。那麼我搞砸了加密有多糟糕?
//setup GET args
$url = "http://term.ie/oauth/example/request_token.php?";
$url .= "oauth_version=1.0&";
$url .= "oauth_nonce=" . rand(0, 100000) . "&";
$url .= "oauth_timestamp=" . time() . "&";
$url .= "oauth_consumer_key=key&";
$url .= "oauth_signature_method=HMAC-SHA1&";
//encrypt the request according to 'secret'
$sig = urlencode(base64_encode(hash_hmac("sha1", $url, "secret")));
//append the url encoded signature as the final GET arg
$url .= "oauth_signature=" . $sig;
//do it to it
echo file_get_contents($url);
編輯由詹姆斯
嘗試:
//setup GET args
$url = "http://term.ie/oauth/example/request_token.php?";
$url .= "oauth_consumer_key=key&";
$url .= "oauth_nonce=" . rand(0, 100000) . "&";
$url .= "oauth_signature_method=HMAC-SHA1&";
$url .= "oauth_timestamp=" . time() . "&";
$url .= "oauth_version=1.0&";
好吧,我在最後一步你遇到了一些麻煩,在這裏你實際上發出了api請求。這是一個不好的簽名,但它讓我感到困惑,因爲它對所有步驟都是相同的例程,其他簽名工作得很好。原來,api請求url中有一個'〜'字符。顯然,這個字符是**不是**應該是在基本字符串中的URL編碼! 當基礎字符串正在構建時,應該將** urlencode($ endpoint)**改爲** str_replace('%7E','〜',rawurlencode($ endpoint))**來幫助處理這個怪癖。 –