I know this question might seem like a duplicate, but I have tried every solution I saw before asking this question again.
I'm trying to consume a REST API which I wrote myself with Django Rest Framework, using C# HttpClient in Xamarin Forms. The request is being sent but it does not include the required Authorization with Token as scheme.
Here are the things I've tried:
public async Task<IssueListModelView> FetchIssues(string token, string type)
{
client.BaseAddress = new Uri(XTMonitorContants.SERVICE_BASE_URL);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(XTMonitorContants.SERVICE_ISSUES_URL + type.ToLower()));
request.Headers.Add("Authorization", "Token " + token);
//client.
//client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token);
client.DefaultRequestHeaders.Add("Authorization", "Token " + token);
var response = client.SendAsync(request);
var jsonString = response.ContinueWith(t => { return t.Result; }).Result.Content.ReadAsStringAsync().Result;
IssueListModelView resp = new IssueListModelView();
if (jsonString != "")
{
try
{
resp = JsonConvert.DeserializeObject<IssueListModelView>(jsonString);
}
catch (Exception)
{
resp.error = "Internal Server Error";
}
}
return resp;
}
I have also tried to send the request directly without using HttpRequestMessage, but also gave me the same server response of
"Authentication credentials were not provided."
Request made by Postman
GET /api/issues/all HTTP/1.1
Host: example.com
Authorization: Token ffba1c43ac346945b788768e2a428d5e5d3c1fc9
Cache-Control: no-cache
Postman-Token: 0c1eb96d-d9d6-a9e7-0cfa-0444fdc0248e
Request made by HttpClient
{Method: GET, RequestUri: 'https://example.com/api/issues/all', Version: 1.1, Content: <null>, Headers: { Authorization: Token ffba1c43ac346945b788768e2a428d5e5d3c1fc9 }}
Please note that I've tested the API properly using Postman and also setup Swagger, it works fine on both.
Would be glad if someone could give me a hand here, as I think there's probably something I'm missing out