✅ 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.
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("myURL"),
Headers =
{
{ "Authorization", "token MyTokenCode" },
},
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("myURL"),
Headers =
{
{ "Authorization", "token MyTokenCode" },
},
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
28 Replies
HimmDawg
HimmDawg14mo ago
Not sure if I understood you correctly. What do you want to add?
Ale Kantousian
Ale Kantousian14mo ago
How can I add "OAuth 2.0" in my Get, when I try to request with this code, I have the following unauthorized 401 error. My token is correct, because I tested the request in postman
HimmDawg
HimmDawg14mo ago
request.Headers.Authorization = new AuthenticationHeaderValue("token", "yourToken");
request.Headers.Authorization = new AuthenticationHeaderValue("token", "yourToken");
You mean this? That's the real token in the picture? You shouldn't post tokens publicly
Ale Kantousian
Ale Kantousian14mo ago
No, my real token is another one, I put that one just to illustrate in the image
HimmDawg
HimmDawg14mo ago
Good 😄
Ale Kantousian
Ale Kantousian14mo ago
I tried this, but I got the same 401 😕 Sometimes I think this is a bug in .NET
HimmDawg
HimmDawg14mo ago
So, when you tried stuff out in postman and it worked there, you could try to copy the headers from postman and add them to your request.
Ale Kantousian
Ale Kantousian14mo ago
This is my Headers
HimmDawg
HimmDawg14mo ago
+7 hidden headers
Ale Kantousian
Ale Kantousian14mo ago
Hummmmm give me a second
Ale Kantousian
Ale Kantousian14mo ago
How can I add this in my headers?
HimmDawg
HimmDawg14mo ago
I guess those wont be needed
Ale Kantousian
Ale Kantousian14mo ago
The same again, 401 😕
HimmDawg
HimmDawg14mo ago
Hmm, without seeing your code, I'd have to guess from here on. Maybe your url is wrong?
Ale Kantousian
Ale Kantousian14mo ago
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);
}
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);
}
HimmDawg
HimmDawg14mo ago
Have you tried using "Bearer" as token prefix?
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "yourToken");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "yourToken");
Ale Kantousian
Ale Kantousian14mo ago
Yes
HimmDawg
HimmDawg14mo ago
I#m running out of ideas here fluffyFoxMood Is there more to the authorization process? Do you have to provide some client id or so in the url?
Ale Kantousian
Ale Kantousian14mo ago
Unfortunately not, that's why I told you that I think it's some .NET bugs 😕
HimmDawg
HimmDawg14mo ago
That's pretty unlikely imo. But no idea what else could be wrong then
Ale Kantousian
Ale Kantousian14mo ago
So, yesterday I tried several solutions according to the documentation, according to StackOverFlow and today here with your tips, I find it very difficult for 3 different way to all go wrong when all 3 showed similar or identical solutions.
HimmDawg
HimmDawg14mo ago
Must be something super obvious but small then fluffyFoxThink remember that I was requesting somthing from "https://..." when i just needed "http://...", but i doubt that this would return 401
Ale Kantousian
Ale Kantousian14mo ago
I didn't understand what you meant
HimmDawg
HimmDawg14mo ago
What exactly did you not understand?
Ale Kantousian
Ale Kantousian14mo ago
About https and http return 401
HimmDawg
HimmDawg14mo ago
That was just an example that it could be a tiny detail you are missing
Ale Kantousian
Ale Kantousian14mo ago
Humm ok
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.