2
由於我無法控制的原因,我使用PHP4編寫了一個twitter客戶端。請求無法正常工作 - 我很難看出發生了什麼問題。有任何想法嗎?無法驗證oauth簽名和令牌
我已經抓住了代碼,並在一個非常簡單的腳本中敲擊它,以說明本質上發生了什麼。出於安全目的,我隱藏了OAuth密鑰。
<?php
$requestTokenURL = 'https://api.twitter.com/oauth/request_token';
$consumerKey = 'fLxA6J1111111111PnvVOg';
$consumerSecret = 'H5QxDHAOHn1111111111111Ozet1HRTtVAV1FM3oYk';
$callbackURL = 'http://www.example.com';
$signature = 'POST&' . urlencode($requestTokenURL) . '&';
$auth_params = array(
'oauth_callback' => $callbackURL,
'oauth_consumer_key' => $consumerKey,
'oauth_nonce' => md5(time()),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
ksort($auth_params);
foreach($auth_params as $k=>$v) {
if ($k == 'oauth_callback') {
$v = urlencode($v);
}
$signature .= urlencode($k) . '%3D' . urlencode($v) . '%26';
}
$signature = substr($signature, 0, strlen($signature) - 3);
$signing_key = $consumerSecret . '&';
$oauth_signature = hmac_sha1($signing_key, $signature);
$auth_params['oauth_signature'] = $oauth_signature;
$auth_string = 'OAuth ';
foreach($auth_params as $k=>$v) {
$auth_string .= $k . '="' . urlencode($v);
$auth_string .= ($k == 'oauth_signature') ? '&' : '';
$auth_string .= '", ';
}
$auth_string = substr($auth_string, 0, strlen($auth_string) - 2);
echo 'Authorization header: <pre>';
echo $auth_string;
echo '</pre>';
echo '<br /><br />';
echo 'OAuth Signature: <pre>';
var_dump($oauth_signature);
echo '</pre>';
echo '<br /><br />';
//exit;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $requestTokenURL);
curl_setopt($curl, CURLOPT_POST, GET);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: ' . $auth_string)
);
$response = curl_exec($curl);
curl_close($curl);
var_dump($response);
/* function from PHP.net - no PHP4 built-in function */
function hmac_sha1($key, $data) {
$blocksize = 64;
$hashfunc = 'sha1';
if (strlen($key) >$blocksize) {
$key = pack('H*', $hashfunc($key));
}
$key = str_pad($key, $blocksize, chr(0x00));
$ipad = str_repeat(chr(0x36), $blocksize);
$opad = str_repeat(chr(0x5c), $blocksize);
$hash = pack('H*', $hashfunc(($key^$opad).pack('H*',$hashfunc(($key^$ipad).$data))));
return base64_encode($hash);
}
?>
的事情是,我試圖從http://dev.twitter.com/pages/auth#signing-requests運行此腳本的價值觀,並且簽名和授權字符串是完全一樣的 - 但是當我試圖用捲曲執行我自己的值(使用exacty相同的代碼)它只是給了我相當不具說明性的「無法驗證oauth簽名和令牌」