Using the Netflix API
Dec 08 2008 10:02 PM Filed in: .NET
The project I am currently working on makes use of
the Netflix API to retrieve
information about titles and displays the box
art. It took me a while to get it working,
mainly because I was trying to do too much. I
figured I’d share this information, in case you
also are going about using the API in the wrong
way. There are two ways to use the API. First,
you use it to make simple calls to their RESTful
web services. Second, you are making a front-end
of some sort that signs in as the user and lets
them manipulate their queue, browse the library,
or watch on demand streams. For our application
I needed to go the first route, but was coding
it the second way, which was overkill, and was
causing me major problems. To perform a simple
Title search, or to do a daily download of their
entire index, you don’t need to sign in as a
Netflix user, you just need to authenticate
using Open Authentication with your consumer key
and consumer secret (which you will need to
apply for on their developer site).
Some kind developer was nice enough to write a simple class to help you with this. You can view the code to his implementation, OAuthBase, here. Add this class to your application, and then it’s as simple as the following code to perform a title search.
Believe me, this is so much simpler than what I started out doing. I was following their examples and I was connecting to their site through my application, bringing up a Netflix login screen, having to add my application to a trusted application list, etc. I wasted a couple days development before I realized that was only needed if I wanted to let users interact with Netflix, not just do a title search.
Some kind developer was nice enough to write a simple class to help you with this. You can view the code to his implementation, OAuthBase, here. Add this class to your application, and then it’s as simple as the following code to perform a title search.
private void Search()
{
var url = new
Uri(“http://api.netflix.com/catalog/titles?term=” +
titleName);
var consumerKey =
ConfigurationSettings.AppSettings[“ConsumerKey”];
var consumerSecret =
ConfigurationSettings.AppSettings[“ConsumerSecret”];
string normalizedUrl;
string
normalizedRequestParameters;
var oauth = new OauthBase();
var signature =
oauth.GenerateSignature(
url,
consumerKey, consumerSecret,
null,
null, “GET”, oauth.GenerateTimeStamp(),
oauth.GenerateNonce(),
out
normalizedUrl, out normalizedRequestParameters);
var finalUri = normalizedUrl
+ “?” + normalizedRequestParameters +
“&oauth_signature=” +
oauth.UrlEncode(signature);
// Call the service to
perform the title search
var xmlDocument = new
XmlDocument();
xmlDocument.Load(finalUri);
}
Believe me, this is so much simpler than what I started out doing. I was following their examples and I was connecting to their site through my application, bringing up a Netflix login screen, having to add my application to a trusted application list, etc. I wasted a couple days development before I realized that was only needed if I wanted to let users interact with Netflix, not just do a title search.
|