Skip to content

Instantly share code, notes, and snippets.

@carlhoerberg
Forked from cheenu/netflix_api_sample_signed_request.rb
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlhoerberg/3a357c300a39efdd2453 to your computer and use it in GitHub Desktop.
Save carlhoerberg/3a357c300a39efdd2453 to your computer and use it in GitHub Desktop.
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