✅ Add OAuth 2.0 in HttpClient Request Headers
Hi folks, I tried to request by Get using HttpClient, but in this case, I don't know how can add this "OAuth 2.0" in HttpClient to send. When I use this code I have the unauthorized 401.




/close request.Headers.Authorization = new AuthenticationHeaderValue("token", "yourToken");var client = new HttpClient();
var request = new HttpRequestMessage {
Method = HttpMethod.Get,
RequestUri = new Uri("MyUrl"),
Headers =
{
{ "Authorization", "MyToken" },
//{ "Postman-Token", "<calculated when request is sent>"},
//{ "Host", "calculated when request is sent" },
//{ "User-Agent", "PostmanRuntime/7.32.2" },
{ "Accept", "*/*" },
{ "Accept-Encoding", "gzip, deflate, br" },
{ "Connection", "keep-alive" },
//{ "Content-Type", "application/json" },
},
};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "yourToken");