0

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?

runnerpaul
  • 5,942
  • 8
  • 49
  • 118

2 Answers2

0

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);
Batuhan
  • 1,521
  • 12
  • 29
0

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.

Morrolan
  • 317
  • 3
  • 10