0
using (WebClient wc = new WebClient())
{
    wc.Headers.Add("Content-Type", "application/json");                
    wc.Headers.Add("Authorization", "Token 12345678");                
    wc.Encoding = Encoding.UTF8;
    var jsonRespStr = wc.DownloadString("https://example.com/api/xxx");
}

I run the above c# script with the following result.
"{"detail":"Authentication credentials were not provided."}"

However, I use the same token call thru PostMan, it works

  • finally, I found the answer at this post https://stackoverflow.com/questions/28564961/authorization-header-is-lost-on-redirect/42566541#42566541 the problem was due to redirect – onefourone anakin Nov 24 '20 at 03:42

1 Answers1

0

If it is a Bearer token, then following lines would do.

        HttpClient _httpClient;
        string tokenString = "fa114107311259f5f33e70a5d85derestve***dse--";
        _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", tokenString);
        string requestUri = "https://example.com/api/xxx";
        HttpResponseMessage httpResponse = await httpClient.GetAsync(requestUri);

You can try "GetStringAsync" instead of "GetAsync".