It looks like you're not following the OAuth Authentication Flow (unless this is just the end where the user has already authenticated). I've been working with OAuth for a bit these past few weeks and had a lot of trouble. Just got it working last night using just the OAuth PHP Library they provide on their website. You can find it here -> http://oauth.googlecode.com/svn/code/php/OAuth.php
This is what I have developed from that (full workflow):
Authenticate Page:
$oauth_signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$oauth_consumer_key = new OAuthConsumer( "your_twitter_consumer_key", "your_twitter_consumer_secret" );
$oauth_token = NULL;
$request_token = OAuthRequest::from_consumer_and_token( $oauth_consumer_key, $oauth_token, 'GET', "https://api.twitter.com/oauth/request_token", array('oauth_callback' => "http://your/callback/url/")) );
$request_token->sign_request( $oauth_signature_method, $oauth_consumer_key, $oauth_token );
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $request_token->to_url());
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curl_handle);
curl_close($curl_handle);
$token = OAuthUtil::parse_parameters($response);
$_SESSION['oauth_token'] = $token['oauth_token'];
$_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
header('Location: https://api.twitter.com/oauth/authorize?oauth_token=' . $token['oauth_token']);
Callback Page:
$oauth_signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$oauth_consumer_key = new OAuthConsumer( "your_twitter_consumer_key", "your_twitter_consumer_secret" );
$oauth_token = new OAuthConsumer( $_SESSION['oauth_token'], $_SESSION['oauth_token_secret'] );
$access_token = OAuthRequest::from_consumer_and_token( $oauth_consumer_key, $oauth_token, 'GET', "https://api.twitter.com/oauth/access_token", array('oauth_verifier' => $_REQUEST['oauth_verifier']) );
$access_token->sign_request( $oauth_signature_method, $oauth_consumer_key, $oauth_token );
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $access_token->to_url());
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curl_handle);
curl_close($curl_handle);
$token = OAuthUtil::parse_parameters($response);
$_SESSION['access_token'] = $token;
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);
// user has been verified. Token is stored in $_SESSION. Now you can make calls to the api
header('Location: http://your/completion/page');
Calling /account/verify_credentials:
$access_token = $_SESSION['access_token'];
$oauth_signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$oauth_consumer_key = new OAuthConsumer( "your_twitter_consumer_key", "your_twitter_consumer_secret" );
$oauth_token = new OAuthConsumer( $access_token['oauth_token'], $access_token['oauth_token_secret'] );
$request = OAuthRequest::from_consumer_and_token( $oauth_consumer_key, $oauth_token, 'GET', 'https://api.twitter.com/account/verify_credentials.json', NULL );
$request->sign_request( $oauth_signature_method, $oauth_consumer_key, $oauth_token );
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $request->to_url());
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curl_handle);
curl_close($curl_handle);
$response = json_decode($response);
print_r($response);
I also created a test status update page with this code:
$access_token = $_SESSION['access_token'];
$oauth_signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$oauth_consumer_key = new OAuthConsumer( "your_twitter_consumer_key", "your_twitter_consumer_secret" );
$oauth_token = new OAuthConsumer( $access_token['oauth_token'], $access_token['oauth_token_secret'] );
$post_fields = array(
'status' => "This is a test status update."
);
$request = OAuthRequest::from_consumer_and_token( $oauth_consumer_key, $oauth_token, 'POST', 'https://api.twitter.com/statuses/update.json', $post_fields );
$request->sign_request( $oauth_signature_method, $oauth_consumer_key, $oauth_token );
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $request->to_url());
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl_handle, CURLOPT_USERAGENT, "Twitter OAuth Test");
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($curl_handle, CURLOPT_POST, TRUE);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post_fields);
$response = curl_exec($curl_handle);
print_r(curl_getinfo($curl_handle));
curl_close($curl_handle);
$response = json_decode($response);
print_r($response);
That's the complete Twitter OAuth Authentication. I hope this helps you or at least gets you started!