Netflix API Authentication with the Ruby Oauth Gem

For any folks out there struggling to get the Ruby OAuth gem working with Netflix authentication, here’s a quick run down on how to get started.

The 0.2.7 version of Ruby OAuth isn’t totally compatible with the Netflix authentication APIs. However, thanks to the grooviness that it is GitHub, Rob Ares graciously forked the OAuth gem in order to get it working. So clone his repository and get cracking!

Here’s a start:


require "oauth/consumer"
consumer = OAuth::Consumer.new(
      "developer api key",
      "developer api secret",
      {
        :site => "http://api.netflix.com",
        :request_token_url => "https://api-user.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  

 request_token.authorize_url({
      :o auth_consumer_key => "you developer api key",
      :application_name => "application name",
      :o auth_callback => "optional"
    })

    access_token = request_token.get_access_token

    response = consumer.request(
      :get,
      "/users/#{access_token.response[:user_id]}",
      access_token,
      {:scheme => :query_string})

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

One Response to “Netflix API Authentication with the Ruby Oauth Gem”

  1. Lee

    Thanks for this. It’s helpful.

    I’ve got a special problem that I’m stuck on. My users are not coming in via browser, so I can’t redirect them to get an access_token. I think I can get them to do this once during registration and then store their access_token for future use. According to Netflix, it doesn’t expire.

    So, I’m trying to figure out how to replace the calls to authorize_url (not needed) and get_access_token with something that I retrieve from my database, associated with the user.

    The trouble is that I can’t figure out how to use the library to make this work. I’ve tried replacing the get_access_token call with the internal constructor it contains for OAuth::AccessToken, but I’m doing something wrong, because my subsequent requests fail with either a 401 Unauthorized or undefined method `[]‘ for nil:NilClass.

    That second error seems to come from the missing access_token.response[:user_id]. But the OAuth::AccessToken constructor didn’t take that as a parameter, so I’m confused by how it gets populated.

    Can you enlighten me? (Sorry for this long comment. It’s not spam. :-)

Leave a Reply