php - GET 到 POST 更改后身份验证不再有效

标签 php authentication twitter oauth twitter-oauth

我使用下面的 PHP 代码成功获取了 Twitter 用户的时间线(REST API/OAuth 1.0a) 现在我想在 Twitter 上关注一个用户。我需要将 GET 更改为 POST 请求,现在代码不再有效。 错误:

[code] => 32 [message] => Could not authenticate you.

需要改变什么才能让它发挥作用?

PHP:

// ("x" = I removed the values)
$token = "x";
$token_secret = "x"; 
$consumer_key = "x";
$consumer_secret = "x";

$host = 'api.twitter.com';
/*
// NOT WORKING:
$method = 'POST';
$path = '/1.1/friendships/create.json'; // api call path
*/

// WORKS:
$method = 'GET'; 
$path = '/1.1/statuses/user_timeline.json'; // api call path

$query = array( // query parameters
    'screen_name' => 'twitter',
    //'count' => '2'
);

$oauth = array(
    'oauth_consumer_key' => $consumer_key,
    'oauth_token' => $token,
    'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
    'oauth_timestamp' => time(),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_version' => '1.0'
);

$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
$query = array_map("rawurlencode", $query);

$arr = array_merge($oauth, $query); // combine the values THEN sort

asort($arr); // secondary sort (value)
ksort($arr); // primary sort (key)

// http_build_query automatically encodes, but our parameters
// are already encoded, and must be by this point, so we undo
// the encoding step
$querystring = urldecode(http_build_query($arr, '', '&'));

$url = "https://$host$path";

// mash everything together for the text to hash
$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);

// same with the key
$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);

// generate the hash
$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));

// this time we're using a normal GET query, and we're only encoding the query params
// (without the oauth params)
$url .= "?".http_build_query($query);

$oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
ksort($oauth); // probably not necessary, but twitter's demo does it

// also not necessary, but twitter's demo does this too
function add_quotes($str) { return '"'.$str.'"'; }
$oauth = array_map("add_quotes", $oauth);

// this is the full value of the Authorization line
$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));

// if you're doing post, you need to skip the GET building above
// and instead supply query parameters to CURLOPT_POSTFIELDS
$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
                  //CURLOPT_POSTFIELDS => $postfields,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => false);

// do our business
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

$twitter_data = json_decode($json);
print_R($twitter_data);
?>

最佳答案

您指定了用于签名的方法,但实际上并没有发出 POST 请求。

您必须设置 curl_setopt($feed, CURLOPT_POST, true)curl_setopt($feed, CURLOPT_POSTFIELDS, $query) 而不是将您的参数添加到 URL 作为查询字符串。

有关使用 CURL 的 POST 请求的更多信息,请访问 documentation page .这是一个文件上传,但唯一的区别是你必须删除的 @

注意:如果 Twitter 要求数据为 application/x-www-form-urlencoded 格式,则必须使用 http_build_query 而不是为CURLOPT_POSTFIELDS 选项。

关于php - GET 到 POST 更改后身份验证不再有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31134000/

相关文章:

go - 如何使用 go-twitter bot 上传媒体?

twitter - TweetSharp 方法 ListTweetsOnHomeTimeline() 返回 null

php - 在一行中左连接两个值

php - 每8个字符后插入一个字符

javascript - 绘制圆弧,取决于输入的时间

php - 结合 SSL/TLS (HTTPS) 的 HTTP 基本身份验证将用于对用户进行身份验证

php - 在 HTML 页面中显示纯 PHP 代码

ember.js - Ember用户认证(客户端和服务器端完全分离)

azure - 在 Azure 上发布 WebApi 后出现 "unsupported_grant_type"

jquery - Twitter 的推文按钮有回调吗?