public HttpWebRequest GetRequest(RequestParameters requestParameters) { OAuthHeaderBuilder oAuthHeaderBuilder = new OAuthHeaderBuilder(AuthenticationContext); OAuthSignatureBuilder signatureBuilder = new OAuthSignatureBuilder(AuthenticationContext); string oauthSignature = signatureBuilder.CreateSignature(requestParameters); requestParameters.Headers.AddOAuthSignature(oauthSignature); var authHeader = oAuthHeaderBuilder.BuildAuthHeader(requestParameters); ServicePointManager.Expect100Continue = false; var queryUrl = requestParameters.GetRequestUrl(); var request = (HttpWebRequest)WebRequest.Create(queryUrl); request.Headers.Add("Authorization", authHeader); request.Method = requestParameters.HttpMethod; request.ContentType = "application/x-www-form-urlencoded"; string postBody = requestParameters.GetPostBody(); if (!string.IsNullOrWhiteSpace(postBody)) { using (Stream stream = request.GetRequestStream()) { byte[] content = Encoding.ASCII.GetBytes(postBody); stream.Write(content, 0, content.Length); } } return request; }
public string CreateSignature(RequestParameters parameters) { // 3.) Creating the signature base string string signatureBaseString = GetSignatureBaseString(parameters); // 4.) Getting a signing key string signingKey = GetSigningKey(); // 5.) Calculating the signature var result = CalculateSignature(signingKey, signatureBaseString); return result; }
public string GetSignatureBaseString(RequestParameters parameters) { const string separator = "&"; var result = new StringBuilder(); // 1.) Convert the HTTP Method to uppercase and set the output string equal to this value. result.Append(parameters.HttpMethod.ToUpperInvariant()); // 2.) Append the ‘&’ character to the output string. result.Append(separator); // 3.) Percent encode the URL and append it to the output string. result.Append(Uri.EscapeDataString(parameters.BaseUrl)); // 4.) Append the ‘&’ character to the output string. result.Append(separator); // 5.) Percent encode the parameter string and append it to the output string. var baseString = GetParameterString(parameters.GetParameters(), separator); result.Append(Uri.EscapeDataString(baseString)); return result.ToString(); }
private static HttpWebRequest GetWebRequest( IAuthenticationContext authenticationContext, RequestParameters requestParameters) { var requestBuilder = new RequestBuilder(authenticationContext); return requestBuilder.GetRequest(requestParameters); }
public RequestContextFixture() { AuthenticationContext = new TestAuthenticationContext(); RequestParameters = new TestRequestParameters(AuthenticationContext); }
public string BuildAuthHeader(RequestParameters requestParameters) { return $"OAuth {GetParameterString(requestParameters.Headers.Values, ", ")}"; }