C#. Twitter API. update/status


#1

I do request to:

request {Method: POST, RequestUri: ‘https://api.twitter.com/1.1/statuses/update.json?status=hello stackoverflow’, Version: 1.1, Content: , Headers: {
Authorization: OAuth oauth_consumer_key=“XXXXXXXXXXXXXX”, oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“1428577639”, oauth_nonce=“NjM1NjQxODg4Mzg2MDIyMTE0”, oauth_version=“1.0”, oauth_token=“nnnnnnn-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”, oauth_signature=“HIs0vcWeko4ZiQHgy%2BVRfOnBPmI%3D” }} System.Net.Http.HttpRequestMessage

and have response as:

StatusCode: 401, ReasonPhrase: ‘Authorization Required’, Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { connection: close date: Thu, 09 Apr 2015 11:08:53 GMT server: tsa_b set-cookie: guest_id=v1%3A142857773319762055; Domain=.twitter.com; Path=/; Expires=Sat, 08-Apr-2017 11:08:53 UTC strict-transport-security: max-age=631138519 x-connection-hash: f29409f461e3cab222b50bb2f762df7a x-response-time: 6
Content-Length: 64 content-type: application/json; charset=utf-8 } {“errors”:[{“code”:32,“message”:“Could not authenticate you.”}]}

What could be the problem? And I wanted to ask during the request should be used “oauth_signature_method”, “oauth_timestamp”, “oauth_nonce” which I used during authorization or new?

And my code:

  var resource_url = "https://api.twitter.com/1.1/statuses/update.json";
  resource_url += "?status=" + Uri.EscapeDataString(txtMain.Text);
  string body = String.Empty;
  body += "oauth_consumer_key=\"" + Classes.TWITTERcl.oauth_consumer_key + "\", ";
  body += "oauth_signature_method=\"" + "HMAC-SHA1" + "\", ";
  body += "oauth_timestamp=\"" + Classes.TWITTERcl.oauth_timestamp + "\", ";
  body += "oauth_nonce=\"" + Classes.TWITTERcl.oauth_nonce + "\", ";
  body += "oauth_version=\"" + "1.0" + "\"";
  body += "oauth_token=\"" + roamingSettings.Values["twitterToken"] + "\", ";
  body += "oauth_signature=\"" + Uri.EscapeDataString(Classes.TWITTERcl.GetSignature()) + "\"";

  HttpClient httpClient = new HttpClient();
  HttpRequestMessage request = new HttpRequestMessage();
  request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("OAuth", body);
  request.RequestUri = new Uri(resource_url, UriKind.Absolute);
  request.Method = new HttpMethod("POST");

  var response = await httpClient.SendAsync(request);
  var text = await response.Content.ReadAsStringAsync();

#2

Changed category to OAuth, as this is an OAuth related question. You might want to adjust the title too.


#3

It is not recommended that you try and roll your own OAuth requests. The encoding and signature steps are complex and precise and the slightest bug can cause them to fail.

Try using a library like this http://deanhume.com/home/blogpost/a-simple-guide-to-using-oauth-with-c-/49