Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter API & DotNetOpenAuth & HttpClient: Error "Could not authenticate you"

I'm trying to post a Twitter status update and I just can't get it working.

var consumer = new TwitterConsumer(); // WebConsumer implementation
var httpClient = new HttpClient(consumer.CreateAuthorizingHandler("THE_TOKEN", new HttpClientHandler()));

var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
     {"status", "test"}
});

// yes, yes, ugly code, only testing here
var s = httpClient.PostAsync("https://api.twitter.com/1.1/statuses/update.json", content);
var t = s.Result;
var u = t.Content.ReadAsStringAsync().Result; // HTTP 401, response from Twitter is {"errors":[{"message":"Could not authenticate you","code":32}]}

Of course, initially I thought there were some problems with my auth tokens or the OAuth process, but all other things are working fine:

The OAuth authentication seems to work, because I can access other API methods like https://api.twitter.com/1.1/account/settings.json.

I can even post status updates when submitting the status via the URI, like POST https://api.twitter.com/1.1/statuses/update.json?status=test. I only get the error when I try to put the status in the request body.

According to Google a lot of people have similar problems, unfortunately the suggested solutions (like double-checking the url-encoding or the content-Request-Header) do not work for me (the request looks as it should in Fiddler).

like image 760
Hannes Sachsenhofer Avatar asked Dec 02 '13 09:12

Hannes Sachsenhofer


1 Answers

While not strictly speaking an answer to your question, this may solve your problem.

I suggest you to have a look on linq2twitter. It's a really nice C# api for Twitter.

You can either peek (it's open-source) on how they implemented signing requests to find a problem with your code or actually use it and not mess with creating/signing requests and parsing responses (used it on one of my past project, works fine, provides convenient api).

See here for example of posting status update and be sure to read this docs section to understand how to properly configure auth (TwitterContext) in your scenario.

Hope this will help.

like image 148
Eugene Loy Avatar answered Sep 23 '22 14:09

Eugene Loy