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"}
Advertisements

5 Comments »

  1. […] This post was mentioned on Twitter by Ruby on rails Guru, Veerasundaravel. Veerasundaravel said: Twitter connection in Rails using twitter_oauth gem: http://wp.me/pkAKm-7y […]

    Pingback by Tweets that mention Twitter connection in Rails using twitter_oauth gem « Veerasundaravel's Ruby on Rails Weblog -- Topsy.com — November 26, 2010 @ 9:28 pm

  2. hello I was fortunate to come cross your topic in digg
    your post is impressive
    I learn a lot in your topic really thanks very much
    btw the theme of you blog is really quality
    where can find it

    Comment by bet365 — November 30, 2010 @ 11:20 am

    • Theme name is ‘Silver is the New Black’. But I can’t find this theme in wordpress theme listing.

      Comment by veerasundaravel — November 30, 2010 @ 11:58 am

  3. Thanks for the tutorial dude….. successfully implemented in my application…

    Comment by Saravanan — December 28, 2010 @ 3:12 pm

  4. The device is also powered with 1500 m – Ah battery which fortunately is removable and deliver 6 hours 25
    mintutes talktime on 2G or 6 hours 18 minutes talktime on 3G and also a standby time of 530 hours.
    T-mobile’s plan will knock you back. Tower Blocks New York from Digital Chocolate.

    Comment by Sony Xperia Tipo ST21i2 Dual — January 29, 2013 @ 1:36 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: