Created
November 19, 2013 23:23
-
-
Save gouf/7554426 to your computer and use it in GitHub Desktop.
ユーザからのID/PASS だけでOAuth 通過して、つぶやいてみる
Ref: http://qiita.com/ikedahidenori/items/0088af5aec79b4b0008e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'oauth' | |
require 'oauth/consumer' | |
require 'twitter' | |
require 'mechanize' | |
# Set your Twitter ID/PASS | |
ID = 'id' | |
PASS = 'pass' | |
consumer_key = ENV['TWITTER_CONSUMER_KEY'] # Please set environment. or raw string | |
consumer_secret = ENV['TWITTER_SECRET'] | |
consumer = OAuth::Consumer.new( | |
consumer_key, | |
consumer_secret, | |
{site: 'https://api.twitter.com'} | |
) | |
request_token = consumer.get_request_token | |
uri = request_token.authorize_url | |
a = Mechanize.new {|agent| | |
agent.user_agent_alias = 'Mac Safari' | |
} | |
oauth_action_uri = 'https://api.twitter.com/oauth/authorize' | |
pin_code = '' | |
a.get(uri) do |page| | |
form = page.form_with(action: oauth_action_uri) | |
# fill id/pass field up if not logged in. | |
unless form.field_with(name: 'session[username_or_email]').nil? then | |
form.field_with(name: 'session[username_or_email]').value = ID | |
end | |
unless form.field_with(name: 'session[password]').nil? then | |
form.field_with(name: 'session[password]').value = PASS | |
end | |
button = form.button_with(value: /Authorize app/) # set submit button | |
res = form.submit(button) | |
pin_code = res.search('//*[@id="oauth_pin"]/p/kbd/code').text # set pin code | |
end | |
access_token = request_token.get_access_token(oauth_verifier: pin_code) | |
tw = Twitter::Client.new( | |
consumer_key: consumer_key, | |
consumer_secret: consumer_secret, | |
oauth_token: access_token.token, | |
oauth_token_secret: access_token.secret | |
) | |
tw.update 'tweet' # Update status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment