RestSharp: UrlEncode in signature base generation

2019-05-30 14:25发布

问题:

I'm working on twitter client for win8, using RestSharp ( http://restsharp.org/ ) and I have such problem: When I'm posting new tweet with RestClient

var timeLine = new RestRequest("/1/statuses/update.json", Method.POST);
var txt = "Hello world";
timeLine.AddParameter("status", txt);

everything works excelent, but if I add more complex status like:

var txt = "Hello, World! What a nice day! #1May";
timeLine.AddParameter("status", txt);

I recieve 401 error. In debugger I saw, that status parameter in Signature Base String is incorrect. I have:

status%3DHello%2C%2520World%21%2520What%2520a%2520nice%2520day%21%2520%231May

and right string (from dev.twitter.com):

status%3DHello%252C%2520World%2521%2520What%2520a%2520nice%2520day%2521%2520%25231May

You can see, that punctuation marks ,!# and other encodes incorrect. How can I fix it? Signature base generation and Encoding are in /Authenticators/OAuth/OAuthTools.cs

回答1:

I have the same problem when I display twitter feed in website. Hence, I used this code to convert the text.

Regex.Replace(str, "@(.*?):", @"@http://twitter.com/#!/$1>$1:");