Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using HtmlAgilityPack;
- using System.Web;
- namespace NetflixAPI
- {
- static class OAuth1a
- {
- //gameplan:
- // target URL
- // Netflix params: Title, offset, limit
- // Oauth params: consumer_key, nonce, timestamp, signature_method
- //
- // and then the signature after, which is based on the above encoded
- //
- // HMAC outputs 20 chars, turn that to b64 then encode that
- //
- // NEed to encode certain chars AND make sure to encode it befre I pass
- // it to the MHA or whatever
- public static void Start()
- {
- string VERB = "GET";
- string URL = "http://api-public.netflix.com/catalog/titles";
- //Not going to change
- string consumer_key = "u7r68et24v6rd5r9u828qvte";
- string consumer_secret = "uWdp2AXnnu";
- string oauth_signature_method = "HMAC-SHA1";
- string oauth_version = "1.0";
- //dynamic values
- string oauth_timestamp = GenerateTimeStamp();
- string oauth_nonce = GenerateTimeStamp();
- string oauth_signature = GenerateSignature(VERB, URL,
- consumer_key,
- consumer_secret,
- oauth_timestamp,
- oauth_nonce,
- oauth_signature_method,
- new KeyValuePair
- <string, string>(
- "term", "liar"),
- new KeyValuePair
- <string, string>(
- "start_index", "0"),
- new KeyValuePair
- <string, string>(
- "max_results", "25"));
- Console.WriteLine(oauth_signature);
- //Need to send the request now, will have to learn about headers, probably.
- string authorizationHeaderParams = String.Empty;
- authorizationHeaderParams += "OAuth ";
- authorizationHeaderParams += "oauth_nonce=" + "\"" +
- Uri.EscapeDataString(oauth_nonce) + "\",";
- authorizationHeaderParams +=
- "oauth_signature_method=" + "\"" +
- Uri.EscapeDataString(oauth_signature_method) +
- "\",";
- authorizationHeaderParams += "oauth_timestamp=" + "\"" +
- Uri.EscapeDataString(oauth_timestamp) + "\",";
- authorizationHeaderParams += "oauth_consumer_key="
- + "\"" + Uri.EscapeDataString(
- consumer_key) + "\",";
- //authorizationHeaderParams += "oauth_token=" + "\"" +
- // Uri.EscapeDataString(oauth_token) + "\",";
- authorizationHeaderParams += "oauth_signature=" + "\""
- + Uri.EscapeDataString(oauth_signature) + "\",";
- authorizationHeaderParams += "oauth_version=" + "\"" +
- Uri.EscapeDataString(oauth_version) + "\"";
- //needs the baseurl, then add in all the params, followed by the
- //signature
- string toSend =
- String.Format("{0}?max_results={2}&oauth_consumer_key={3}&oauth_nonce={4}&oauth_signature_method=HMAC-SHA1&oauth_timestamp={5}&oauth_version=1.0&start_index=0&term={1}&oauth_signature={6}",
- URL, "liar", "25", consumer_key, oauth_nonce,
- oauth_timestamp, oauth_signature);
- //get the URL to send, now we need to create ethe request
- //WebRequest web = WebRequest.Create(toSend);
- HttpWebRequest web = (HttpWebRequest)WebRequest.Create(toSend);
- web.KeepAlive = false;
- //add headers, except when I do, I get 403 instead of 401s statuses
- //web.Headers.Add( "Authorization", authorizationHeaderParams);
- Stream objStream;
- objStream = web.GetResponse().GetResponseStream();
- StreamReader objReader = new StreamReader(objStream);
- string sLine = "";
- int i = 0;
- while (sLine!=null)
- {
- i++;
- sLine = objReader.ReadLine();
- if (sLine!=null)
- Console.WriteLine("{0}:{1}",i,sLine);
- }
- Console.ReadLine();
- Console.WriteLine("Done!");
- }
- public static string GenerateTimeStamp()
- {
- double time = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
- return Convert.ToInt32(time).ToString();
- }
- public static string GenerateNonce()
- {
- return Guid.NewGuid().ToString();
- }
- public static string GenerateSignature(string HttpMethod, string url,
- string consumer_key, string consumer_secret, string
- oauth_timestamp, string oauth_nonce, string
- oauth_signature_method= "HMACSHA1", params KeyValuePair<string,
- string>[] parameters)
- {
- //so this needs an encoded base string, which'll be
- //METHOD&URL&PARAMS
- //then I pass it to the HMAC-SHA1 which spits out a 20char, which'll
- //be encoded and used as sig param on the unencoded basestring
- //create BASESTRING where we already know the METHOD and URL, so we
- //need to grab all the params
- // string enc_params = GenerateEncodedParameterString(consumer_key
- // :"consumer_key" ,
- // oauth_signature_method :"oauth_signature_method" ,
- // oauth_version "oauth_version" , oauth_timestamp
- // :"oauth_timestamp" ,
- // oauth_nonce : "oauth_nonce" , parameters);
- //This is a set of params that need to be passed to the paramString method, but the
- // method is expecting an array of KVPs so I need to make an array
- // from the default OAuth args and then add the custom params, like
- // Term and stuff later.
- KeyValuePair<string, string>[] qwe =
- {
- new KeyValuePair<string, string>("oauth_consumer_key", consumer_key),
- new KeyValuePair<string, string>("oauth_signature_method", oauth_signature_method),
- new KeyValuePair<string, string>("oauth_version", "1.0"),
- new KeyValuePair<string, string>("oauth_timestamp", oauth_timestamp),
- new KeyValuePair<string, string>("oauth_nonce", oauth_nonce)
- };
- //Turn the array to a list so you can addrange, then back to an array
- // for the sake of the method which requires it.
- var poop = parameters.ToList();
- poop.AddRange(qwe);
- var a_listed_poop = poop.ToArray();
- string enc_params = GenerateEncodedParameterString(a_listed_poop);
- //Now we've got the encoded param set, we need to encode the uri and prepend the httpmethod.
- //TODO: fix the hardcoded key
- string signString = consumer_key + "&";
- //need this basestring to combine with the signString to make the
- //signature, once we add it to the SHA1 or whatever
- string encodedBaseString = GenerateEncodedBaseString(HttpMethod, url, enc_params);
- string encoded_signature = Sign(encodedBaseString, signString);
- //return encoded_signature;
- return HttpUtility.UrlEncode(encoded_signature);
- }
- public static string GenerateEncodedParameterString( params KeyValuePair<string,string>[] parameters)
- {
- var q = from entry in parameters
- let encodedkey = HttpUtility.UrlEncode(entry.Key)
- let encodedValue = HttpUtility.UrlEncode(entry.Value)
- let encodedEntry = encodedkey + "=" + encodedValue
- orderby encodedEntry
- select encodedEntry;
- var a = q.ToArray();
- var result = string.Join("&", a);
- return result;
- }
- public static string GenerateEncodedBaseString( string HttpMethod, string baseURI, string paramString)
- {
- //returns method&url¶ms encoded
- return HttpMethod.ToUpper() + "&" + HttpUtility.UrlEncode(baseURI) + "&" + HttpUtility.UrlEncode(paramString);
- }
- public static string Sign(string signatureBaseString, string signingKey)
- {
- var keyBytes = System.Text.Encoding.ASCII.GetBytes(signingKey);
- using (var myhmacsha1 = new System.Security.Cryptography.HMACSHA1(keyBytes)) {
- byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(signatureBaseString);
- var stream = new MemoryStream(byteArray);
- var signedValue = myhmacsha1.ComputeHash(stream);
- var result = Convert.ToBase64String(signedValue, Base64FormattingOptions.None);
- return result;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment