'Discord.NET DiscordRestClient 401 Unauthorized
I'm using Discord.Net v3.5.0.
I have an OAuth2 Access Token and I'm trying to use a DiscordRestClient to get the user's guilds, but I'm getting back 401 Unauthorized.
DiscordRestClient discordClient = new();
await discordClient.LoginAsync(TokenType.Bearer, accessToken);
IReadOnlyCollection<IGuild> restGuilds = await discordClient.GetGuildsAsync();
Stepping over await discordClient.GetGuildsAsync() throws the exception.
fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
Unhandled exception in circuit 'iPxGWxw_tZW73Ya4zYXPehZr3w_Wdcs9uXYuEaUkslY'.
Discord.Net.HttpException: The server responded with error 401: 401: Unauthorized
at Discord.Net.Queue.RequestBucket.SendAsync(RestRequest request)
at Discord.Net.Queue.RequestQueue.SendAsync(RestRequest request)
at Discord.API.DiscordRestApiClient.SendInternalAsync(String method, String endpoint, RestRequest request)
at Discord.API.DiscordRestApiClient.SendAsync[TResponse](String method, String endpoint, BucketId bucketId, ClientBucketType clientBucket, RequestOptions options)
at Discord.API.DiscordRestApiClient.GetGuildAsync(UInt64 guildId, Boolean withCounts, RequestOptions options)
at Discord.Rest.ClientHelper.GetGuildsAsync(BaseDiscordClient client, Boolean withCounts, RequestOptions options)
I can successfully use a regular HttpClient though, so I can confirm the Access Token is valid.
HttpClient httpClient = httpClientFactory.CreateClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
JsonArray? myGuilds = await httpClient.GetFromJsonAsync<JsonArray>("https://discord.com/api/v9/users/@me/guilds");
What am I doing wrong?
Solution 1:[1]
You are not initialize DiscordRestClient proberly.
DiscordRestClient discordClient = new();
Initializes a new DiscordRestClient with the provided configuration.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MD. RAKIB HASAN |
