Language: C#
How to Encode for OAuth
1: /// <summary> 2: /// URL-encodes a string for use with OAuth. 3: /// </summary> 4: /// <param name="toEncode"> 5: /// The string to encode. 6: /// </param> 7: /// <returns> 8: /// A URL-encoded string where all of the encoded characters are upper-case. 9: /// </returns> 10: /// <remarks> 11: /// This is a different URL-encoding implementation since the default 12: /// .NET one outputs the percent encoding in lower case. While this is 13: /// not a problem with the percent encoding spec, it is used in 14: /// upper-case throughout OAuth (http://oauth.net/core/1.0a#encoding_parameters). 15: /// </remarks> 16: /// <exception cref="System.ArgumentNullException"> 17: /// Thrown if <paramref name="toEncode" /> is <see langword="null" />. 18: /// </exception> 19: public static string UrlEncodeForOAuth(this string toEncode) 20: { 21: if (toEncode == null) 22: { 23: throw new ArgumentNullException("toEncode"); 24: } 25: StringBuilder result = new StringBuilder(); 26: foreach (char symbol in toEncode) 27: { 28: if (SafeUrlCharacters.IndexOf(symbol) != -1) 29: { 30: result.Append(symbol); 31: } 32: else 33: { 34: result.AppendFormat("%{0:X2}", (int)symbol); 35: } 36: } 37: return result.ToString(); 38: }
Tags:
Description:
This is an extension method for encoding a string per OAuth 1.0 spec by Travis Illig!
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

