Last active
December 29, 2020 07:18
-
-
Save roana0229/9c3794c6d1a5a27bff947220873df92f to your computer and use it in GitHub Desktop.
Generate 'oauth_signature' from OAUTH_CONSUMER_KEY, CONSUMER_API_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET, and make a request.
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
# e.g.: OAUTH_CONSUMER_KEY=value CONSUMER_API_SECRET=value OAUTH_TOKEN=value OAUTH_TOKEN_SECRET=value ruby api_request_twitter.rb | jq . | |
# OAUTH_CONSUMER_KEY = Consumer API keys - API key | |
# CONSUMER_API_SECRET = Consumer API keys - API secret key | |
# OAUTH_TOKEN = Access token & access token secret - Access token | |
# OAUTH_TOKEN_SECRET = Access token & access token secret - Access token secret | |
require "erb" | |
include ERB::Util | |
require 'uri' | |
require 'securerandom' | |
require 'openssl' | |
require 'base64' | |
params = { | |
'oauth_consumer_key' => ENV['OAUTH_CONSUMER_KEY'], | |
'oauth_nonce' => "#{SecureRandom.alphanumeric(42)}", | |
'oauth_timestamp' => Time.now.to_i.to_s, | |
'oauth_signature_method' => 'HMAC-SHA1', | |
'oauth_token' => ENV['OAUTH_TOKEN'], | |
'oauth_version' => '1.0', | |
} | |
# e.g.: GET request | |
method = 'GET' | |
endpoint = 'https://api.twitter.com/1.1/search/tweets.json' | |
query = 'q=hoge&result_type=recent' | |
# e.g.: POST request | |
# method = 'POST' | |
# endpoint = 'https://api.twitter.com/1.1/statuses/update.json' | |
# query = "status=#{ERB::Util.url_encode('hello,world')}" | |
query.split('&').each { |q| | |
params[q.split('=')[0]] = URI.decode(q.split('=')[1]) | |
} | |
encoded_params = params.keys.sort.map { |item| "#{ERB::Util.url_encode(item)}=#{ERB::Util.url_encode(params[item])}" }.join('&') | |
oauth_base = "#{method}&#{ERB::Util.url_encode(endpoint)}&#{ERB::Util.url_encode(encoded_params)}" | |
STDERR.puts '--------------------------------' | |
STDERR.puts "oauth_base" | |
STDERR.puts oauth_base | |
encoded_consumer_api_secret = ERB::Util.url_encode(ENV['CONSUMER_API_SECRET']) | |
encoded_oauth_token_secret = ERB::Util.url_encode(ENV['OAUTH_TOKEN_SECRET']) | |
oauth_key = "#{encoded_consumer_api_secret}&#{encoded_oauth_token_secret}" | |
STDERR.puts '--------------------------------' | |
STDERR.puts "oauth_key" | |
STDERR.puts oauth_key | |
params['oauth_signature'] = ERB::Util.url_encode(Base64.encode64(OpenSSL::HMAC.digest('sha1', oauth_key, oauth_base)).chomp) | |
STDERR.puts '--------------------------------' | |
STDERR.puts "params" | |
STDERR.puts params | |
header = params.keys.select { |item| | |
['oauth_consumer_key', 'oauth_nonce', 'oauth_timestamp', 'oauth_signature_method', 'oauth_token', 'oauth_version', 'oauth_signature'].include?(item) | |
}.sort.map { |item| "#{item}=\"#{params[item]}\"" }.join(', ') | |
STDERR.puts '--------------------------------' | |
STDERR.puts "header" | |
STDERR.puts header | |
curl_str = "curl -v -X #{method} --url '#{endpoint}#{query != '' ? "?#{query}" : ''}' --header 'authorization: OAuth #{header}'" | |
STDERR.puts '--------------------------------' | |
STDERR.puts "$ #{curl_str}" | |
STDERR.puts '================================' | |
puts `#{curl_str}` | |
STDERR.puts '================================' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment