Updating user's status - 401 Error


#1

Hello,
I’m trying to update a user’s status in php using the following code but each time i am getting a 401 error

$twitter_url = 'https://api.twitter.com/1/statuses/update.json';
$tweet = "test tweet";

$post_parameters="oauth_consumer_key=".$request->get_parameter('oauth_consumer_key')."&oauth_signature_method=".$request->get_parameter('oauth_signature_method').
"&oauth_token=".$request->get_parameter('oauth_token')."&oauth_timestamp=".$request->get_parameter('oauth_timestamp')."&oauth_nonce=".$request->get_parameter('oauth_nonce').
"&oauth_version=".$request->get_parameter('oauth_version')."&oauth_signature=".urlencode($request->get_parameter('oauth_signature'))."&status=".urlencode($tweet);

$ci = curl_init();
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ci, CURLOPT_TIMEOUT, 10);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ci, CURLOPT_HEADER, FALSE);
curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ci, CURLOPT_VERBOSE, true);

curl_setopt($ci, CURLOPT_POST, TRUE);
curl_setopt($ci, CURLOPT_POSTFIELDS, $post_parameters);

curl_setopt($ci, CURLOPT_URL, $twitter_url);
$result = curl_exec($ci);
$response_info=curl_getinfo($ci);
curl_close ($ci);

return $response_info;

I am 100% sure that all the parameters set in $post_parameters are correct because when i use them for verifying credentials, i receive an HTTP 200 header. The problem is in my opinion coming from the curl section.

Can someone please help?
Thanks,
Krishley


#2

The difference between generating a signature for a POST and GET to account/verify_credentials is pretty great – how are you generating your signature base string in this context? Have you tried using the OAuth tool built into this site to generate golden examples to code against?


#3

Hello Taylor,
I use Abraham’s php library to generate the signature like this :

$consumer_key = XXXXX; $consumer_secret = XXXXX; $oauth_access_token = XXXXX; $oauth_access_token_secret = XXXXX;

$twitter_url = ‘https://api.twitter.com/1/statuses/update.json’;

require_once(‘OAuth.php’);
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
$sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
$token = new OAuthConsumer($oauth_access_token, $oauth_access_token_secret);

$request = OAuthRequest::from_consumer_and_token($consumer, $token, ‘POST’, $twitter_url, array());
$request->sign_request($sha1_method, $consumer, $token);
$oauth_signature = urlencode($request->get_parameter(‘oauth_signature’));


#4

In this code, you’re signing the request ($request->sign_request) before you’ve set a status parameter that you’re sending along with the request.


#5

Thanks Taylor…problem fixed! :smiley:
Just had to set a status parameter to the request as you mentioned

$request = OAuthRequest::from_consumer_and_token($consumer, $token, 'POST', $twitter_url, array('status'=>$tweet));