{"id":342,"date":"2011-03-17T20:24:08","date_gmt":"2011-03-18T03:24:08","guid":{"rendered":"http:\/\/blog.monnet-usa.com\/?p=342"},"modified":"2011-03-17T20:24:08","modified_gmt":"2011-03-18T03:24:08","slug":"leverage-twitter-features-in-your-ruby-application-with-twitter4r","status":"publish","type":"post","link":"https:\/\/blog.monnet-usa.com\/?p=342","title":{"rendered":"Leverage Twitter Features In Your Ruby Application With Twitter4R"},"content":{"rendered":"<style>\n\t\t\t\t\t\th3 {\ttext-decoration: underline; }<\/p>\n<p>\t\t\t\t\t\th5 \n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmargin:0 50px 0 50px;\n\t\t\t\t\t\t}<\/p>\n<p>\t\t\t\t\t\t.download_panel\n\t\t\t\t\t\t{\n\t\t\t\t\t\tbackground-color: #EDD6AD; \n\t\t\t\t\t\tcolor: #555555; \n\t\t\t\t\t\tfont-weight:bold;\n\t\t\t\t\t\ttext-align:center;\n\t\t\t\t\t\tmargin:0 50px 0 50px;\n\t\t\t\t\t\tpadding:6px;\n\t\t\t\t\t\t}<\/p>\n<\/style>\n<h3>Intro <\/h3>\n<p>A few years ago I integrated Twitter with a Ruby application, mainly to display a subset of my timeline.<br \/>\n\t\t\t\t\tLater I added the ability to post tweets. All this became possible using <a href='#spbg'>Susan Potter<\/a>&#8216;s &#8220;most-excellent&#8221; <a href='#tw4rgh'>Twitter4R<\/a> gem.\n\t\t\t\t\t<\/p>\n<p>Back in late 2010, Twitter upgraded its API from simple id\/password authentication to <a href='#oan'>OAuth<\/a> authentication. The <a href='#tw4rgh'>Twitter4R<\/a> gem was then also enhanced to leverage the <a href='#oagh'>OAuth-Ruby<\/a> library. I had created a to-do item to go back and upgrade my apps so this past week I decided it was time to document the process in a quick tutorial!<\/p>\n<h3>Twitter4R Approach<\/h3>\n<p>\t\t\t\t\t<img src='.\/wp-content\/media\/twitter4r\/twitter4r-approach.png' width='60%'> <\/p>\n<p>When you register your client application with Twitter, you are assigned an <a>App OAuth Token<\/a> consisting of a <a>consumer key<\/a> and <a>consumer secret<\/a> pair. Your app will configure Twitter4R to use this app token during all API calls with Twitter.<\/p>\n<p>Your app will also need to keep track of <a>User OAuth Tokens<\/a> and pass a specific user token through the API.<br \/>\n\t\t\t\t\tBehind the scene Twitter4R will perform REST calls to the actual Twitter API using JSON data.<\/p>\n<p><p>If you only want your app to access your own account, you will create an Access Token for your Twitter account on your newly-registered app<\/p>\n<h3>Playing With Twitter4R In The Console<\/h3>\n<h5>Installing Twitter4R<\/h5>\n<p>From a command window \/ shell, just run:<\/p>\n<pre class=\"brush: ruby\">\r\ngem install oauth\r\n<\/pre>\n<p>Notice that both the Twitter4R and the OAuth gems are installed.<\/p>\n<p>\t\t\t\t\t<img src='.\/wp-content\/media\/twitter4r\/twitter4r-install.png' > <\/p>\n<h5>Registering Our App With Twitter<\/h5>\n<p>To obtain a <a>consumer key and secret pair<\/a>, we&#8217;ll need to register our application with Twitter at <a href='https:\/\/dev.twitter.com\/apps'>https:\/\/dev.twitter.com\/apps<\/a>. We will need to provide the following:<\/p>\n<ol>\n<li>The name of our app (which must be unique within Twitter&#8217;s app registry)<\/li>\n<li>A description<\/li>\n<li>The base url of our app<\/li>\n<li>The url to redirect authenticated users to in our app<\/li>\n<li>The type of access needed: read or read\/write (if we want to be able to update our status)<\/li>\n<\/ol>\n<p>\t\t\t\t\t\t<span>Example:<\/span><br \/>\n\t\t\t\t\t\t<img src='.\/wp-content\/media\/twitter4r\/twitter4r-registration.png' > <\/p>\n<h5>Obtaining Your Own User OAuth Access Token<\/h5>\n<p>Once you have registered the app, if you click on the &#8220;My Access Token&#8221; button, Twitter will give your OAuth user key and secret token.<br \/>\n\t\t\t\t\t<span>Example:<\/span><br \/>\n\t\t\t\t\t<img src='.\/wp-content\/media\/twitter4r\/twitter4r-mytoken.png' > <\/p>\n<p>In another section we&#8217;ll see how to authorize a user and get its access token programmatically.<\/p>\n<h5>Testing Twitter4R In IRB<\/h5>\n<p>Let&#8217;s start a Ruby console using IRB and load Twitter4R:<\/p>\n<pre class=\"brush: ruby\">\r\ngem 'twitter4r'\r\nrequire 'twitter'\r\nrequire 'twitter\/console'\r\n <\/pre>\n<p>Now we can configure the Twitter4R client using the Twitter::Client.configure class method.<br \/>\n\t\t\t\t\tThe block allows us to customize the application and its app OAuth token. Evaluate the following code snippet:<\/p>\n<pre class=\"brush: ruby\">\r\nTwitter::Client.configure do |conf|\r\n   # App configuration\r\n   conf.application_name = 'BeaconPulse)))'\r\n   conf.application_version = '1.0'\r\n   conf.application_url = 'http:\/\/beaconpulse.heroku.com\/'\r\n\r\n   #  App OAuth token key\/secret pair\r\n   conf.oauth_consumer_token = \"***YourAppConsumerKeyFromYourTwitterAppRegistration***\"\r\n   conf.oauth_consumer_secret = \"***YourAppConsumerSecretFromYourTwitterAppRegistration***\"\r\nend\r\n<\/pre>\n<p>We can then inspect the resulting configuration using:<\/p>\n<pre class=\"brush: ruby\">\r\ncfg = Twitter::Client.config\r\n <\/pre>\n<h5>Instantiating A Twitter4RClient<\/h5>\n<p>We&#8217;ll use the user key\/secret pair we looked up on the Twitter Access Token page earlier:<\/p>\n<pre class=\"brush: ruby\">\r\nclient = Twitter::Client.new(:oauth_access => \r\n { \t:key \t=> \"***YourUserOAuthAccessTokenKey***\", \r\n\t:secret => \"***YourUserOAuthAccessTokenPassword***\"\r\n })\r\n <\/pre>\n<h5>Taking Twitter Features For A Spin<\/h5>\n<p>Now that we have a client instance, we can exercise its various methods (see <a href='http:\/\/twitter4r.rubyforge.org\/rdoc\/classes\/Twitter\/Client.html'>rdoc<\/a> for details) such as:<\/p>\n<ul>\n<li><a href='http:\/\/twitter4r.rubyforge.org\/rdoc\/classes\/Twitter\/Client.html#M000048'>user<\/a> information about a given account:\n<pre class=\"brush: ruby\">\r\ni = client.user 'techarch'\r\n <\/pre>\n<\/li>\n<li><a href='http:\/\/twitter4r.rubyforge.org\/rdoc\/classes\/Twitter\/Client.html#M000049'>my<\/a> info \/ friends \/ followers:\n<pre class=\"brush: ruby\">\r\nfr = client.my :friends\r\nfo = client.my :followers\r\n <\/pre>\n<\/li>\n<li><a href='http:\/\/twitter4r.rubyforge.org\/rdoc\/classes\/Twitter\/Client.html#M000035'>timeline_for<\/a> me \/ user \/ public \/ friends \/ :\n<pre class=\"brush: ruby\">\r\ntm = client.timeline_for :me\r\ntm1 = client.timeline_for :me, :count => 1 # my last tweet\r\n\r\ntu = client.timeline_for :user, :id => 'susanpotter'\r\ntp = client.timeline_for :public\r\ntf = client.timeline_for :friends\r\n <\/pre>\n<\/li>\n<li>post a <a href='http:\/\/twitter4r.rubyforge.org\/rdoc\/classes\/Twitter\/Client.html#M000045'>status<\/a>:\n<pre class=\"brush: ruby\">\r\nt = client.status :post, 'Posting tweets from IRB using the Twitter4R gem rocks!'\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>\t\t\t\t\tAs you can see you can have a lot of fun with the client!\n\t\t\t\t\t<\/p>\n<h3>Authorizing A User And Getting Its Access Token<\/h3>\n<p>In an earlier section, we just looked up our own access token on the Twitter page for our app registration.<br \/>\n\t\t\t\t\tBut to allow any user to access our app we would need to:<\/p>\n<ol>\n<li>Instantiate an <a>OAuth Consumer<\/a> (like Twitter4R does behind the scenes) with our app consumer key\/secret pair:\n<pre class=\"brush: ruby\">\r\n# we reuse the cfg variable we set earlier since it contains our app configuration\r\nk = cfg.oauth_consumer_token\r\ns = cfg.oauth_consumer_secret\r\nu = \"#{(cfg.protocol == :ssl ? :https : cfg.protocol).to_s}:\/\/#{cfg.host}:#{cfg.port}\"\r\n\r\noc = OAuth::Consumer.new(k ,s, :site=> u)\r\n<\/pre>\n<\/li>\n<li>Ask the OAuth consumer to:\n<ul>\n<li>create an <a>OAuth RequestToken<\/a> (a temporary credential token used to authenticate our consumer app on Twitter)<\/li>\n<li>and give us an authorization url:<\/li>\n<\/ul>\n<pre class=\"brush: ruby\">\r\nrt = oc.get_request_token\r\nauth_url = rt.authorize_url\r\n<\/pre>\n<p>\t\t\t\t\t\t\tWe can actually paste this url in a browser.<\/p>\n<\/li>\n<li>Redirect the user to the authorization url so that Twitter can prompt for authorization to access our app\n<\/li>\n<li>Once authorized, get the <a>OAuth Verifier<\/a> (a verification code which will be passed back to Twitter in subsequent requests)\n<\/li>\n<li>User the Request Token to retrieve an <a>OAuth AccessToken<\/a> (the actual authorized user token needed for all future API calls to Twitter) for the user based on the OAuth Verifier:\n<pre class=\"brush: ruby\">\r\nat = rt.get_access_token(:oauth_verifier=>'***ThePINnumberReturnedByTwitter***')\r\n\r\nuser_key = at.token\r\nuser_secret = at.secret\r\n <\/pre>\n<\/li>\n<li>Optionally store the user OAuth key\/secret pair so we can instantiate a Twitter4R Client to invoke APIs on the user&#8217;s behalf:\n<pre class=\"brush: ruby\">\r\nclient = Twitter::Client.new(:oauth_access => { :key => user_key, :secret => user_secret })\r\ninfo = client.user 'techarch'\r\n <\/pre>\n<\/li>\n<\/ol>\n<h3>Approach For Integrating Twitter4R In Your App<\/h3>\n<p>Now that you have a handle on the basics using the Interactive Ruby Console, you can check out the full <a href='http:\/\/twitter4r.rubyforge.org\/rdoc\/'>RDoc<\/a><br \/>\n\t\t\t\t\tand add Twitter support to your favorite Ruby app. In a future blog post I&#8217;ll cover the integration approach for a Ruby Camping web app.<\/p>\n<p><span>Here is what the Twitter4R ecosystem looks like from 50,000 feet:<\/span><br \/>\n\t\t\t\t\t<img src='.\/wp-content\/media\/twitter4r\/twitter4r-yuml.png' > <\/p>\n<p><\/p>\n<h3>So What? <\/h3>\n<p>Since Twitter has moved to <a href='#oan'>OAuth<\/a> you need a robust and efficient library like <a href='#tw4rgh'>Twitter4R<\/a> to integrate Twitter in your application.<br \/>\n\t\t\t\t\t<a href='#tw4rgh'>Twitter4R<\/a> leverages the Twitter REST API using the JSON format to limit bandwith consumption.<br \/>\n\t\t\t\t\tThe gem is &#8220;framework-neutral&#8221;, so it can be used in pure Ruby apps, as well as on top of frameworks such as Rails, <a href='#rc'>Ruby-Camping<\/a>, and Sinatra.<br \/>\n\t\t\t\t\tIf you have not experimented with <a href='#tw4rgh'>Twitter4R<\/a>, hopefully this tutorial will get you started.<\/p>\n<p>\t\t\t\t\t<a name=\"referencesandresources\" ><\/a><\/p>\n<h3>References and Resources<\/h3>\n<h5>Twitter4R<\/h5>\n<ul>\n<li><a name='tw4rgh' href='https:\/\/github.com\/twitter4r\/twitter4r-core'>Twitter4R on GitHub<\/a><\/li>\n<li><a name='spbg' href='http:\/\/geek.susanpotter.net\/'>Susan Potter&#8217;s blog<\/a><\/li>\n<li><a name='tw4rsh' href='http:\/\/www.slideshare.net\/mbbx6spp\/twitter4r-oauth'>&#8220;Configuring Your Twitter4R App With OAuth&#8221; by Susan Potter &#8211; on SlideShare<\/a><\/li>\n<\/ul>\n<h5>OAuth And My Other Related Posts:<\/h5>\n<ul name=\"myotherrelatedposts\">\n<li><a name='oan' href='http:\/\/oauth.net\/'>OAuth.net site<\/a><\/li>\n<li><a name='oau' href='http:\/\/hueniverse.com\/'>OAuth Universe site<\/a><\/li>\n<li><a name='oagh' href='https:\/\/github.com\/oauth\/oauth-ruby'>OAuth Ruby on GitHub<\/a><\/li>\n<li><a href='https:\/\/blog.monnet-usa.com\/?p=293'>Transform Your Ruby Camping Web App Into An OAuth Provider (explains the OAuth &#8220;dance&#8221;!)<\/a><\/li>\n<li><a name='rc' href='http:\/\/github.com\/camping\/camping'>Ruby Camping Framework<\/a><\/li>\n<\/ul>\n<div class=\"blog-msm-ad\">\n<div class=\"blog-msm-ad1\">\n\t\t\t\t\t\t\tIf you enjoyed this post, I would love it if you could check out <a href=\"http:\/\/bit.ly\/myskillsmap\">mySkillsMap<\/a>, my skills management app.\n\t\t\t\t\t\t<\/div>\n<div class=\"blog-msm-ad2\">\n\t\t\t\t\t\t\t\t<a href=\"http:\/\/www.myskillsmap.com\/take-charge-of-your-skills-development\"><br \/>\n\t\t\t\t\t\t\t\t\t<img src='.\/wp-content\/media\/msm\/start-your-free-trial-now.png'  \/><br \/>\n\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t<\/div>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Intro A few years ago I integrated Twitter with a Ruby application, mainly to display a subset of my timeline. Later I added the ability to post tweets. All this became possible using Susan Potter&#8216;s &#8220;most-excellent&#8221; Twitter4R gem. Back in late 2010, Twitter upgraded its API from simple id\/password authentication to OAuth authentication. The Twitter4R [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,3,44,45],"tags":[],"class_list":["post-342","post","type-post","status-publish","format-standard","hentry","category-oauth","category-ruby","category-twitter","category-twitter4r"],"_links":{"self":[{"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=\/wp\/v2\/posts\/342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=342"}],"version-history":[{"count":10,"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=\/wp\/v2\/posts\/342\/revisions"}],"predecessor-version":[{"id":352,"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=\/wp\/v2\/posts\/342\/revisions\/352"}],"wp:attachment":[{"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.monnet-usa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}