Tech, Gaming and Food Enthusiast
Implementing oAuth Twitter with Code Igniter
On August 31st Twitter will be axing basic auth GET requests, which is being overtaken by the more secure oAuth, there are a few tutorials out there on how to use oAuth and how to get started creating an app with CodeIgniter, but not many with actually helpful advice so here is my 2 penneth.
How to get started with oAuth
Firstly log into twitter and visit this link http://dev.twitter.com/apps/
This link will allow you to create a new app, I would link directly to the apps/create URL but twitter will serve you with a Method Not allowed HTTP header status error as you must request the page with POST, weird but i’m not going to question their methods!
Once you have entered your information, twitter will give you the 4 important pieces of information:
- Access Token
- Secret Access Token
- Consumer Key
- Secret Consumer Key
The access token and secret access token may not be obvious to find… On the right hand nav there will be a link to My Tokens, which will give you the first two on the list.
So for the Code Igniter part.
As always there is a useful library that will handle most of the hard work for you. I use a Library by Elliot Haughin – [email protected] which is available under the GNU General Public License and can be downloaded here
Once downloaded add the twitter.php class file into System\Application\Libraries folder.
In this short example I will connect to twitter via oAuth and request a timeline.
class Twitter extends Controller {
//Could store these in the config etc, up to you
private $_consumer_key = 'myconsumerkey';
private $_consumer_secret = 'myconsumersecretkey';
private $_access_token = 'myaccesstoken';
private $_secret_access_token = 'mysecretaccesstoken';
function __construct() {
parent::Controller();
}
function index() {
$this->load->library('twitter');
$this->twitter->oauth($this->_consumer_key, $this->_consumer_secret, $this->_access_token, $this->_secret_access_token);
$data['tweets'] = $this->twitter->call('statuses/user_timeline');
//Load this data into a view and show it.
}
}
There are all sorts of requests that you can make, timeline is just one example, a full list can be found in the private $_methods array.
Give it a go, I had a twitter widget up and running in minutes!
| Print article | This entry was posted by Matt on August 25, 2010 at 11:23 am, and is filed under Code Igniter, Web Dev. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
- Matt Williams’ Blog: Implementing oAuth Twitter with CodeIgniter | Development Blog With Code Updates : Developercast.com
- Matt Williams’ Blog: Implementing oAuth Twitter with CodeIgniter | PHP
- Implementing oauth twitter search in C# and JSON « Coder buddy
- code de la route gratuit
- OAuth basics, twitter OAuth for CodeIgniter | blog.i-evaluation.com
about 2 years ago
Hi Obolus,
The only thing I would say is that I wouldn’t be parsing the data within the model, I would simply use the models for data retrieval.
about 2 years ago
Thanks for the post. I’ve read through Elliot’s page and this one. I’m a little confused about a simple problem – how do you pull other people’s twitter feed/timeline?
The only way I’m aware of doing it is by going the route of a custom search query…
$this->twitter->search(‘search’, array(‘q’ => ‘from:’ . $twitter_name));
The problem is that you can’t rely on this because some tweets don’t show up in real time search results.
You also can’t simply add on a user name to the timeline call to make something like this…
$this->twitter->call(‘statuses/user_timeline/’ . $twitter_name );
Am I missing something here?
Thanks again
about 2 years ago
Hi Obulus,
Thanks for the comment, in response there are only a couple things you can do. Your method will work, there are only a few timeline methods allowed by twitter.
The timeline methods will accept an optional parameter to set the specific user
Hope this clears the issue up
Thanks
Matt
about 2 years ago
Nevermind! For some reason I didn’t even think about the argument for the user_timeline call (to get tweets for a specific person). Thats what I get for not reading through the Twitter API until now.
Works great, thanks.
about 2 years ago
This is what my model looks like btw…
function getUserTweets($user, $amount = 10)
{
// Pull the last 20 (default amount set by Twitter) tweets from the user’s timeline
$tweet_feed = $this->twitter->call(‘statuses/user_timeline’, array(‘screen_name’ => $user));
// Convert the results to arrays instead of objects (using a customer helper function – _parseObjectToArray)
foreach ($tweet_feed as $raw_tweet)
{
$tweets_as_array []= $this->_parseObjectToArray($raw_tweet);
}
// Get the stuff I need from each tweet (the tweet text and the date it was posted)
foreach ($tweets_as_array as $tweet_data)
{
$tweets []= array(‘date’ => $tweet_data['created_at'], ‘tweet’ => $tweet_data['text']);
}
// Limit the results specified in the controller, leave as default amount set in function argument above (10) if none
$final_tweets = array_slice($tweets, 0, $amount);
return $final_tweets;
}
about 2 years ago
The part about converting the objects to arrays is kind of pointless since you can just get the data like this: $tweet_data->created_at
Work in progress
about 2 years ago
Yea, it’s a habit of mine to use models to store libraries of functions like this as time savers. I also don’t like big controllers or a lack of flexibility. Allllllssssso, I don’t know the ins and outs of CI too well so I have to stick to what I know works, heh.
about 2 years ago
I wouldn’t worry about it much, it is only a small issue, which is only a programming standard niggle so it can be different from person to person
about 2 years ago
You’re a life saver!
I have just tried so many different libraries to implement an oAuth solution with no success.
One attempt with your tutorial and I have access within minutes.
Thank you for your article you’ve spared a key component of my application from being cut from the first public release.
about 2 years ago
Glad I could help.
about 2 years ago
hello,
watch out the latest twitter codeigniter library on tahsin’s garage.
about 2 years ago
Thanks, very clear and it works