Skip to content

Instantly share code, notes, and snippets.

@steffentchr
Created February 10, 2010 16:33
Show Gist options
  • Select an option

  • Save steffentchr/300511 to your computer and use it in GitHub Desktop.

Select an option

Save steffentchr/300511 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Demonstrates how access tokens/secrets can be retrieved by a non-signed reqest to http://twitter.com/oauth/access_token
# Based on http://github.com/jstewart/yammer4r/blob/master/bin/yammer_create_oauth_yml.rb
# Usage: ./twitter_test.rb -k CONSUMER_KEY -s CONSUMER_SECRET
require 'optparse'
require 'rubygems'
require 'oauth'
OPTIONS = {}
TWITTER_OAUTH = "http://twitter.com"
ARGV.options do |o|
script_name = File.basename($0)
o.set_summary_indent(' ')
o.banner = "Usage: #{script_name} [OPTIONS]"
o.define_head "Create a yaml file for twitter oauth"
o.separator ""
o.separator "[-k] and [-s] options are mandatory"
o.separator ""
o.on("-k", "--key=val", String,
"Consumer key for Twitter app") { |key| OPTIONS[:key] = key}
o.on("-s", "--secret=val", String,
"Consumer secret for Twitter app") { |secret| OPTIONS[:secret] = secret}
o.separator ""
o.on_tail("-h", "--help", "Show this help message.") { puts o; exit }
o.parse!
end
unless OPTIONS[:key] && OPTIONS[:secret]
raise ArgumentError, "Must supply consumer key and secret (use -h for help)"
end
consumer = OAuth::Consumer.new OPTIONS[:key], OPTIONS[:secret], {:site => TWITTER_OAUTH}
request_token = consumer.get_request_token
puts "Please visit the following URL in your browser to authorize your application, then enter the 4 character security code when done: #{request_token.authorize_url}"
oauth_verifier = gets
response = consumer.token_request(consumer.http_method,
(consumer.access_token_url? ? consumer.access_token_url : consumer.access_token_path),
request_token,
{},
:oauth_verifier => oauth_verifier.chomp)
non_signed_consumer = OAuth::Consumer.new OPTIONS[:key], "THIS IS NOT MY SECRET", {:site => TWITTER_OAUTH}
access_token = OAuth::AccessToken.new(non_signed_consumer,response[:oauth_token],response[:oauth_token_secret])
puts "token: #{access_token.token}"
puts "secret: #{access_token.secret}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment