Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Last active August 29, 2015 14:16
Show Gist options
  • Save ifyouseewendy/58b46e4b660496604138 to your computer and use it in GitHub Desktop.
Save ifyouseewendy/58b46e4b660496604138 to your computer and use it in GitHub Desktop.
HOST = ""
APP_KEY = ""
APP_SECRET = ""
REQUEST_TOKEN = ""
REQUEST_TOKEN_SECRET = ""
REQUEST_TOKEN_VERIFIER = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
require 'cgi'
require 'base64'
require 'securerandom'
require 'openssl'
require 'net/http'
require 'dotenv'
Dotenv.load
##
# Assemble parameters
#
oauth_consumer_key = ENV['APP_KEY']
oauth_nonce = SecureRandom.hex
oauth_signature_method = 'HMAC-SHA1'
oauth_timestamp = Time.now.to_i.to_s
oauth_version = '1.0'
# alphabetic params
params = {
oauth_consumer_key: oauth_consumer_key,
oauth_nonce: oauth_nonce,
oauth_signature_method: oauth_signature_method,
oauth_timestamp: oauth_timestamp,
oauth_version: oauth_version
}
parameters = params.map{|k,v| "#{k}=#{CGI.escape(v)}"}.join("&")
puts "--> Parameters:\n#{parameters}"
##
# Generate signature
#
host = ENV['HOST']
url = "#{host}/oauth/request_token"
base_string = ['GET', CGI.escape(url), CGI.escape(parameters) ].join('&')
secret_key = "#{ENV['APP_SECRET']}&"
oauth_signature = CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1', secret_key, base_string)}").chomp)
puts "--> Signature:\n#{oauth_signature}"
##
# Make request
#
request_url = url + '?' + parameters + '&oauth_signature=' + oauth_signature
puts "--> Request URL:\n#{request_url}"
response = Net::HTTP.get URI(request_url)
readable_hash_of = ->(str){str.split('&').reduce({}){|h, s| w = s.split('='); h[w[0]] = w[1]; h} }
response_ha = readable_hash_of[response]
puts "--> Response:\n#{response_ha}"
puts "--> Authorize URL:\n#{host}/oauth/authorize_token?oauth_token=#{response_ha['oauth_token']}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment