Veerasundaravel's Ruby on Rails Weblog

November 26, 2010

Twitter connection in Rails using twitter_oauth gem

Filed under: Ruby On Rails, Twitter — Tags: , , , , , , — Veerasundaravel @ 5:18 pm

Twitter has introduced OAuth support for authenticating user request to their site. Hence using this OAuth support you can connect your site users with their own twitter profile.

To use the Twitter API, the first thing you have to do is register your application. You can register your application in this page.

You have to provide your Application Name, Home page of your application and Callback Url where your users need to be redirected after authenticating their account in Twitter. But still you can override the callback url in your application level.

After registering your application you can get Consumer key and Consumer Secret values as follows. Please make note of these values, we need these values for talking with Twitter API.

 

Install twitter_oauth gem:

Now we are going to use twitter_oauth gem for connecing with Twitter API. This gem is a Twitter OAuth REST API client library for Ruby.

gem install twitter_oauth

 

Authorized request example:

Using the obtained consumer_key and consumer_secret you can send authorized request to Twitter API as follows. First we need to create a client instance using that consumer keys. Then you can get the access_token by specifying the callback_url with the client instance. And finally you can get the authorize url for your user where they will redirected to twitter site for authentication.

client = TwitterOAuth::Client.new(
    :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 http://groups.google.com/group/twitter-development-talk/browse_thread/thread/472500cfe9e7cdb9/848f834227d3e64d )

request_token.authorize_url
=> http://twitter.com/oauth/authorize?oauth_token=TOKEN
#Link this url to in your view file, so that user will be redirected to the Twitter authentication page.

 

 

Accessing user profile informations:

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(
  request_token.token,
  request_token.secret,
  :oauth_verifier => params[:oauth_verifier]
)

client.authorized?
=> true

client.info["name"]
=> 'twitter-user10'

client.info["profile_image_url"]
=> 'http://a1.twimg.com/profile_images/500391593/dsc00852_normal.jpg'

 

Sending a status update:

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

For further client profile information options refer this page.

Hence you can connect your site-users with their twitter profile using their access_token and access_secret.

 

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 = TwitterOAuth::Client.new  

puts client.show('twitter')
=> {"status"=>{"truncated"=>false, "favorited"=>false, "text"=>"Update on service issues http://tinyurl.com/ca872j", "id"=>1357776683, "in_reply_to_user_id"=>nil,
  "in_reply_to_status_id"=>nil, "source"=>"twitterfeed", "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"=>"http://twitter.com", "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"=>"http://static.twitter.com/images/themes/theme1/bg.gif", "friends_count"=>30, "statuses_count"=>290,
  "location"=>"San Francisco, CA", "profile_image_url"=>"http://s3.amazonaws.com/twitter_production/profile_images/75075164/twitter_bird_profile_normal.png",
  "created_at"=>"Tue Feb 20 14:35:54 +0000 2007"}

The Silver is the New Black Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 120 other followers

%d bloggers like this: