Passing Extra Parameters to Twitter via OAuth
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.
-
Jacob Emerick
Apr 6, '13
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.
Add to this discussion-
Mike
Jun 12, '13
Forgot to mention to also add an ampersand to version like $oauth_hash .= 'oauth_version=1.0&'; if passing screen name too.
-
Jacob Emerick
Jun 12, '13
Ah, thanks for the heads up, Mike! I've adjusted the example above, should be all good now.
Add to this discussion-
Jacob Emerick
Jun 13, '13
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?
-
Olle
Jun 13, '13
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;-)
-
Jacob Emerick
Jun 13, '13
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!
Add to this discussion-
Jacob Emerick
Sep 19, '13
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!
Add to this discussion