public static EscapeOAuthString ( string text ) : string | ||
text | string | The text to escape. |
return | string |
internal string OAuthCalculateSignature(string method, string url, IDictionary <string, string> parameters, string tokenSecret) { var key = SharedSecret + "&" + tokenSecret; var keyBytes = Encoding.UTF8.GetBytes(key); var sb = new StringBuilder(); foreach (var pair in parameters.OrderBy(p => p.Key)) { sb.Append(pair.Key); sb.Append("="); sb.Append(UtilityMethods.EscapeOAuthString(pair.Value)); sb.Append("&"); } sb.Remove(sb.Length - 1, 1); var baseString = method + "&" + UtilityMethods.EscapeOAuthString(url) + "&" + UtilityMethods.EscapeOAuthString(sb.ToString()); var hash = Sha1Hash(keyBytes, baseString); return(hash); }
/// <summary> /// Calculates for form encoded POST data to be included in the body of an OAuth call. /// </summary> /// <remarks>This will include all non-OAuth parameters. The OAuth parameter will be included in the Authentication header.</remarks> /// <param name="parameters"></param> /// <returns></returns> public static string OAuthCalculatePostData(Dictionary <string, string> parameters) { string data = String.Empty; foreach (KeyValuePair <string, string> pair in parameters) { if (!pair.Key.StartsWith("oauth")) { data += pair.Key + "=" + UtilityMethods.EscapeOAuthString(pair.Value) + "&"; } } return(data); }
public string OAuthCalculateSignatureForCalls(string method, string url, Dictionary <string, string> parameters, string tokenSecret) { string baseString = ""; string key = ApiSecret + "&" + tokenSecret; byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key); var sorted = parameters.OrderBy(p => p.Key); StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> pair in sorted) { if (pair.Key.StartsWith("method")) { } else { sb.Append(pair.Key); sb.Append("="); sb.Append(UtilityMethods.EscapeOAuthString(pair.Value)); sb.Append("&"); } } sb.Remove(sb.Length - 1, 1); baseString = method + "&" + UtilityMethods.EscapeOAuthString(url) + "&" + UtilityMethods.EscapeOAuthString(sb.ToString()); //baseString = method + "&" + UtilityMethods.EscapeOAuthString(url); //System.Security.Cryptography.HMACSHA1 sha1 = new System.Security.Cryptography.HMACSHA1(keyBytes); //byte[] hashBytes = sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(baseString)); //string hash = Convert.ToBase64String(hashBytes); string hash = UtilityMethods.Sha1Encrypt(baseString, key); Debug.WriteLine("key = " + key); Debug.WriteLine("base = " + baseString); Debug.WriteLine("sig = " + hash); return(hash); }
/// <summary> /// Calculates for form encoded POST data to be included in the body of an OAuth call. /// </summary> /// <remarks>This will include all non-OAuth parameters. The OAuth parameter will be included in the Authentication header.</remarks> /// <param name="parameters"></param> /// <returns></returns> public static string OAuthCalculatePostData(Dictionary <string, string> parameters) { string data = String.Empty; foreach (KeyValuePair <string, string> pair in parameters) { if (!pair.Key.StartsWith("oauth") && !pair.Key.StartsWith("method")) { data += pair.Key + "=" + UtilityMethods.EscapeOAuthString(pair.Value) + "&"; } } string ret = data.Length > 1 ? data.Remove(data.Length - 1, 1).ToString() : data; return(ret); }
/// <summary> /// Calculates the signature for an OAuth call. /// </summary> /// <param name="method">POST or GET method.</param> /// <param name="url">The URL the request will be sent to.</param> /// <param name="parameters">Parameters to be added to the signature.</param> /// <param name="tokenSecret">The token secret (either request or access) for generating the SHA-1 key.</param> /// <returns>Base64 encoded SHA-1 hash.</returns> public string OAuthCalculateSignature(string method, string url, Dictionary <string, string> parameters, string tokenSecret) { string baseString = ""; string key = ApiSecret + "&" + tokenSecret; byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key); #if !SILVERLIGHT SortedList <string, string> sorted = new SortedList <string, string>(); foreach (KeyValuePair <string, string> pair in parameters) { sorted.Add(pair.Key, pair.Value); } #else var sorted = parameters.OrderBy(p => p.Key); #endif StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> pair in sorted) { sb.Append(pair.Key); sb.Append("="); sb.Append(UtilityMethods.EscapeOAuthString(pair.Value)); sb.Append("&"); } sb.Remove(sb.Length - 1, 1); baseString = method + "&" + UtilityMethods.EscapeOAuthString(url) + "&" + UtilityMethods.EscapeOAuthString(sb.ToString()); #if WindowsCE FlickrNet.Security.Cryptography.HMACSHA1 sha1 = new FlickrNet.Security.Cryptography.HMACSHA1(keyBytes); #else System.Security.Cryptography.HMACSHA1 sha1 = new System.Security.Cryptography.HMACSHA1(keyBytes); #endif byte[] hashBytes = sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(baseString)); string hash = Convert.ToBase64String(hashBytes); Debug.WriteLine("key = " + key); Debug.WriteLine("base = " + baseString); Debug.WriteLine("sig = " + hash); return(hash); }
/// <summary> /// Calculates for form encoded POST data to be included in the body of an OAuth call. /// </summary> /// <remarks>This will include all non-OAuth parameters. The OAuth parameter will be included in the Authentication header.</remarks> /// <param name="parameters"></param> /// <returns></returns> public static string OAuthCalculatePostData(Dictionary <string, string> parameters) { string data = String.Empty; foreach (KeyValuePair <string, string> pair in parameters) { // Silverlight < 5 doesn't support modification of the Authorization header, so all data must be sent in post body. #if SILVERLIGHT data += pair.Key + "=" + UtilityMethods.EscapeOAuthString(pair.Value) + "&"; #else if (!pair.Key.StartsWith("oauth")) { data += pair.Key + "=" + Uri.EscapeDataString(pair.Value) + "&"; } #endif } return(data); }