Passing Extra Parameters to Twitter via OAuth

Web Development by Jacob Emerick

A while ago I wrote a post explaining how to pull data from Twitter’s API. I focused on pulling the default authenticated user timeline (which, if you set up the application under your account, you are the authenticated user). Wouldn’t it be great if you could customize the request a bit?

The default feed will have the most recent 20 tweets from you. If you want to pull a different user or a different amount of tweets you’ll have to pass in some parameters with your request. Note: protected timelines can only be pulled by the owner or their followers. There are two parameters that we’ll need to use… screen_name and count.

Building off my first post, to pass in additional paramters you’ll need to change a few lines in the OAuth Signature.

$oauth_hash = '';
$oauth_hash .= 'count=TOTAL_COUNT_YOU_WANT&';
$oauth_hash .= 'oauth_consumer_key=YOUR_CONSUMER_KEY&';
$oauth_hash .= 'oauth_nonce=' . time() . '&';
$oauth_hash .= 'oauth_signature_method=HMAC-SHA1&';
$oauth_hash .= 'oauth_timestamp=' . time() . '&';
$oauth_hash .= 'oauth_token=YOUR_ACCESS_TOKEN&';
$oauth_hash .= 'oauth_version=1.0&';
$oauth_hash .= 'screen_name=SCREEN_NAME_HERE';

Another change you need to make is with the OAuth header, again adding the two new parameters.

$oauth_header = '';
$oauth_header .= 'count="TOTAL_COUNT_YOU_WANT", ';
$oauth_header .= 'oauth_consumer_key="YOUR_CONSUMER_KEY", ';
$oauth_header .= 'oauth_nonce="' . time() . '", ';
$oauth_header .= 'oauth_signature="' . $signature . '", ';
$oauth_header .= 'oauth_signature_method="HMAC-SHA1", ';
$oauth_header .= 'oauth_timestamp="' . time() . '", ';
$oauth_header .= 'oauth_token="YOUR_ACCESS_TOKEN", ';
$oauth_header .= 'oauth_version="1.0", ';
$oauth_header .= 'screen_name="SCREEN_NAME_HERE"';

The last change is with the curl url itself.

curl_setopt($curl_request, CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=TOTAL_COUNT_YOU_WANT&screen_name=SCREEN_NAME_HERE');

There are other parameters you could pass in as well, including exclude_replies, max_id, and more. You would just follow the same changes for each new field. When I was testing this I noticed that Twitter seemed to want the parameters in alphabetical order - not sure if that was a temp issue or is still the case, but it’s not hard to set things up that way. To see the other parameters, check out Twitter’s documentation on user_timeline.

Comments (11)

  1. Miklas Njor

    Hi Jacob.

    Thanks for the post and the one leading up to this. Made a lot of things a lot clearer.

    Just to confirm that parameters still have to be in alphabetical order, although a don't understand why twitter want's it this way.

    I unpack a list of variables for the parameters, so not all parameters are used every time. This means having to build if statements to compensate for the '&' and ', ' which might or might not be there, adding quite a lot of messy lines to your nice code.

    And to others who, like me, might be a bit slow, alphabetical means that extra parameters beginning with letters A -> N (like max_id!) go before the Oauth_stuff... :)

    1. Jacob Emerick

      Hi Miklas - thanks! Good point about the alphabetical... It's really annoying with all of the 'oauth' params, especially when you're passing in 'count' and 'screen_name' params and the custom params are all jumbled up with the required ones.

  2. Mike

    Thanks for the code and write ups. I kept getting an authentication error when trying to pass parameters at first. For anyone else that has the issue, be sure to use $oauth_hash . = count=TOTAL_COUNT_YOU_WANT&'; instead of just $oauth_hash .= 'count=TOTAL_COUNT_YOU_WANT';

    The example is missing the ampersand.

    1. Mike

      Forgot to mention to also add an ampersand to version like $oauth_hash .= 'oauth_version=1.0&'; if passing screen name too.

    2. Jacob Emerick

      Ah, thanks for the heads up, Mike! I've adjusted the example above, should be all good now.

  3. Olle

    Hi I can't get this to work I only get "Could not authenticate you" as soon as I try to add a parameter (count) this way. Without the parameter I get the tweets. Any idea?

    Kind regards
    Olle

    1. Jacob Emerick

      Hi Olle! Without looking at your code my first thought is what Mike mentioned earlier, that you may be missing an ampersand behind your count parameter within the oauth hash. Second thought is the actual CURLOPT_URL... you'll need to add ?count=XXX to the API URL. Do either one of those help out?

    2. Olle

      Many thanks! I missed to ad to the CURLOPT_URL. With shame I hope this helps someone else that like me, apparently have difficulties with reading a whole page;-)

    3. Jacob Emerick

      Ha, no problem! It took me a lot of head-scratching to get this working for the first time as well. First time w/ OAuth and all... This was not an easy one to get started with. Glad it worked out!

  4. Naveen

    Thanks for this post.

    Does anyone know how to pass in multiple usernames? When I pass in one value for screen_name it works fine. When I try to pass two in it chokes. I think it has something to do with the "," separating the usernames?

    1. Jacob Emerick

      Hey Naveen,

      The solution I've always gone for this sort of thing is setting up a twitter list and pulling that. The benefit here is that it's easier to manage (especially for clients) than going and playing with the code down the road. There could be a way to pull multiple users, but I haven't tried passing it directly into that param. Hope this helps!