Skip to content

Instantly share code, notes, and snippets.

@jingkaihe
Created January 9, 2015 11:17
Show Gist options
  • Save jingkaihe/c9ad3f8e1d70005ecbed to your computer and use it in GitHub Desktop.
Save jingkaihe/c9ad3f8e1d70005ecbed to your computer and use it in GitHub Desktop.
require 'yaml'
require 'httparty'
def get_secret
secret_file = File.expand_path('../.secret.yml', __FILE__)
YAML.load_file(secret_file)
end
secret = get_secret
consumer_key_secret = "#{secret["CONSUMER_KEY"]}:#{secret["CONSUMER_SECRET"]}"
encoded_key_secret = Base64.strict_encode64(consumer_key_secret)
headers = {
'Authorization' => "Basic #{encoded_key_secret}",
'Content-Type' => "application/x-www-form-urlencoded;charset=UTF-8"
}
body = 'grant_type=client_credentials'
response = HTTParty.post('https://api.twitter.com/oauth2/token', body: body, headers: headers)
puts response["access_token"] if response.code == 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment