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

1

7

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).

Intimidate answered 2/12, 2013 at 9:23 Comment(6)
api.twitter.com/1.1/statuses/update.json accepts POST method only, so I am not sure you can submit status updates via GET method.Accomplice
Compare the HTTP "Authorization" request header from account/settings.json API method with the "Authorization" HTTP request header from statuses/update.json and check for "oauth_consumer_key", "oauth_signature_method", "oauth_nonce", "oauth_token", "oauth_signature", check the Content-Type header too, this may helps you finding the problem.Accomplice
vzamanillo, GET method definitely works. I don't think that my problem is with the OAuth authentication, because every other method I tried on the API works fine. Just not the posting of a new status.Intimidate
dev.twitter.com/docs/api/1.1/post/statuses/update, if you try to use GET you will receive an error. What api version are you using?Accomplice
Sorry, I was wrong :( I double-checked, and indeed I used a POST request, but added the status parameter to the URL instead of to the body. So I used a POST request, but a GET parameter - I'll update my question accordingly. Apologies for the mix-up.Intimidate
Then look at my first comment and check headers and content type of the request.Accomplice
C
2

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.

Conal answered 9/12, 2013 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.