Skip to content

Instantly share code, notes, and snippets.

@hoffrocket
Created May 7, 2009 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoffrocket/108144 to your computer and use it in GitHub Desktop.
Save hoffrocket/108144 to your computer and use it in GitHub Desktop.
converts twitter username and password to oauth access token and secret
require 'rubygems'
require 'twitter'
require 'mechanize'
consumer_token = 'token'
consumer_secret = 'secret'
twitter_name = ARGV[0]
twitter_password = ARGV[1]
oauth = Twitter::OAuth.new(consumer_token,consumer_secret);
a = WWW::Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}
page = a.get( oauth.request_token.authorize_url)
form = page.forms.first
form['session[username_or_email]'] = twitter_name
form['session[password]'] = twitter_password
result = a.submit(form)
if (result.body.include?('Success!') #result will be your own redirect page (Web Apps)
or
result.body.include?("You've successfully granted access to")) #twitter success page (APIs)
oauth.authorize_from_request(oauth.request_token.token, oauth.request_token.secret)
puts "#{twitter_name} has twitter_token=#{oauth.access_token.token}, twitter_secret=#{oauth.access_token.secret}"
else
puts result.body
puts "Failed to authorize #{twitter_name}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment