✅ 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
HimmDawg2y ago
Not sure if I understood you correctly. What do you want to add?
Ale Kantousian
Ale KantousianOP2y 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
HimmDawg2y 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 KantousianOP2y ago
No, my real token is another one, I put that one just to illustrate in the image
HimmDawg
HimmDawg2y ago
Good 😄
Ale Kantousian
Ale KantousianOP2y ago
I tried this, but I got the same 401 😕 Sometimes I think this is a bug in .NET
HimmDawg
HimmDawg2y 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 KantousianOP2y ago
This is my Headers
HimmDawg
HimmDawg2y ago
+7 hidden headers
Ale Kantousian
Ale KantousianOP2y ago
Hummmmm give me a second
Ale Kantousian
Ale KantousianOP2y ago
How can I add this in my headers?
HimmDawg
HimmDawg2y ago
I guess those wont be needed
Ale Kantousian
Ale KantousianOP2y ago
The same again, 401 😕
HimmDawg
HimmDawg2y ago
Hmm, without seeing your code, I'd have to guess from here on. Maybe your url is wrong?
Ale Kantousian
Ale KantousianOP2y 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
HimmDawg2y 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 KantousianOP2y ago
Yes
HimmDawg
HimmDawg2y 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 KantousianOP2y ago
Unfortunately not, that's why I told you that I think it's some .NET bugs 😕
HimmDawg
HimmDawg2y ago
That's pretty unlikely imo. But no idea what else could be wrong then
Ale Kantousian
Ale KantousianOP2y 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
HimmDawg2y 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 KantousianOP2y ago
I didn't understand what you meant
HimmDawg
HimmDawg2y ago
What exactly did you not understand?
Ale Kantousian
Ale KantousianOP2y ago
About https and http return 401
HimmDawg
HimmDawg2y ago
That was just an example that it could be a tiny detail you are missing
Ale Kantousian
Ale KantousianOP2y ago
Humm ok
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server