Forked from cheenu/netflix_api_sample_signed_request.rb
Last active
August 29, 2015 14:00
-
-
Save carlhoerberg/3a357c300a39efdd2453 to your computer and use it in GitHub Desktop.
This file contains 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 'cgi' | |
require 'base64' | |
require 'openssl' | |
def oauth_url(url) | |
parameters = ["oauth_consumer_key=#{ENV.fetch 'OAUTH_KEY'}", | |
"oauth_nonce=#{Random.rand(100000000).to_s}", | |
"oauth_signature_method=#{'HMAC-SHA1'}", | |
"oauth_timestamp=#{Time.now.to_i.to_s}", | |
"oauth_version=1.0"].join('&') | |
base_string = ['GET', CGI.escape(url), CGI.escape(parameters)].join '&' | |
## Cryptographic hash function used to generate oauth_signature | |
# by passing the secret key and base string. Note that & has | |
# been appended to the secret key. Don't forget this! | |
secret_key = "#{ENV.fetch('OAUTH_SECRET')}&" | |
oauth_signature = CGI.escape(Base64.encode64(OpenSSL::HMAC.digest('sha1', secret_key, base_string)).chomp) | |
"#{url}?#{parameters}&oauth_signature=#{oauth_signature}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment