OAuth PHP lib - Twitter Token Request fails
I'm trying to request a token from Twitter using this PHP library: http://oauth.googlecode.com/svn/code/php/ But it doesn't work, I get "Failed to validate oauth signature and token".
My code:
//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);
It looks like you need to do the request_token call as a GET request not a POST:
$req = OAuthRequest::from_consumer_and_token($consumer, null, "GET", $api_endpoint, $parameters);
I tested this and it appears to work to get a token from my own application.
You also might want to take a look at Abraham's Twitter OAuth library, makes implementation a whole lot easier.
https://github.com/abraham/twitteroauth
精彩评论