PHP实现登录与twitter第一步和401错误

时间:2013-08-19 13:28:01

标签: php twitter oauth authorization

我正在尝试实现一个PHP代码来完成3个步骤,通过twitter按钮连接某人。

如在twitter文档中所述,我必须首先向/ oauth / request_token发出POST请求

那是失败的,它回来时出现了401错误:

“无法验证oauth签名和令牌”

这是如何:

这是我的标题

Array
(
    [0] => Host: api.twitter.com
    [1] => User-Agent: skeleton master
    [2] => accept:*/*
    [3] => Content-Type: application/x-www-form-urlencoded
    [4] => Authorization: OAuth oauth_callback="http%3A%2F%2Fj55.host3.com",

oauth_consumer_key="**************",// value from my app     
                  oauth_nonce="sa72twxjlej6xd7cd02dcm51x0ss2g1vq9xj5u2r9m",
                  oauth_signature="fxaUSTkN8oT8wYnOKfLCEktZmmw%253D",
                  oauth_signature_method="HMAC-SHA1",
                  oauth_timestamp="1376917213",
                  oauth_version="1.0"
    )

我的签名功能:

function define_signature(){
$string="oauth_callback=".rawurlencode($this->oauth_callback);
$string.="&oauth_consumer_key=".rawurlencode($this->oauth_consumer_key);
$string.="&oauth_nonce=".rawurlencode($this->oauth_nonce);
$string.="&oauth_signature_method=".rawurlencode("HMAC-SHA1");
$string.="&oauth_timestamp=".rawurlencode($this->oauth_timestamp);
$string.="&oauth_version=1.0";

$str=$this->HTTP_method."&".rawurlencode($this->base_url)."&".rawurlencode($string);
$this->base_string=$str;
$str2=rawurlencode($this->oauth_consumer_secret)."&";
$key=base64_encode(hash_hmac("sha1",$str,$str2,true));
$this->oauth_signature=rawurlencode($key);
}

我发送curl

$c=curl_init();

$url="https://api.twitter.com/oauth/request_token";
curl_setopt($c,CURLOPT_URL,$url);

curl_setopt($c,CURLOPT_HTTPHEADER,$header);
curl_setopt($c,CURLOPT_POST,true);
curl_setopt($c,CURLOPT_RETURNTRANSFER,true);

curl_setopt($c,CURLOPT_HEADER,true);
curl_setopt($c, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

curl_setopt($c,CURLOPT_SSL_VERIFYPEER,true);


$output=curl_exec($c);

print_r($output);

这是我的签名函数中使用的基本字符串

POST&https%3A%2F%2Fapi.twitter.com%2Foauth%2Frequest_token&
oauth_callback%3Dhttp%253A%252F%252F.host31.com%
26oauth_consumer_key%**********
%26oauth_nonce%3Dqqlbu83vwexwnugyxi
ipdufus4plexu4ngfhoidlxa%26oauth_signature_method%3DHMAC-
SHA1%26oauth_timestamp%3D1376914890%26oauth_version%3D1.0

我没有发现这个错误....帮助我svp

0 个答案:

没有答案