0

I'm trying to get an authorization token for the Twitter REST api but it seems I'm doing something wrong. Is there something wrong with my code?

//Authorization
var customerKey = "xxxxxxxxxx";
var customerSecret = "xxxxxxxxxxxxxxxxxxx";
var b64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", WebUtility.UrlEncode(customerKey), WebUtility.UrlEncode(customerSecret))));
var req = new HttpRequestMessage(HttpMethod.Post, "https://api.twitter.com/oauth2/token");
req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", b64);
req.Content = new FormUrlEncodedContent(new Dictionary<string, string>() {
    { "grant_type", "client_credentials" }
});
var token = "";
using (var res = await http.SendAsync(req))
{
    if (res.IsSuccessStatusCode)
        token = Regex.Match(await res.Content.ReadAsStringAsync(), "\"access_token\":\"([^\"]+)").Groups[1].Value;
}
http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer ", token);

In response I'm getting this message:

{"errors":[{"code":99,"message":"Unable to verify your credentials","label":"authenticity_token_error"}]}
Reynevan
  • 1,475
  • 1
  • 18
  • 35
  • [this answer](http://stackoverflow.com/a/32084770/969278) may help – Halis S. Sep 28 '16 at 15:21
  • I have logged out of twitter on my PC, still the same message. Is it possible that some other api authentication session is still going? Because it's possible that it worked one time before, I don't remember for sure but it's possible. Is ASP.NET website/Twitter API storing that somewhere? – Reynevan Sep 28 '16 at 15:32
  • I tried your code with my own key & secret, and there was nothing wron with the response. Your key & secret must still be used somewhere else I think. – Halis S. Sep 29 '16 at 06:29
  • Actually, I an no longer able look them up anymore. When I try listing my Twitter Applications, or executing a request, I'm getting `Access Denied` error... I don't even wanna know anymore... – Reynevan Sep 29 '16 at 07:03

0 Answers0