1

I'm having trouble with the twitter api, I'm using a few helper functions I found online. I only want to grab my already public tweets and redisplay them on my website like the easy old simple xml approach.

It works in its current state with a basic request and grabs the default amount of posts from my feed

$url is set to the URI of twitter resource, when I add parameters like ?include_rts=false authentication fails

"{"errors":[{"message":"Could not authenticate you","code":32}]}"

I've been puzzling this for most of today and hope some one here can point me in the right direction with adding parameteres

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)); //return complete base string
}

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 = "***********";
$oauth_access_token_secret = "**************";
$consumer_key = "***********";
$consumer_secret = "************";

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



$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
                  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);

print_r($json);
Larry
  • 1,231
  • 4
  • 12
  • 18
  • Have you allowed your website as an allowed application in your Twitter? I ran into the same problem a couple years back. – David J Eddy Jun 04 '13 at 19:31
  • Still working on it, i've required the api exchange, and it works locally on wamp but its failing to find the file on live server – Larry Jun 10 '13 at 12:12
  • I've fixed that error but now its not connecting at all on the server. Loads for ages and then gives a 324 error! – Larry Jun 10 '13 at 16:23

1 Answers1

3

Guess what? You're in luck!

The twitter library I wrote uses very similar code like 'buildHeader' etc.

I wrote a step-by-step post explaining how to use the library (it's really simple) for authenticated requests.

I really suggest using that - then you won't have to worry about "could not authenticate you" errors (they're a nightmare). It just works.

The code is here on github.


Step by step, in short you:

  • Need a set of keys for your application from the twitter dev site (seems like you've already got these, nice one)
  • Enable read/write access on the twitter dev site
  • Include TwitterAPIExchange.php
  • Put these four keys into the $settings array for the above library
  • Choose the URL from the twitter docs and GET/POST
  • Perform the request in a few lines.

That's it, no more unauthenticated errors as this sorts it all out for you. If you'd prefer, take a look at the source code of my class and you'll see how it differs and how I got it working, as it seems pretty similar.

I do recommend just grabbing the class and using it though, it'll enable you to start actually coding your application and not worrying about how to talk to an external service.

Community
  • 1
  • 1
Jimbo
  • 25,790
  • 15
  • 86
  • 131
  • I just wanted to say a HUGE THANK YOU for this. I have been struggling to get my head round the new Twitter API and had loads of authentication issues. Now thanks to you I've been able to get it working and now a whole world of possibilities is before me! :-) – iagdotme Jun 05 '13 at 21:38
  • @iagdotme Great to hear - see, I told you ;) Could you please upvote / tick this answer if you consider it what you require? Cheers. – Jimbo Jun 06 '13 at 08:41
  • Actually I just stumbled upon this question and answer, but I do hope it helped the original questioner! I thought I had upvoted, but I have now! – iagdotme Jun 06 '13 at 12:41
  • @Jimbo i've followed all the steps in the documentation and have configured the Twitter app on apps.twitter.com but I'm still getting could not authenticate you error. – Mina Apr 06 '14 at 16:31