OAuth PHP lib-Twitter令牌请求失败


OAuth PHP lib - Twitter Token Request fails

我正试图使用这个PHP库从Twitter请求令牌:http://oauth.googlecode.com/svn/code/php/但它不起作用,我得到"验证oauth签名和令牌失败"。

我的代码:

//http://oauth.googlecode.com/svn/code/php/OAuth.php
require 'OAuth.php';
$key = 'XXXXXX';
$secret = 'XXXXXX';
$consumer = new OAuthConsumer($key, $secret);
$sig_method = new OAuthSignatureMethod_HMAC_SHA1;
$api_endpoint = 'https://api.twitter.com/oauth/request_token';
$parameters = array('oauth_callback'=>'oob');
$req = OAuthRequest::from_consumer_and_token($consumer, null, "POST", $api_endpoint, $parameters);
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
$req->sign_request($sig_method, $consumer, null);
$ch = curl_init($req->to_url());
curl_exec($ch);
curl_close($ch);

看起来您需要将request_token调用作为GET请求而不是POST:

$req = OAuthRequest::from_consumer_and_token($consumer, null, "GET", $api_endpoint, $parameters);

我对此进行了测试,从我自己的应用程序中获取令牌似乎很有效。

您可能还想看看Abraham的Twitter OAuth库,它使实现更加容易。

https://github.com/abraham/twitteroauth