GetAsync using Basic Authentication even though I specify Bearer

1.2k Views Asked by At

I have the below GetAsync which I'm trying to authenticate with a bearer token but I get a 403 response. When I look at the request headers I can see Authorization: Basic YWJjOmRlZg==

var client = new HttpClient(handler)
{
    BaseAddress = new Uri("https://api.myendpoint.com/v2/")
};
client.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Bearer", "token");
var httpResponseMessage = _myclient.GetAsync("instruments?country=US&type=BOND").GetAwaiter().GetResult();

The token is valid. I confirmed that with Postman. I also hardcoded it into AuthenticationHeaderValue to ensure I had the correct token. I guess I assumed I was passing the bearer token incorrectly.

How do I get it to use Bearer authentication?

2

There are 2 best solutions below

0
Batuhan On

Passing token to header (I am currently using this):

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

or something like this:

client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
0
Morrolan On

The HTTP 403 Forbidden response is thrown by the server because it refuses to authorize your request. This probably means you are missing some claim or specific claim value in your bearer token.