Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 1.46 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env ruby
  2.  
  3. # Command line util for acquiring a one-off Twitter OAuth access token
  4. # Based on http://blog.beefyapps.com/2010/01/simple-ruby-oauth-with-twitter/
  5.  
  6. require 'rubygems'
  7. require 'oauth'
  8.  
  9. puts <<EOS
  10. Set up your application at https://twitter.com/apps/ (as a 'Client' app),
  11. then enter your 'Consumer key' and 'Consumer secret':
  12.  
  13. Consumer key:
  14. EOS
  15. consumer_key = STDIN.readline.chomp
  16. puts "Consumer secret:"
  17. consumer_secret = STDIN.readline.chomp
  18.  
  19. consumer = OAuth::Consumer.new(
  20.         consumer_key,
  21.         consumer_secret,
  22.         {
  23.                 :site => 'http://twitter.com/',
  24.                 :request_token_path => '/oauth/request_token',
  25.                 :access_token_path => '/oauth/access_token',
  26.                 :authorize_path => '/oauth/authorize'
  27.         })
  28.  
  29. request_token = consumer.get_request_token
  30.  
  31. puts <<EOS
  32. Visit #{request_token.authorize_url} in your browser to authorize the app,
  33. then enter the PIN you are given:
  34. EOS
  35.  
  36. pin = STDIN.readline.chomp
  37. access_token = request_token.get_access_token(:oauth_verifier => pin)
  38.  
  39. puts <<EOS
  40. Congratulations, your app has been granted access! Use the following config:
  41.  
  42. TWITTER_CONSUMER_KEY = '#{consumer_key}'
  43. TWITTER_CONSUMER_SECRET = '#{consumer_secret}'
  44. TWITTER_ACCESS_TOKEN = '#{access_token.token}'
  45. TWITTER_ACCESS_SECRET = '#{access_token.secret}'
  46.  
  47. And use the following code to connect to Twitter:
  48.  
  49. require 'twitter'
  50. auth = Twitter::OAuth.new(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
  51. auth.authorize_from_access(TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_SECRET)
  52. client = Twitter::Base.new(auth)
  53.  
  54. EOS