Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created March 10, 2014 08:05
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save komasaru/9461154 to your computer and use it in GitHub Desktop.
Save komasaru/9461154 to your computer and use it in GitHub Desktop.
Ruby script to get twitter access token from consumer key.
# ***************************************
# Oauth - Access Token, Secret 取得処理
# ***************************************
require 'oauth'
class TwitterAccessToken
SITE_URL = "https://twitter.com"
def get_token
# Consumer 情報入力
print 'Please input CONSUMER KEY : '
consumer_key = gets.chomp
print 'Please input CONSUMER SECRET: '
consumer_secret = gets.chomp
# Oauth オブジェクト生成
oauth = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site: SITE_URL
)
# リクエストトークン取得
request_token = oauth.get_request_token
# PIN コード取得
puts "Please access this URL : #{request_token.authorize_url}"
print "Please enter the PIN code : "
pin_code = gets.to_i
# アクセストークン取得
access_token = request_token.get_access_token(
oauth_verifier: pin_code
)
# アクセストークン表示
puts "---"
puts "ACCESS TOKEN : #{access_token.token}"
puts "ACCESS TOKEN SECRET : #{access_token.secret}"
rescue => e
puts "[#{e.class}] #{e.message}"
exit 1
end
end
TwitterAccessToken.new.get_token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment