Get error: Could not authenticate you [code] => 32


#1

Hi,
i use follow code and works fine…

function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = “$key=” . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . ‘&’ . rawurlencode(implode(’&’, $r));
}

function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ‘;
$values = array();
foreach($oauth as $key=>$value)
$values[] = “$key=”" . rawurlencode($value) . “”";
$r .= implode(’, ', $values);
return $r;
}

$url = “https://api.twitter.com/1.1/statuses/user_timeline.json”;

$oauth_access_token = “YOURVALUE”;
$oauth_access_token_secret = “YOURVALUE”;
$consumer_key = “YOURVALUE”;
$consumer_secret = “YOURVALUE”;

$oauth = array( ‘oauth_consumer_key’ => $consumer_key,
‘oauth_nonce’ => time(),
‘oauth_signature_method’ => ‘HMAC-SHA1’,
‘oauth_token’ => $oauth_access_token,
‘oauth_timestamp’ => time(),
‘oauth_version’ => ‘1.0’);

$base_info = buildBaseString($url, ‘GET’, $oauth);
$composite_key = rawurlencode($consumer_secret) . ‘&’ . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac(‘sha1’, $base_info, $composite_key, true));
$oauth[‘oauth_signature’] = $oauth_signature;

// Make Requests
$header = array(buildAuthorizationHeader($oauth), ‘Expect:’);
$options = array( CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

$twitter_data = json_decode($json);

but when I change api
$url = “https://api.twitter.com/1.1/statuses/user_timeline.json”;
to
$url = “https://api.twitter.com/1.1/statuses/show.json?id=210462857140252672”;

I get the following error:

stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [message] => Could not authenticate you [code] => 32 ) ) )

Thanks.


#2

It doesn’t appear that you’re including query parameters in your signature base string calculation – there are a few ways you can address that – one way would be to parse the URL in that function and extract such parameters.

I would really recommend using an already-existing OAuth library rather than try to write one yourself. There are quite a lot of gotchas in OAuth.


#3

You need to add the parameters in the oauth array AND in the options array. See the answer in the stackoverflow here : http://stackoverflow.com/a/14007402


#4

@remitoffoli Thank you so much! I’ve been looking for this exactly.