Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#

Unraveling the Netflix API - Part I - Netflix API Basics

Rate me:
Please Sign up or sign in to vote.
4.40/5 (8 votes)
9 Sep 2009CPOL15 min read 86.2K   1.3K   36  
How to use the Netflix APIs
/*
 * Netflix API Basics
 * Copyright (c) 2009 Dave Cook Consulting, LLC
 *                    http://www.netdave.com
 * 
 * This code released under the Code Project Open License
 * http://www.codeproject.com/info/cpol10.aspx
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace NetflixAPIBasics
{
	public partial class frmMain : Form
	{
		public frmMain()
		{
			InitializeComponent();
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			if (txtKey.Text.Length == 0 || txtSecret.Text.Length == 0)
			{
				MessageBox.Show("You must provide both a Consumer Key and Consumer Secret.");
				return;
			}
			if (txtSearch.Text.Length == 0)
			{
				txtSearch.Text = "<Enter a search title>";
				return;
			}

			OAuth.OAuthBase oauth = new OAuth.OAuthBase();

			// add inputs
			Uri requestUrl = new Uri("http://api.netflix.com/catalog/titles/");
			oauth.AddQueryParameter("term", txtSearch.Text);
			oauth.AddQueryParameter("max_results", udMaxResults.Value.ToString());

			// prepare outputs
			string normalizedUrl;
			string normalizedRequestParameters;

			// generate request signature
			string sig = oauth.GenerateSignature(requestUrl,
												 txtKey.Text, txtSecret.Text,
												 null, null,		// token , tokenSecret (not needed)
												 "GET", oauth.GenerateTimeStamp(), oauth.GenerateNonce(),
												 out normalizedUrl, out normalizedRequestParameters);
			// construct request
			txtRequest.Text = requestUrl + "?" +
							normalizedRequestParameters +
							"&oauth_signature=" + oauth.UrlEncode(sig);
			// make request
			string results = "";
			Cursor.Current = Cursors.WaitCursor;
			try
			{
				WebRequest req = WebRequest.Create(txtRequest.Text);
				WebResponse rsp = req.GetResponse();
				StreamReader sr = new StreamReader(rsp.GetResponseStream());
				results = sr.ReadToEnd();
				rsp.Close();
			}
			catch (Exception ex)
			{
				txtRequest.Text = "Request failed: " + ex.Message;
				Cursor.Current = Cursors.Default;
				return;
			}

			// display the results
			string fna = "Results.xml";
			StreamWriter sw = new StreamWriter(fna);
			sw.Write(results);
			sw.Close();
			// webBrowser ctl doesn't know where we're executing
			int pathLen = Application.ExecutablePath.LastIndexOf('\\');
			string path = Application.ExecutablePath.Substring(0, pathLen + 1);
			webResults.Navigate(path + fna);
			Cursor.Current = Cursors.Default;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Dave Cook Consulting, LLC
United States United States
I am a programmer/writer, specializing in developing SDKs, and work for a rather well known software company in Redmond, WA. More information than you could possibly be interested in knowing can be found at NetDave.com.

I'm also a ham radio aficionado, holding the callsign WAØTTN.

Comments and Discussions