Skip to content

Instantly share code, notes, and snippets.

@areichman
Created March 16, 2012 03:53
Show Gist options
  • Save areichman/2048447 to your computer and use it in GitHub Desktop.
Save areichman/2048447 to your computer and use it in GitHub Desktop.
Some Ruby snippets I wrote to learn how to use OAuth and the SmugMug API
#!/usr/bin/ruby
# based on http://blog.andydenmark.com/2009/03/how-to-build-oauth-consumer.html
require 'rubygems'
require 'digest'
require 'base64'
require 'cgi'
require 'openssl'
require 'restclient'
require 'json'
### Get request token
### -----------------
puts " getRequestToken:\n"
request_token_url = "http://api.smugmug.com/services/oauth/getRequestToken.mg"
params = {}
# token and secret, provided by smugmug
params['oauth_consumer_key'] = ""
params['oauth_consumer_secret'] = ""
# "non-secret" params
s = ''; 40.times { s += (0..9).to_a.choice.to_s }
params['oauth_nonce'] = Digest::MD5.hexdigest(Time.now.to_i.to_s + s.to_s)
params['oauth_timestamp'] = Time.now.to_i
params['oauth_signature_method'] = "HMAC-SHA1"
params['oauth_version'] = "1.0"
# normalize the request parameters
sorted_params = params.sort.collect{|p| "#{p[0]}=#{p[1]}"}.join('&')
# construct the signature base string
signature_base_string = ["GET", CGI.escape(request_token_url), CGI.escape(sorted_params)].join('&')
# construct the oauth signature
params['oauth_signature'] = Base64.encode64(OpenSSL::HMAC.digest('sha1', "#{params['oauth_consumer_secret']}&", signature_base_string)).chomp
# get the request token
response = RestClient.get request_token_url, {:params => params}
request_token = {}
response.split('&').each do |a|
k,v = a.split
request_token[k] = v
end
puts " #{request_token}\n"
### Authorize request token
### _______________________
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment