Fixed OAuth UrlEncode to handle extended characters

Mar 12, 2011 at 10:50 AM

Hi all,

I was recommended the OAuth.cs from this project on stackoverflow.  I was hoping that the OAuth library could handle extended characters (unlike my own).  Doesn't look like it does, but I like the library so I decided to make the fix to UrlEncode.

This change to UrlEncode appears to work for me:

        public static string UrlEncode(string value)
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(value);
            var result = new System.Text.StringBuilder();

            foreach (char symbol in value)
                if (unreservedChars.IndexOf(symbol) != -1)
                    foreach (byte b in Encoding.UTF8.GetBytes(symbol.ToString()))
                        result.Append('%' + String.Format("{0:X2}", b));
            return result.ToString();

Here is the code that I've been testing it with:

            OAuth.Manager man = new OAuth.Manager(CONSUMER_KEY, CONSUMER_SECRET, token, secret);
            string url = "" + OAuth.Manager.UrlEncode("setting up my twitter 私のさえずりを設定する");
            string header = man.GenerateAuthzHeader(url, "POST");

            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.PreAuthenticate = true;
            request.AllowWriteStreamBuffering = true;
            request.Headers.Add("Authorization", header);

            using (var updateResponse = (HttpWebResponse)request.GetResponse())


Cheers all,

Apr 11, 2011 at 2:11 PM

Thank you, Russell.  Will definitely include this into the library.

Apr 11, 2011 at 2:12 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.