Advertisement
TankorSmash

OAuth1a.cs

Nov 12th, 2012
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.52 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using HtmlAgilityPack;
  8. using System.Web;
  9. namespace NetflixAPI
  10. {
  11.     static class OAuth1a
  12.     {
  13.         //gameplan:
  14.         // target URL
  15.         // Netflix params: Title, offset, limit
  16.         // Oauth params: consumer_key, nonce, timestamp, signature_method
  17.         //
  18.         // and then the signature after, which is based on the above encoded
  19.         //
  20.         // HMAC outputs 20 chars, turn that to b64 then encode that
  21.         //
  22.         // NEed to encode certain chars AND make sure to encode it befre I pass
  23.         // it to the MHA or whatever
  24.  
  25.         public static void Start()
  26.         {
  27.             string VERB = "GET";
  28.             string URL = "http://api-public.netflix.com/catalog/titles";
  29.  
  30.                 //Not going to change
  31.    
  32.             string consumer_key = "u7r68et24v6rd5r9u828qvte";
  33.             string consumer_secret = "uWdp2AXnnu";
  34.             string oauth_signature_method = "HMAC-SHA1";
  35.             string oauth_version = "1.0";
  36.  
  37.                 //dynamic values
  38.             string oauth_timestamp = GenerateTimeStamp();
  39.             string oauth_nonce = GenerateTimeStamp();
  40.                 // need to figure this out
  41.                 // string oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D";
  42.             string oauth_signature = GenerateSignature(VERB, URL,
  43.                                                        consumer_key,
  44.                                                        consumer_secret,
  45.                                                        oauth_timestamp,
  46.                                                        oauth_nonce,
  47.                                                        oauth_signature_method,
  48.                                                        new KeyValuePair
  49.                                                            <string, string>(
  50.                                                            "term", "liar"),
  51.                                                        new KeyValuePair
  52.                                                            <string, string>(
  53.                                                            "start_index", "0"),
  54.                                                        new KeyValuePair
  55.                                                            <string, string>(
  56.                                                            "max_results", "25"));
  57.         }
  58.  
  59.         public static string GenerateTimeStamp()
  60.         {
  61.             double time = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
  62.             return Convert.ToInt32(time).ToString();
  63.         }
  64.  
  65.         public static string GenerateNonce()
  66.         {
  67.             return Guid.NewGuid().ToString();
  68.         }
  69.  
  70.         public static string GenerateSignature(string HttpMethod, string url,
  71.                 string consumer_key, string consumer_secret, string
  72.                 oauth_timestamp, string oauth_nonce, string
  73.                 oauth_signature_method= "HMACSHA1", params KeyValuePair<string,
  74.                 string>[] parameters)
  75.         {
  76.             //so this needs an encoded base string, which'll be
  77.             //METHOD&URL&PARAMS
  78.             //then I pass it to the HMAC-SHA1 which spits out a 20char, which'll
  79.             //be encoded and used as sig param on the unencoded basestring
  80.  
  81.             //create BASESTRING where we already know the METHOD and URL, so we
  82.             //need to grab all the params
  83.             // string enc_params = GenerateEncodedParameterString(consumer_key
  84.             // :"consumer_key" ,
  85.             //         oauth_signature_method :"oauth_signature_method" ,
  86.             //         oauth_version "oauth_version" , oauth_timestamp
  87.             //         :"oauth_timestamp" ,
  88.             //         oauth_nonce : "oauth_nonce" , parameters);
  89.             KeyValuePair<string, string>[] qwe =
  90.                 {
  91.                     new KeyValuePair<string, string>("consumer_key",
  92.                                                      consumer_key),
  93.                     new KeyValuePair<string, string>("oauth_signature_method",
  94.                                                      oauth_signature_method),
  95.                     new KeyValuePair<string, string>("oauth_version",
  96.                                                      "1"),
  97.                     new KeyValuePair<string, string>("oauth_timestamp",
  98.                                                      oauth_timestamp)
  99.                     ,
  100.                     new KeyValuePair<string, string>("oauth_nonce", oauth_nonce)
  101.                 };
  102.  
  103.  
  104.             parameters.ToList().AddRange(qwe.ToList());
  105.             string enc_params = GenerateEncodedParameterString(parameters);
  106.            
  107.            
  108.  
  109.             //TODO: fix the hardcoded key
  110.             string signString = consumer_key + "&";
  111.  
  112.             var encoded_signature = "asd";
  113.             return encoded_signature;
  114.         }
  115.  
  116.         public static string GenerateEncodedParameterString( params KeyValuePair<string,string>[] parameters)
  117.         {
  118.             var q = from entry in parameters
  119.                 let encodedkey = HttpUtility.UrlEncode(entry.Key)
  120.                 let encodedValue = HttpUtility.UrlEncode(entry.Value)
  121.                 let encodedEntry = encodedkey + "=" + encodedValue
  122.                 orderby encodedEntry
  123.                 select encodedEntry;
  124.             var result = string.Join("&", q.ToArray());
  125.             return result;
  126.         }
  127.  
  128.  
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement