Browse Source

Authorization header should not be prefixed for bearer tokens, and should not be set in DiscordRestApiClient constructor if blank (it gets set later).

If using bearer token, connect to regular gateway.
pull/2516/head
Cool-Man 2 years ago
parent
commit
2efd6a493a
2 changed files with 13 additions and 4 deletions
  1. +3
    -2
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  2. +10
    -2
      src/Discord.Net.WebSocket/DiscordSocketApiClient.cs

+ 3
- 2
src/Discord.Net.Rest/DiscordRestApiClient.cs View File

@@ -75,7 +75,8 @@ namespace Discord.API
RestClient = _restClientProvider(baseUrl);
RestClient.SetHeader("accept", "*/*");
RestClient.SetHeader("user-agent", UserAgent);
RestClient.SetHeader("authorization", GetPrefixedToken(AuthTokenType, AuthToken));
if (!string.IsNullOrEmpty(AuthToken))
RestClient.SetHeader("authorization", GetPrefixedToken(AuthTokenType, AuthToken));
}
/// <exception cref="ArgumentException">Unknown OAuth token type.</exception>
internal static string GetPrefixedToken(TokenType tokenType, string token)
@@ -83,7 +84,7 @@ namespace Discord.API
return tokenType switch
{
TokenType.Bot => $"Bot {token}",
TokenType.Bearer => $"Bearer {token}",
TokenType.Bearer => $"{token}", // Bearer tokens are not prefixed
_ => throw new ArgumentException(message: "Unknown OAuth token type.", paramName: nameof(tokenType)),
};
}


+ 10
- 2
src/Discord.Net.WebSocket/DiscordSocketApiClient.cs View File

@@ -234,8 +234,16 @@ namespace Discord.API
{
if (!_isExplicitUrl && _gatewayUrl == null)
{
var gatewayResponse = await GetBotGatewayAsync().ConfigureAwait(false);
_gatewayUrl = FormatGatewayUrl(gatewayResponse.Url);
if (AuthTokenType == TokenType.Bot)
{
var gatewayResponse = await GetBotGatewayAsync().ConfigureAwait(false);
_gatewayUrl = FormatGatewayUrl(gatewayResponse.Url);
}
else
{
var gatewayResponse = await GetGatewayAsync().ConfigureAwait(false);
_gatewayUrl = FormatGatewayUrl(gatewayResponse.Url);
}
}

gatewayUrl = _gatewayUrl;


Loading…
Cancel
Save