Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
yet another OAuth util
require 'rubygems'
require 'oauth'
require 'awesome_print'
require 'uri'
# Usage:
# 1) Get consumer key/secret from https://dev.twitter.com/apps
# 2) Run ruby oauth_util.rb https://api.twitter.com/statuses/home_timeline.json
# 3) Authorize as instructed
# 4) $$$
uri = URI.parse ARGV[0]
consumer = OAuth::Consumer.new(
'key',
'secret',
:site => "#{uri.scheme}://#{uri.host}",
:request_token_path => "/oauth/request_token",
:access_token_path => "/oauth/access_token",
:authorize_path => "/oauth/authorize"
)
storage = './oauth_token'
if File.exists? storage
token, secret = File.read(storage).split(',')
access_token = OAuth::AccessToken.new consumer, token, secret
else
request_token = consumer.get_request_token rescue \
abort("Request token fail! Typo in site name, key, or secret?")
puts "All right!", "1) Go here:", request_token.authorize_url, "2) Paste PIN here:"
code = $stdin.gets.chomp
access_token = request_token.get_access_token :oauth_token_verifier => code
File.open(storage, 'w') {|f| f.write "#{access_token.token},#{access_token.secret}"}
end
# See oauth gem docs for access_token usage: https://github.com/oauth/oauth-ruby
awesome_print access_token.get uri.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment