"Could not authenticate" when adjusting url to have parameters


#1

Howdy,

I’m trying retrieve statuses and I want to filter retweets and replies.

The code below works without parameters. I’m not sure how to actually apply the parameters correctly. The base for the sample is not mine and the only place I could tell maybe these parameters “should” go are in the commented out “CURLOPT_POSTFIELDS => $postfields” but I have no idea.

https://api.twitter.com/1.1/statuses/user_timeline.json
to
https://api.twitter.com/1.1/statuses/user_timeline.json?include_rts=false&exclude_replies=true

Below is what I was trying but was getting an error 32 - Could not authenticate you

<?php

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";
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?include_rts=false";

$oauth_access_token = "removed";
$oauth_access_token_secret = "removed";
$consumer_key = "removed";
$consumer_secret = "removed";


$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);

foreach ($twitter_data as $tweet)
{
$text = $tweet->text;
echo $text . "</br></br>";
}

?>

#2

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


#3

Thanks for replying. Wasn’t getting much traffic on my question here and I did eventually find an answer on stackoverflow (http://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1)

I appreciate your comment as it will be a great reference.