{"error":"Could not authenticate with OAuth.","request":"\/1\/statuses\/update.json"}


#1

HI all…
I am having troubles with status update…as i am completely new to this…please help me out.as soon as possible as it is urgent.
i am showing my code…
i am directly giving oauth-token and oauth_token_secret just for simplicity here…but i am successfully able to get those values…
here is my code…

<?php
function _http($url, $post_data = null)
    {       
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        if(isset($post_data))
        {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }

        $response = curl_exec($ch);
       
        curl_close($ch);
	
        return $response;
    }
 function buildAuthorizationHeader($oauth){
    $r = 'Authorization: OAuth '; //header prefix

    $values = array(); //temporary key=value array
    foreach($oauth as $key=>$value)
        $values[] = "$key=\"" . rawurlencode($value) . "\""; //encode key=value string

    $r .= implode(', ', $values); //reassemble
    return $r; //return full authorization header
}//end buildAuthorizationHeader()

function sendRequest($oauth, $baseURI){
    $header = array( buildAuthorizationHeader($oauth), 'Expect:'); //create header array and add 'Expect:'
	
    $options = array(CURLOPT_HTTPHEADER => $header, //use our authorization and expect header
                           CURLOPT_HEADER => false, //don't retrieve the header back from Twitter
                           CURLOPT_URL => $baseURI, //the URI we're sending the request to
                           CURLOPT_POSTFIELDS=>$post, //this is going to be a POST - required
                           CURLOPT_RETURNTRANSFER => true, //return content as a string, don't echo out directly
                           CURLOPT_SSL_VERIFYPEER => false); //don't verify SSL certificate, just do it

    $ch = curl_init(); //get a channel
    curl_setopt_array($ch, $options); //set options
    $response = curl_exec($ch); //make the call
    curl_close($ch); //hang up

    return $response;
}//end sendR
$ot="xxxxxxx";
$ost="XXXXXXXX";
$user_id="386511063";
$screen_name="uniqe_mohini";
$ct="XXXX";
$cs="XXXX";

$baseURI="http://api.twitter.com/1/statuses/update.json";
$params=array(
			"status"=>"hello",
			"oauth_consumer_key"=>$ct,
			"oauth_nonce"=>time(),
			"oauth_signature_method"=>"HMAC_SHA1",
			"oauth_token"=>$ot,
			"oauth_timestamp"=>time(),
			"oauth_version"=>"1.0"
			);
uksort($params,'strcmp');
foreach ($params as $k=>$v)
{
	$pairs[]=rawurlencode($k)."=".rawurlencode($v);
}

$connectedstring=implode('&',$pairs);
$basestring="POST&".rawurlencode($baseURI)."&".rawurlencode($connectedstring);

$compositeKey=rawurlencode($cs)."&".rawurlencode($ost);
$signatur=base64_encode(hash_hmac('sha1',$basestring,$compositeKey,true));
$params['oauth_signature']=$signatur;
uksort($params,'strcmp');
$response = sendRequest($params, $baseURI);
echo $response;
?>

but i am getting the error “Could not authenticate with oauth”…
please help me out…what i am missing…
would be really thankful …


#2

My advice is to use tmhOAuth - there are so many edge cases to get right that using a tested library is really the preferable way to go here.

Here’s an example on how to post a tweet using it: https://github.com/themattharris/tmhOAuth/blob/master/examples/tweet.php

In the code you’ve listed, it looks like _http is never called, and you’re not actually sending the POST body in the sendRequest call. That alone would be enough to invalidate the signature.


#3

{“error”:“Could not authenticate with OAuth.”,“request”:"/1/statuses/update.json"}

Trying to set up next srcipts plug in. filled in all fields on form with info provided by twitter.
Was requested to send test and this was the result.

Any ideas please


#4

Please start a new discussion instead of replying to one which was last updated 47 weeks ago. When doing so, please include information about exactly what you’ve tried - libraries, configuration, and concise examples of the code you’re using to issue requests to the API.