C
C#2y ago
memw

✅ Consistent way to Authorize with HttpClient?

So i'm trying to send a request to the discord API with <HttpClient> and i tried a couple ways to authorize this being one:
<HttpClient>.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"Bot {Config.Values!.DiscordToken}");
<HttpClient>.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"Bot {Config.Values!.DiscordToken}");
But this one doesn't work and it always will send me an unauthorized response 401 Before anything, yes i tried the token, i Console.Writeline'd the token and i made the same request with insomnia client and it works there.
3 Replies
TheRanger
TheRanger2y ago
why not use a discord library instead? $discordlibs
MODiX
MODiX2y ago
Discord.NET (https://github.com/discord-net/Discord.Net) + Relatively easy to use & large & friendly community which makes it easier to get help with the library + Low mantainance for existing bots on library version updates - High memory usage for caching & docs can be a bit lacking (especially for newer features) but overall has a good coverage of the library Disqord (https://github.com/Quahu/Disqord) + Good integration with the .NET generic host + Allows you to modify mostly everything to your heart's content - Heavy reliance on the host builder and DI means you cant use it without the host builder really easily DSharpPlus (https://github.com/DSharpPlus/DSharpPlus) + Beginner friendly + Hides Discord's API to make it easier to use - New functionality takes a while to be added Remora.Discord (https://github.com/Remora/Remora.Discord) + Full flexibility of utilising Discord's API, close to actual implementation + Full integration with .NET IoC, host pipeline - Steeper learning curve, less documentation
memw
memw2y ago
The thing is that i'm using a discord library but this request is to check the token validity ah, i see thanks.