public static WebParameterCollection SortParametersExcludingSignature(WebParameterCollection parameters) { var copy = new WebParameterCollection(parameters); var exclusions = copy.Where(n => EqualsIgnoreCase(n.Name, "oauth_signature")); copy.RemoveAll(exclusions); foreach (var parameter in copy) { parameter.Value = Uri.EscapeUriString(parameter.Value); } copy.Sort((x, y) => x.Name.Equals(y.Name) ? x.Value.CompareTo(y.Value) : x.Name.CompareTo(y.Name)); return(copy); }
static private string ExecuteRequest(string url, WebParameterCollection param = null) { string req = url; if (param != null) { req += '?' + OAuthHelpers.Concatenate(param, "=", "&"); } Debug.WriteLine($"{req}"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(req); request.Headers.Add("Authorization", OAuthHelpers.GetAuthorizationToken(url, param)); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. return(reader.ReadToEnd()); }
public static string GetAuthorizationToken(string url, WebParameterCollection param = null) { string TimeInSecondsSince1970 = ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString(); string SHA1HASH = ""; string Nonce = ""; do { Nonce = GetNonce(); string consumer_secret = Uri.EscapeDataString(ConfigurationManager.AppSettings["consumer_secret"]); string token_secret = Uri.EscapeDataString(ConfigurationManager.AppSettings["token_secret"]); string signature_base_string = GetSignatureBaseString(url, TimeInSecondsSince1970, Nonce, param); SHA1HASH = GetSha1Hash(consumer_secret + "&" + token_secret, signature_base_string); } while (SHA1HASH.Contains("+") || SHA1HASH.Contains("/")); string Header = $"OAuth oauth_consumer_key=\"{ConfigurationManager.AppSettings["oauth_consumer_key"]}\",oauth_nonce=\"{Nonce}\",oauth_timestamp=\"{TimeInSecondsSince1970}\",oauth_token=\"{ConfigurationManager.AppSettings["oauth_token"]}\",oauth_signature=\"{SHA1HASH}\",oauth_signature_method=\"HMAC-SHA1\",oauth_version=\"1.0\""; Debug.WriteLine($"{ Header}"); return(Header); }
public static string GetSignatureBaseString(string url, string TimeStamp, string Nonce, WebParameterCollection param) { //1.Convert the HTTP Method to uppercase and set the output string equal to this value. string Signature_Base_String = "GET"; Signature_Base_String = Signature_Base_String.ToUpper(); //2.Append the ‘&’ character to the output string. Signature_Base_String = Signature_Base_String + "&"; //3.Percent encode the URL and append it to the output string. string PercentEncodedURL = Uri.EscapeDataString(url); Signature_Base_String = Signature_Base_String + PercentEncodedURL; //4.Append the ‘&’ character to the output string. Signature_Base_String = Signature_Base_String + "&"; //5.append parameter string to the output string. // a bit crap but should work for 1 param. Let see :) var authParameters = new WebParameterCollection { new WebParameter("oauth_consumer_key", ConfigurationManager.AppSettings["oauth_consumer_key"]), new WebParameter("oauth_nonce", Nonce), new WebParameter("oauth_signature_method", "HMAC-SHA1"), new WebParameter("oauth_timestamp", TimeStamp), new WebParameter("oauth_version", "1.0"), new WebParameter("oauth_token", ConfigurationManager.AppSettings["oauth_token"]) }; if (param != null) { foreach (var authParameter in param) { authParameters.Add(authParameter); } } Signature_Base_String += Uri.EscapeDataString(NormalizeRequestParameters(authParameters)); //if (param.Length > 0) // if (string.Compare(param.Substring(0, 1), "o") < 0) // { Signature_Base_String = Signature_Base_String + Uri.EscapeDataString(param + "&"); } //Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("oauth_consumer_key=" + ConfigurationManager.AppSettings["oauth_consumer_key"]); //Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_nonce=" + Nonce); //Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_signature_method=" + "HMAC-SHA1"); //Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_timestamp=" + TimeStamp); //Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_token=" + ConfigurationManager.AppSettings["oauth_token"]); //Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_version=" + "1.0"); //if (param.Length > 0) // if (string.Compare(param.Substring(0, 1), "o") >= 0) // { Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&" + param); } return(Signature_Base_String); }
public virtual void AddRange(WebParameterCollection collection) { AddCollection(collection); }