我遇到了生成oauth 1.0签名的问题,任何人都可以看到错误的内容吗?
api响应是:"无效请求:缺少签名"
使用commande行的代码启动脚本,其中consumer_id为第一个参数,consumer_secret为第二个参数。
" php' script_name' ' consumer_id' ' CONSUMER_SECRET'"
并且如果要在其他api上进行测试,请不要忘记在代码中更改base_url
这是我的代码:
<?php
function print_help(){
echo "usage: <script> <consumer_id> <consumer_secret>";
}
if ($argc != 3) {
print_help();
exit;
}
$base_url = "https://sso.openx.com/api/index/initiate";
$time = (string)time();
$signature_method= "HMAC-SHA1";
$oauth_version = "1.0";
$nonce = "ZNWVga";
$realm = "";
$consumer_key = (string)$argv[1];
$consumer_secret = (string)$argv[2];
$base_string = "POST&" . rawurlencode($base_url) ."&".
rawurlencode('realm="' . $realm .
'"&oauth_nonce="' . $nonce .
'"&oauth_timestamp="' . $time.
'"&oauth_consumer_key="' . $consumer_key .
'"&oauth_signature_method="' . $signature_method.
'"&oauth_version="' . $oauth_version.
'"&oauth_callback="oob"');
$signature = base64_encode(hash_hmac("sha1", rawurlencode($consumer_secret . "&"), $base_string, true ));
$ch = curl_init();
$headers = array();
$headers[] = 'Accept-Encoding: identity';
$headers[] = 'Connection: close';
$headers[] = 'Content-Length: 0';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Authorization: OAuth realm="'.$realm.
'", oauth_nonce="'.$nonce.
'", oauth_timestamp="' . $time .
'", oauth_consumer_key="' . $consumer_key .
'", oauth_signature_method="' . $signature_method .
'", oauth_version="'.$oauth_version.
'", oauth_signature="' . (string)$signature .
'",oauth_callback="oob"';
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$data = curl_exec($ch);
$head = curl_getinfo($ch, CURLINFO_HEADER_OUT);
print_r($head);
curl_close($ch);
var_dump($data);
//echo "\n\n\n\n output -> " + $out
?>
提前谢谢大家。
快乐的编码!