Using Ruby oauth gem to access Netflix
The example at Mandarin Soda didn't work for the latest version of the Ruby oauth gem (version 0.3.2.2 as of this writing). Took me a day to work out all the differences. The slightly alter code is a gist on GitHub (and reproduced here).
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 'oauth/consumer' | |
# need to define methods: app_name, developer_key, developer_secret | |
consumer = OAuth::Consumer.new(developer_key, | |
developer_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") | |
request_token = consumer.get_request_token | |
# (optional) :oauth_callback => redirect_url | |
url = request_token.authorize_url(:oauth_consumer_key => developer_key, | |
:application_name => app_name) | |
`firefox -url "#{url}"` | |
# wait until you're done with firefox; otherwise, access token will be nil | |
access_token = request_token.get_access_token | |
# xml | |
response = access_token.get "/users/#{access_token.params[:user_id]}" | |
xml = response.body | |
# JSON | |
response = access_token.get "/users/#{access_token.params[:user_id]}?output=json" | |
json = response.body |
- :request_token_url is http instead of https
- access_token.response is a subclass of Net::HTTPResponse so there's no access_token.response[:user_id]; rather, you get the user ID with access_token.params[:user_id]
- using access_token.get(url) instead of consumer.request(:get, url, access_token, {:scheme => :query_string}) but I think they're the same (and the latter still works)

0 Comments:
Post a Comment
<< Home