Twitter OAuth REST API client library for Ruby

Install the gem

sudo gem install twitter_oauth # (via gemcutter)

Using the gem

To make authorized requests with the client library you’ll need to create a Twitter OAuth Application.

See sinitter for a full website integration example.

Unauthorized request example

The Twitter API can be called to make public requests without needing any client credentials. Most methods will not work in this mode but some of them do. An example to retrieve the public details about a Twitter user is below.

client =  

=> => {"status"=>{"truncated"=>false, "favorited"=>false, "text"=>"Update on service issues", "id"=>1357776683, "in_reply_to_user_id"=>nil, "in_reply_to_status_id"=>nil, "source"=>"<a href=\"\">twitterfeed</a>", "created_at"=>"Fri Mar 20 01:17:35 +0000 2009"}, "name"=>"Twitter", "profile_sidebar_fill_color"=>"CDFFFF", "profile_sidebar_border_color"=>"8a6447", "profile_background_tile"=>false, "profile_link_color"=>"0000ff", "url"=>"", "favourites_count"=>0, "id"=>783214, "description"=>"Always wondering what everyone's doing.", "profile_text_color"=>"000000", "protected"=>false, "utc_offset"=>-28800, "screen_name"=>"twitter", "profile_background_color"=>"9ae4e8", "time_zone"=>"Pacific Time (US & Canada)", "followers_count"=>469150, "profile_background_image_url"=>"", "friends_count"=>30, "statuses_count"=>290, "location"=>"San Francisco, CA", "profile_image_url"=>"", "created_at"=>"Tue Feb 20 14:35:54 +0000 2007"}

You can also access to the search API which is available in either authorized or unauthorized modes.

search ='twitter')
search.results.size => 20
search.results.first.from_user => "josephpred"
=> "Useful public service Twitter account for those of you hitting Tahoe or just needing to cross the pass to Reno: @i80chains"

Authorized request example

To use the full power of the Twitter API you need to authorize your application and a valid Twitter user via OAuth. An example showing how to update the status of an authorized user is below.

Firstly we need to create an instance of the client with your application client credentials you have been given by Twitter when you set up your application.

client =
    :consumer_key => 'YOUR_APP_CONSUMER_KEY',
    :consumer_secret => 'YOURA_APP_CONSUMER_SECRET'
request_token = client.request_token(:oauth_callback => oauth_confirm_url)
#:oauth_callback required for web apps, since oauth gem by default force PIN-based flow 
#( see )


In your application your user would be redirected to Twitter to authorize the application at this point. You’ll need to store the request token (usually in the session) for later. The code continues below assuming the user has authorized your application.

access_token = client.authorize(
  :oauth_verifier => params[:oauth_verifier]

=> true

client.update('checking out the twitter_oauth library') # sends a twitter status update

Now if you keep hold of the access_token (usually in the database) for this user you won’t need to re-authorize them next time. When you create an instance of the client you can just pass in the access token and secret that you have stored.

access_token = @user.access_token # assuming @user
client =
    :consumer_key => 'YOUR_CONSUMER_KEY',
    :consumer_secret => 'YOUR-CONSUMER-SECRET',
    :token => access_token.token, 
    :secret => access_token.secret

=> true