Skip to content

Instantly share code, notes, and snippets.

@mojodna
Forked from boscomonkey/oauth_Netflix_example.rb
Created May 7, 2009 15:08
Show Gist options
  • Save mojodna/108149 to your computer and use it in GitHub Desktop.
Save mojodna/108149 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -rubygems
require 'oauth'
require 'optparse'
options = {}
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] <query>"
opts.on("--app-name NAME", "Specifies the name of the applications.") do |v|
options[:app_name] = v
end
opts.on("--consumer-key KEY", "Specifies the consumer key to use.") do |v|
options[:consumer_key] = v
end
opts.on("--consumer-secret SECRET", "Specifies the consumer secret to use.") do |v|
options[:consumer_secret] = v
end
opts.on("--token TOKEN", "Specifies the access token to use.") do |v|
options[:oauth_token] = v
end
opts.on("--secret SECRET", "Specifies the access token secret to use.") do |v|
options[:oauth_token_secret] = v
end
end
option_parser.parse!
if options[:consumer_key].nil? || options[:consumer_secret].nil? || options[:app_name].nil?
puts option_parser.help
exit 1
end
# need to define methods: app_name, developer_key, developer_secret
consumer = OAuth::Consumer.new \
options[:consumer_key],
options[:consumer_secret],
:site => "http://api.netflix.com",
:request_token_url => "http://api.netflix.com/oauth/request_token",
:access_token_url => "http://api.netflix.com/oauth/access_token",
:authorize_url => "https://api-user.netflix.com/oauth/login"
if options[:oauth_token].nil? || options[:oauth_token_secret].nil?
# no token and secret were provided, get the user to authorize the app
request_token = consumer.get_request_token
authorize_url = request_token.authorize_url \
:oauth_consumer_key => options[:consumer_key],
:application_name => options[:app_name]
puts "Please visit this url to authorize:"
puts authorize_url
puts "Press return to continue..."
gets
# TODO this is returning a 401
access_token = request_token.get_access_token
puts "For future reference:"
puts "Access token: #{access_token.token}"
puts "Token secret: #{access_token.secret}"
else
access_token = OAuth::AccessToken.new \
consumer,
options[:oauth_token],
options[:oauth_token_secret]
end
# XML
response = access_token.get("/users/#{access_token.params[:user_id]}")
xml = response.body
puts "XML response:"
puts response.body
# JSON
response = access_token.get("/users/#{access_token.params[:user_id]}?output=json")
json = response.body
puts "JSON response:"
puts response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment