After spending months scratching my head and wondering if OAuth even works at all. I was able to crank out a library that will do OAuth for twitter in about 2 hrs. Yeah you are right I probably should have been actually doing my job but this is way more fun. It also gave me the opportunity to revisit my google OAuth library and fix the issues it had with HMAC signing.
Basically HMAC-SHA1 signing was impossible in the old google library, so I fixed that and it should still work the same if you are using RSA so don't worry about updating. If you want HMAC though you will need to jump through a few more hoops (nothing too serious) mainly involving keeping and using a token_secret value. I outline everything in the CI wiki if you are interested in how to use it and how it works.
Anyway back to the library I just finished. This library works pretty much the same exact way as the google library the only difference is twitter only lets you use HMAC signing. This means that you need to maintain a token secret value. Not a huge deal but one more thing to keep track of. If you want a more technical look at the implementation check out the CI wiki post I made
here
Otherwise if you would rather just download the file you can get it from my dropbox account:
http://dl.dropbox.com/u/13081549/twitter_oauth.php
You also need the oauth_helper I wrote you can get it from dropbox here:
http://dl.dropbox.com/u/13081549/oauth_helper.php
If you want to see what it does then visit the wiki page
here
Using this library is very similar to using the google oauth library here is an example:
function request_token()
{
$params = array('key'=>'CONSUMER_KEY', 'secret'=>'CONSUMER_SECRET');
$this->load->library('twitter_oauth', $params);
$response = $this->twitter_oauth->get_request_token(site_url("site/access_token"));
$this->session->set_userdata('token_secret', $response['token_secret']);
redirect($response['redirect']);
}
function access_token()
{
$params = array('key'=>'CONSUMER_KEY', 'secret'=>'CONSUMER_SECRET');
$this->load->library('twitter_oauth', $params);
$response = $this->twitter_oauth->get_access_token(false, $this->session->userdata('token_secret'));
$this->_store_in_db($response);
}
The response array returned from the get_access_token call contains the access token, the access token secret, the twitter user id, and twitter screen name. You can do what you want with the last two values but you need to store the access token and token secret as these are how you authenticate with twitter.
[EDIT]
It was pointed out to me that once you have an OAuth access token it isn't exactly clear what you do with it. The OAuth portion of your API requests goes in the Authorization section of the header. The oauth_helper provides a nice function that will create the desired Authorization section.
$access = array('oauth_token'=>urlencode($this->session->userdata('oauth_token')), 'oauth_token_secret'=>urlencode($this->session->userdata('oauth_token_secret')));
$extra = array('status'=>rawurlencode('Tweeting from my API'));
$authhdr = get_auth_header('http://api.twitter.com/1/statuses/update.json', 'CONSUMER KEY', 'CONSUMER SECRET', array_merge($access, $extra), 'POST', OAUTH_ALGORITHMS::HMAC_SHA1);
The rest of the header is up to you. Make sure you include the values you are posting in the get_auth_header extra field, and that it is rawurlencoded.
[EDIT 2]
I also ran into an issue where I did not specify a callback when I registered the application with twitter. If you don't specify a callback, there is a bug with the registration process that will force your application to be set as a desktop application. If you are getting an index out of bounds exception when you make your first request make sure you have a callback set for your application on twitter.