public
Description: Demo app for Twitter OAuth using sinatra and twitter_oauth gem
Homepage: sinitter.moocode.com
Clone URL: git://github.com/moomerman/sinitter.git
name age message
file .gems Thu Dec 24 08:16:05 -0800 2009 adding .gems manifest for heroku [moomerman]
file .gitignore Fri Mar 20 04:33:57 -0700 2009 initial basic version [moomerman]
file README.textile Thu Dec 24 09:22:07 -0800 2009 updated the README [moomerman]
file config.ru Thu Dec 24 08:06:30 -0800 2009 added a rackup file to deploy to heroku [moomerman]
directory public/ Sat Apr 18 03:25:31 -0700 2009 making use of the new sign in with twitter func... [moomerman]
file sinitter.rb Thu Dec 24 08:54:51 -0800 2009 added configured callback url and passing the o... [moomerman]
directory views/ Sun May 31 09:53:32 -0700 2009 removed public timeline temporarily as its caus... [moomerman]
README.textile

Sinitter

Sinitter is a sinatra application to demonstrate the integration of Twitter OAuth via the twitter_oauth gem.

Live Demo

You can see sinitter running at http://github-sinitter.heroku.com/.

Local Setup

To run the application locally you’ll need to [create a Twitter OAuth Application](http://twitter.com/oauth_clients/new).

Once your application is setup you will have a consumer key and consumer secret. Create config.yml in the root of sinitter eg.

consumer_key: YOUR-CONSUMER-KEY
consumer_secret: YOUR-CONSUMER-SECRET

Grab the twitter_oauth gem:

gem sources -a http://gems.github.com
sudo gem install moomerman-twitter_oauth

Run:

./sinitter.rb

The application should now be available at http://localhost:4567/

Step-by-step

Firstly you need an instance of the Twitter OAuth API client with your credentials.

@client = TwitterOAuth::Client.new(
    :consumer_key => @@config['consumer_key'],
    :consumer_secret => @@config['consumer_secret'],
)

This client handles all the communication and authentication with Twitter. The next stage is to prompt the user to click something on your site that initiates the authorization procedure – imagine this is a link to /connect on your site, this is what the code might look like

get '/connect' do
  request_token = @client.request_token(
    :oauth_callback => ENV['CALLBACK_URL'] || @@config['callback_url']
  )
  session[:request_token] = request_token.token
  session[:request_token_secret] = request_token.secret
  redirect request_token.authorize_url  
end

We have just created a new request token and stored the details of the token in the session for when the user is returned to your site by Twitter. Then we have redirected to the authorize_url that will take the user to the Twitter site.

When you configure your application details on Twitter you have to specify a callback URL. In this example let us assume it is /auth

get '/auth' do
  # Exchange the request token for an access token.
  @access_token = @client.authorize(
    session[:request_token],
    session[:request_token_secret],
    :oauth_verifier => params[:oauth_verifier]
  )
  
  if @client.authorized?
    # Storing the access tokens so we don't have to go back to Twitter again
    # in this session.  In a larger app you would probably persist these details somewhere.
    session[:access_token] = @access_token.token
    session[:secret_token] = @access_token.secret
    session[:user] = true
    redirect '/home'
  else
    redirect '/login'
  end
end

Here we have retrieved the request token details from the session and asked Twitter to confirm the authorization, if this has all gone according to plan you will now have an access token for this user. The access token never expires (unless the user removes your aplication from their settings page) so you can use that in the future without having to do the authorization process again.

To start making authorized requests we can now create an instance of the API client with this users access token

@client = TwitterOAuth::Client.new(
  :consumer_key => @@config['consumer_key'],
  :consumer_secret => @@config['consumer_secret'],
  :token => session[:access_token],
  :secret => session[:secret_token]
)

Now we’re ready to call the API on behalf of this user. Here is an example action that updates the users status on twitter

post '/update' do
  @client.update(params[:update])
  redirect '/'
end

That’s it!