public NegotiationToken GetToken() { WebRequest request = WebRequest.Create(requestUri); request.Method = "POST"; Nonce nonce = Nonce.Generate(); TimeStamp timestamp = TimeStamp.Generate(); BaseString baseString = new BaseString(request.RequestUri, request.Method, nonce, timestamp, credentials, HmacSha1Signature.MethodName); Signature signature = new HmacSha1Signature(baseString.ToString(), credentials); AuthorizationHeader header = new AuthorizationHeader(credentials, nonce, timestamp, signature); request.Headers.Add(HttpRequestHeader.Authorization, header.ToString()); using (WebResponse res = request.GetResponse()) using (Stream s = res.GetResponseStream()) using (StreamReader sr = new StreamReader(s)) { NameValueCollection response = HttpUtility.ParseQueryString(sr.ReadToEnd()); return new NegotiationToken(response["oauth_token"], response["oauth_token_secret"]); } }
protected override Signature GenerateSignature(WebRequest request, Nonce nonce, TimeStamp timestamp) { BaseString baseString = new BaseString(request.RequestUri, request.Method, nonce, timestamp, credentials, HmacSha1Signature.MethodName); baseString.Token = token; return new HmacSha1Signature(baseString.ToString(), credentials, token); }
public void RequestOnNonStandardPort() { Nonce nonce = new Nonce("1234"); TimeStamp timestamp = new TimeStamp("1327772674"); Uri requestUri = new Uri("http://api.netflix.com:5050/catalog/titles"); ClientCredentials credentials = new ClientCredentials("key", "secret"); BaseString baseString = new BaseString(requestUri, "PUT", nonce, timestamp, credentials, HmacSha1Signature.MethodName); string expectedBaseString = "PUT&http%3A%2F%2Fapi.netflix.com%3A5050%2Fcatalog%2Ftitles&oauth_consumer_key%3Dkey%26oauth_nonce%3D1234%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1327772674%26oauth_version%3D1.0"; Assert.AreEqual(expectedBaseString, baseString.ToString()); }
public void RequestWithQueryParameters() { Nonce nonce = new Nonce("1234"); TimeStamp timestamp = new TimeStamp("1327772674"); Uri requestUri = new Uri("http://api.netflix.com/catalog/titles?arg1=value1&arg2=&arg3"); ClientCredentials credentials = new ClientCredentials("key", "secret"); BaseString baseString = new BaseString(requestUri, "GET", nonce, timestamp, credentials, HmacSha1Signature.MethodName); string expectedBaseString = "GET&http%3A%2F%2Fapi.netflix.com%2Fcatalog%2Ftitles&arg1%3Dvalue1%26arg2%3D%26arg3%3D%26oauth_consumer_key%3Dkey%26oauth_nonce%3D1234%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1327772674%26oauth_version%3D1.0"; Assert.AreEqual(expectedBaseString, baseString.ToString()); }