0

我正在尝试实现一个 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);
}

我用卷曲发送

$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

4

0 回答 0