8

我正在尝试使用RestSharp从我的网络应用程序发布 Twitter 状态。以下代码完美运行:

var status = "I am fine with posting this status.";

var client = new RestClient("https://api.twitter.com");

// The OAuth keys/tokens/secrets are retrieved elsewhere
client.Authenticator = OAuth1Authenticator.ForProtectedResource(
    _consumerKey, _consumerSecret, _accessToken, _accessTokenSecret
);

var request = new RestRequest("/1.1/statuses/update.json", Method.POST);
request.AddParameter("status", status, ParameterType.GetOrPost);

var response = client.Execute(request);

但是,如果我在状态文本中包含以下任何字符,此代码将失败并出现身份验证错误:! * ' ( )

通过大量的论坛拖网,我推断这与 OAuth 签名编码与 POST 参数的编码不匹配有关。我在 SO 上找到了这个问题,但是在 GitHub 上搜索 RestSharp 问题并没有发现任何帮助。

我可以在 RestSharp 源UrlEncodeRelaxed代码(来自 RestSharp)在传递之前,例如:

var status = "I'm NOT fine with posting this status.";

string[] UriRfc3986CharsToEscape = new[] { "!", "*", "'", "(", ")" };
string[] UriRfc3968EscapedHex = new[] { "%21", "%2A", "%27", "%28", "%29" };

for (var i = 0; i < UriRfc3986CharsToEscape.Length; i++)
    status = status.Replace(UriRfc3986CharsToEscape[i], UriRfc3968EscapedHex[i]);

但这也不起作用(我仍然收到身份验证错误)。

这里实际上是什么问题,我应该怎么做才能正确编码状态?或者这是一个 RestSharp 错误?

4

1 回答 1

4

您是否尝试过使用 .NET Framework 中内置的 HttpUtility 类?您可以在 System.Web 命名空间中找到它。

string urlEncodedText = HttpUtility.UrlEncode("Your text goes here");

MSDN


这绝对是一个 RestSharp 问题......我玩了一段时间,问题是无论如何都没有禁用标准百分比编码......

void Main()
{
    var ProtectedChars = "0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A".Replace(" 0x", "%").Split(',');
    var client = new RestClient("https://api.twitter.com");

    client.Authenticator = OAuth1Authenticator.ForProtectedResource(
        "_consumerKey", "_consumerSecret", "_accessToken", "_accessTokenSecret"
    );

    var status = "Dogs, Cats & Mice";

    string newStatus = string.Empty;

    foreach (char c in status)
    {
        var charBytes = Encoding.UTF8.GetBytes(c.ToString());
        var tempText = string.Empty;

        for (int i = 0; i < charBytes.Count(); i++)
        {
            byte b = charBytes[i];
            string hex = "%" + b.ToString("X2");
            tempText += hex;
        }

        if (ProtectedChars.Any(x => x == tempText))
        {
            newStatus += c;
        }
        else
        {
            newStatus += string.Format("{0:X2}", tempText);
        }
    }

    var request = new RestRequest("/1.1/statuses/update.json", Method.POST);
    request.AddParameter(new Parameter{ Name = "status", Type = ParameterType.GetOrPost, Value = newStatus } );

    var response = client.Execute(request);


}

在 Fiddler 中,我一直在监视发往 Twitter 的请求……这就是我发现的……

POST https://api.twitter.com/1.1/statuses/update.json HTTP/1.1
Authorization: /* auth data */
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp/104.4.0.0
Content-Type: application/x-www-form-urlencoded
Host: api.twitter.com
Content-Length: 44
Accept-Encoding: gzip, deflate

status=Dogs%252C%2520Cats%2520%2526%2520Mice

Http 请求的问题在于我们请求的主体……如果我提供的值没有被触及……那么 Twitter 就会识别出该消息并为我们更新我们的状态……

status=Dogs%252C%2520Cats%2520%2526%2520Mice  <--- Altered by RestSharp
status=Dogs%2C%20Cats%20%26%20Mice            <--- What I initially sent
于 2014-09-26T02:05:03.543 回答