From a8b8ffc523f99bf5d0c6a603b17dff951f405067 Mon Sep 17 00:00:00 2001 From: Bryson Date: Mon, 20 Oct 2025 04:01:34 -0400 Subject: [PATCH] Fix crash when logging in with discord Updated discord API version and handling within deserialization structs --- DiscordOAuth.cs | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/DiscordOAuth.cs b/DiscordOAuth.cs index 2fb057d..39bb867 100644 --- a/DiscordOAuth.cs +++ b/DiscordOAuth.cs @@ -177,7 +177,7 @@ public static async void OAuthLoginRefresh(string refresh_token) try { - HttpResponseMessage response = await client.PostAsync("https://discord.com/api/v6/oauth2/token", new FormUrlEncodedContent(postDataDict)); + HttpResponseMessage response = await client.PostAsync("https://discord.com/api/v10/oauth2/token", new FormUrlEncodedContent(postDataDict)); if (!response.IsSuccessStatusCode) { @@ -193,7 +193,15 @@ public static async void OAuthLoginRefresh(string refresh_token) } string responseString = await response.Content.ReadAsStringAsync(); - Dictionary data = JsonConvert.DeserializeObject>(responseString); + JObject jsonResponse = JObject.Parse(responseString); + Dictionary data = new Dictionary + { + { "access_token", jsonResponse["access_token"]?.ToString() }, + { "refresh_token", jsonResponse["refresh_token"]?.ToString() }, + { "token_type", jsonResponse["token_type"]?.ToString() }, + { "expires_in", jsonResponse["expires_in"]?.ToString() }, + { "scope", jsonResponse["scope"]?.ToString() } + }; ProcessResponse(data); } } @@ -224,9 +232,17 @@ public static async void OAuthLoginResponse(string code) try { - HttpResponseMessage response = await client.PostAsync("https://discord.com/api/v6/oauth2/token", new FormUrlEncodedContent(postDataDict)); + HttpResponseMessage response = await client.PostAsync("https://discord.com/api/v10/oauth2/token", new FormUrlEncodedContent(postDataDict)); string responseString = await response.Content.ReadAsStringAsync(); - Dictionary data = JsonConvert.DeserializeObject>(responseString); + JObject jsonResponse = JObject.Parse(responseString); + Dictionary data = new Dictionary + { + { "access_token", jsonResponse["access_token"]?.ToString() }, + { "refresh_token", jsonResponse["refresh_token"]?.ToString() }, + { "token_type", jsonResponse["token_type"]?.ToString() }, + { "expires_in", jsonResponse["expires_in"]?.ToString() }, + { "scope", jsonResponse["scope"]?.ToString() } + }; ProcessResponse(data); } catch (HttpRequestException ex) @@ -272,17 +288,25 @@ private static async void FetchUserDetails() { try { - HttpResponseMessage userResponse = await client.GetAsync("https://discord.com/api/v6/users/@me"); + HttpResponseMessage userResponse = await client.GetAsync("https://discord.com/api/v10/users/@me"); string responseString = await userResponse.Content.ReadAsStringAsync(); - Dictionary data = JsonConvert.DeserializeObject>(responseString); + JObject jsonResponse = JObject.Parse(responseString); - if (data == null || !data.ContainsKey("username")) + if (jsonResponse == null || jsonResponse["username"] == null) { Logger.LogRow(Logger.LogType.Error, "Invalid user data received from OAuth server."); return; } - discordUserData = data; + discordUserData = new Dictionary + { + { "id", jsonResponse["id"]?.ToString() }, + { "username", jsonResponse["username"]?.ToString() }, + { "discriminator", jsonResponse["discriminator"]?.ToString() ?? "0" }, + { "avatar", jsonResponse["avatar"]?.ToString() }, + { "global_name", jsonResponse["global_name"]?.ToString() } + }; + FetchAccessCodes(); } catch (Exception ex)