Created
December 14, 2019 12:09
-
-
Save tilfin/083b1f76fb890364425ff0e53c6c57f5 to your computer and use it in GitHub Desktop.
Get Twitter OAuth AccessToken
This file contains hidden or 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 'uri' | |
require 'oauth' | |
CONSUMER_KEY = '<your consumer key>' | |
CONSUMER_SECRET = '<your consumer secret>' | |
CALLBACK_URL = '<your callback url>' | |
consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, { | |
site: "https://api.twitter.com", | |
scheme: :header | |
}) | |
request_token = consumer.get_request_token(oauth_callback: CALLBACK_URL) | |
req_url = request_token.authorize_url(oauth_callback: CALLBACK_URL) | |
puts <<EOS | |
Go to the following URL and authorize your account on a browser: | |
#{req_url} | |
Copy the redirected url from the browser and paste it: | |
EOS | |
input = STDIN.gets | |
redirected_url = input.chomp | |
rurl = URI.parse(redirected_url) | |
params = {}.tap do |h| | |
URI.decode_www_form(rurl.query).each do |entry| | |
h[entry[0].to_sym] = entry[1] | |
end | |
end | |
access_token = request_token.get_access_token(oauth_verifier: params[:oauth_verifier]) | |
puts <<EOS | |
=================================== | |
user_id: #{access_token.params[:user_id]} | |
screen_name: #{access_token.params[:screen_name]} | |
Access Token: #{access_token.token} | |
Access Token Secret: #{access_token.secret} | |
=================================== | |
EOS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment