POST status update to Twitter from WINPRT returns 401 when containing special characters


#1

I am trying to solve my problem for some time now, but I’m running out of ideas.

I am using the following method to send a status update to Twitter via POST:

private async void PostStatusUpdateToTwitter(string text) { String TwitterUrl = "https://api.twitter.com/1.1/statuses/update.json";
        string timeStamp = GetTimeStamp();
        string nonce = GetNonce();

        String SigBaseStringParams = MyUrlEncoder.UrlEncode("oauth_consumer_key=" + TwitterConsumerKey);
        SigBaseStringParams += MyUrlEncoder.UrlEncode("&oauth_nonce=" + nonce);
        SigBaseStringParams += MyUrlEncoder.UrlEncode("&oauth_signature_method=HMAC-SHA1");
        SigBaseStringParams += MyUrlEncoder.UrlEncode("&oauth_timestamp=" + timeStamp);
        SigBaseStringParams += MyUrlEncoder.UrlEncode("&oauth_token=" + App.SettingsStore.TwitteroAuthToken);
        SigBaseStringParams += MyUrlEncoder.UrlEncode("&oauth_version=1.0");
        SigBaseStringParams += MyUrlEncoder.UrlEncode("&status=") + MyUrlEncoder.UrlEncodeWith25(text);
        String SigBaseString = "POST&";
        SigBaseString += MyUrlEncoder.UrlEncode(TwitterUrl) + "&" + SigBaseStringParams;

        String Signature = GetSignature(SigBaseString, TwitterConsumerSecret, App.SettingsStore.TwitteroAuthTokenSecret);

        HttpStringContent httpContent = new HttpStringContent("status=" + text);
        httpContent.Headers.ContentType = HttpMediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
        string authorizationHeaderParams = "oauth_consumer_key=\"" + TwitterConsumerKey + "\", oauth_nonce=\"" + nonce + "\", oauth_signature=\"" + MyUrlEncoder.UrlEncode(Signature) + "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + timeStamp + "\", oauth_token=\"" + MyUrlEncoder.UrlEncode(App.SettingsStore.TwitteroAuthToken) + "\", oauth_version=\"1.0\"";

        HttpClient httpClient = new HttpClient();

        httpClient.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("OAuth", authorizationHeaderParams);

        var httpResponseMessage = await httpClient.PostAsync(new Uri(TwitterUrl), httpContent);
        if (httpResponseMessage.IsSuccessStatusCode)
        {
            string response = await httpResponseMessage.Content.ReadAsStringAsync();
        }
        else if (!httpResponseMessage.IsSuccessStatusCode)
        {
            MessageDialog twitterPostErrorMsg = new MessageDialog(string.Format("there was a problem posting your tweet: \n\nStatuscode: {0} \n\nMessage: {1}", httpResponseMessage.StatusCode, httpResponseMessage.Content), "Sorry,");
            await twitterPostErrorMsg.ShowAsync();
        }
    }

My problem: if I do send a status update that contains only plain text without characters that need encoding, they get posted. As soon as I am adding characters that need encoding, Twitter returns a 401 statuscode with a reference to their error code 32 (Your call could not be completed as dialed.) and sending the status update failed.

I compared the signature my code creates as well as the Authorization Headers, they are absolutely equal with the code above:

Signature:
Twitter API Console:

POST&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fstatuses%2Fupdate.json&oauth_consumer_key%3DXXXX%26oauth_nonce%3D1a53364d16df21a7d63bd61f3b65bab8%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1397880971%26oauth_token%3DXXXX%26oauth_version%3D1.0%26status%3Dif%2520%2528%2521this.working%2529%2520%257B%2520me.screaming%2520%253D%2520true%253B%2520%257D%2520esle%2520%257B%2520me.smiling%2520%253D%2520true%253B%2520%257D%2520%2523wpdev

My app:

POST&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fstatuses%2Fupdate.json&oauth_consumer_key%3DXXXX%26oauth_nonce%3Dthcyabkyyyylidvbofmplnsxwuykzjdn%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1397886504%26oauth_token%3DXXXX%26oauth_version%3D1.0%26status%3Dif%2520%2528%2521this.working%2529%2520%257B%2520me.screaming%2520%253D%2520true%253B%2520%257D%2520else%2520%257B%2520me.smiling%2520%253D%2520true%253B%2520%257D%2520%2523wpdev

Authorization Header
Twitter API Console:

oauth_consumer_key="XXXX", oauth_nonce="1a53364d16df21a7d63bd61f3b65bab8", oauth_signature="hCXHS4hVUN7ukvzJWpuu29hMEP4%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1397880971", oauth_token="XXXX", oauth_version="1.0"

My app:

oauth_consumer_key="XXXX", oauth_nonce="yrjczyygulkfffetxfwjypmhwqarjgcw", oauth_signature="1rloh0bHFiE3M7VhJ30%2B5fWEIVg%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1397916317", oauth_token="XXXX", oauth_version="1.0"

really don’t know what I can try else, as the encoding here is correct and without the symbol characters all works well.

Thanks in advance for any idea.