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:

  1. Access Token
  2. Secret Access Token
  3. Consumer Key
  4. 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!