by Mark Bao
Here’s Lightwitter, an example for Twitter’s OAuth API authentication system in Ruby on Rails. Included herein are:
- Authenticating to OAuth, with callback code.
- Sample application that calls Twitter’s API REST endpoints with OAuth authentication.
- An abstraction for the really huge default OAuth call,
oauth_request - Abstractions for other OAuth and general stuff in
application.rb:current_user,logged_in?,get_consumer, andaccess_token. - Documented code on what exactly is going on.
First, you’ll need the OAuth gem (and the JSON gem if you don’t have it)
$ gem install oauth
$ gem install json
Configure your databases in config/database.yml, and then migrate.
$ rake db:migrate
Your keys can be found at the Twitter OAuth application manager page. Put these in the get_consumer method in app/controllers/application.rb. First consumer key, then consumer secret. It should look something like:
# get_consumer establishes a OAuth consumer object
def get_consumer
# consumer key, consumer secret
OAuth::Consumer.new("jaifCOie34LOLef93WTF32",
"dfa5i2sjJZ245O2F45I23Dfje3ofJEO2I42FF35Q55",
{ :site=>"http://twitter.com" })
end
Made it this far? You’re too legit to quit. Have fun.
For the lack of a consistent international agreement about what ‘public domain’ is, this work is licensed under the WTFPL (the Do What The F*** You Want To Public License) http://sam.zoy.org/wtfpl/
(And uh, dual licensed under the MIT license if such language doesn’t jive well with you/your employer.)
