Skip to content

Instantly share code, notes, and snippets.

@techarch
Created June 10, 2010 05:04
Show Gist options
  • Save techarch/432578 to your computer and use it in GitHub Desktop.
Save techarch/432578 to your computer and use it in GitHub Desktop.
# These are the basic steps to use the twitter gem from @jnunemaker
#
# 1. Install the gem: gem install twitter
#
# 2. Login to Twitter and register your application.
#
# Note: it looks like you cannot use a url like http://locahost:3301/ (bummer)
# An easy alternative is to create an app on Heroku.
#
# IMPORTANT: Make sure that the callback url looks like this:
# http://yourapp.yoursite.com/authorization
# And make sure to select Read and Write otherwise changing it later
# will cause: (401): Unauthorized - Read-only application cannot POST
# and you will need to delete the app and re-register it!
#
# 3. Open a command window/shell, start IRB, and require the twitter gem
#
gem 'twitter'
require 'twitter'
# 4. Set a variable to store your consumer key
ck = '*** paste your consumer key here ***'
# 5. Set a variable to store your consumer secret
cs = '*** paste your consumer secret here ***'
# 6. Let's define an OAuth consumer
consumer = OAuth::Consumer.new(tk, ts, {:site => 'http://twitter.com'})
# 7. Now let's request an OAuth token and save it
rt = consumer.get_request_token
# 8. Let's find out the OAuth authorization url for Twitter for our request token:
au = rt.authorize_url
# 9. Copy paste the result (e.g. http://api.twitter.com/oauth/authorize?oauth_token=****YOUR*OAUTH*VERIFIER*** ) in a browser
# 10. Twitter will prompt you to authorize access for your app and give you a PIN.
# Save it off for later use.
# 11. Let's create an OAuth Access Token
at = OAuth::RequestToken.new(consumer, rt.token, rt.secret)
# 12. Let's instantiate a Twitter::OAuth object based on our API key and secret
oauth = Twitter::OAuth.new(ck,cs)
# 13. Now we need to authorize our OAuth Access Token:
aa = oauth.authorize_from_request(at.token, at.secret, '*YOUR*PIN*')
# 14. And we can finally instantiate the Twitter client!
client = Twitter::Base.new(oauth)
# 15. Let's get the user timeline and mentions
client.user_timeline
client.mentions
# 16. And to top it off let's post a status update:
client.update 'Yippee! Twitting this from Ruby IRB using @jnunemaker twitter gem! :-) '
# Enjoy - @techarch on Twitter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment