From cf278c11f8c6a278aba43c689dbe54859b976943 Mon Sep 17 00:00:00 2001 From: ShirosakiMio <852468399@qq.com> Date: Mon, 19 Aug 2024 09:41:05 +0800 Subject: [PATCH] add tip for microsoft login Xbox 400 error --- .../com/tungsten/fcl/setting/Accounts.java | 2 + FCL/src/main/res/values-zh/strings.xml | 1 + FCL/src/main/res/values/strings.xml | 1 + .../auth/microsoft/MicrosoftService.java | 56 +++++++++++-------- .../tungsten/fclcore/util/io/HttpRequest.java | 11 +++- .../util/io/ResponseCodeException.java | 7 +++ 6 files changed, 53 insertions(+), 25 deletions(-) diff --git a/FCL/src/main/java/com/tungsten/fcl/setting/Accounts.java b/FCL/src/main/java/com/tungsten/fcl/setting/Accounts.java index a3319bb3..7c43b176 100644 --- a/FCL/src/main/java/com/tungsten/fcl/setting/Accounts.java +++ b/FCL/src/main/java/com/tungsten/fcl/setting/Accounts.java @@ -471,6 +471,8 @@ public final class Accounts { } else { return context.getString(R.string.account_methods_microsoft_error_unknown); } + } else if (exception instanceof MicrosoftService.XBox400Exception) { + return context.getString(R.string.account_methods_microsoft_error_wrong_verify_method); } else if (exception instanceof MicrosoftService.NoMinecraftJavaEditionProfileException) { return context.getString(R.string.account_methods_microsoft_error_no_character); } else if (exception instanceof MicrosoftService.NoXuiException) { diff --git a/FCL/src/main/res/values-zh/strings.xml b/FCL/src/main/res/values-zh/strings.xml index 8f19091b..b392bad3 100644 --- a/FCL/src/main/res/values-zh/strings.xml +++ b/FCL/src/main/res/values-zh/strings.xml @@ -67,6 +67,7 @@ Xbox Live 不支持您所在的国家/地区。 你的微软账户没有链接到 Xbox 账户,请先创建。 登录失败 + 请在 Microsoft 账户登陆页面使用账户 + 密码登录。请不要使用验证码登录。 你的账户尚未获取 Minecraft : Java Edition 请检查并确保年龄设置大于 18 岁。 Microsoft 账户登录完成 diff --git a/FCL/src/main/res/values/strings.xml b/FCL/src/main/res/values/strings.xml index 2a4d0471..e9728040 100644 --- a/FCL/src/main/res/values/strings.xml +++ b/FCL/src/main/res/values/strings.xml @@ -81,6 +81,7 @@ Xbox Live is not available in your current country/region. Your Microsoft account does not have a linked Xbox account yet. Please create one before continuing. Failed to log in + Please log in using your account and password on the Microsoft account login page. Please do not use a verification code to log in. Your account does not own the Minecraft Java Edition.\nThe game profile may not have been created. Please check if the age indicated in your account settings is at least 18 years old. If not and you believe this is an error, you can go to official website to change it. Microsoft account authorization is now completed. diff --git a/FCLCore/src/main/java/com/tungsten/fclcore/auth/microsoft/MicrosoftService.java b/FCLCore/src/main/java/com/tungsten/fclcore/auth/microsoft/MicrosoftService.java index 733ad1fe..8e0e8b79 100644 --- a/FCLCore/src/main/java/com/tungsten/fclcore/auth/microsoft/MicrosoftService.java +++ b/FCLCore/src/main/java/com/tungsten/fclcore/auth/microsoft/MicrosoftService.java @@ -118,30 +118,39 @@ public class MicrosoftService { } private MicrosoftSession authenticateViaLiveAccessToken(String liveAccessToken, String liveRefreshToken) throws IOException, JsonParseException, AuthenticationException { - // Authenticate with XBox Live - XBoxLiveAuthenticationResponse xboxResponse = HttpRequest - .POST("https://user.auth.xboxlive.com/user/authenticate") - .json(mapOf( - pair("Properties", - mapOf(pair("AuthMethod", "RPS"), pair("SiteName", "user.auth.xboxlive.com"), - pair("RpsTicket", "d=" + liveAccessToken))), - pair("RelyingParty", "http://auth.xboxlive.com"), pair("TokenType", "JWT"))) - .retry(5) - .accept("application/json").getJson(XBoxLiveAuthenticationResponse.class); + String uhs; + XBoxLiveAuthenticationResponse xboxResponse, minecraftXstsResponse; + try { + // Authenticate with XBox Live + xboxResponse = HttpRequest + .POST("https://user.auth.xboxlive.com/user/authenticate") + .json(mapOf( + pair("Properties", + mapOf(pair("AuthMethod", "RPS"), pair("SiteName", "user.auth.xboxlive.com"), + pair("RpsTicket", "d=" + liveAccessToken))), + pair("RelyingParty", "http://auth.xboxlive.com"), pair("TokenType", "JWT"))) + .retry(5) + .accept("application/json") + .getJson(XBoxLiveAuthenticationResponse.class); - String uhs = getUhs(xboxResponse, null); + uhs = getUhs(xboxResponse, null); - // Authenticate Minecraft with XSTS - XBoxLiveAuthenticationResponse minecraftXstsResponse = HttpRequest - .POST("https://xsts.auth.xboxlive.com/xsts/authorize") - .json(mapOf( - pair("Properties", - mapOf(pair("SandboxId", "RETAIL"), - pair("UserTokens", Collections.singletonList(xboxResponse.token)))), - pair("RelyingParty", "rp://api.minecraftservices.com/"), pair("TokenType", "JWT"))) - .ignoreHttpErrorCode(401) - .retry(5) - .getJson(XBoxLiveAuthenticationResponse.class); + minecraftXstsResponse = HttpRequest + .POST("https://xsts.auth.xboxlive.com/xsts/authorize") + .json(mapOf( + pair("Properties", + mapOf(pair("SandboxId", "RETAIL"), + pair("UserTokens", Collections.singletonList(xboxResponse.token)))), + pair("RelyingParty", "rp://api.minecraftservices.com/"), pair("TokenType", "JWT"))) + .ignoreHttpErrorCode(401) + .retry(5) + .getJson(XBoxLiveAuthenticationResponse.class); + } catch (ResponseCodeException e) { + if (e.getResponseCode() == 400) { + throw new XBox400Exception(); + } + throw e; + } getUhs(minecraftXstsResponse, uhs); @@ -298,6 +307,9 @@ public class MicrosoftService { public static final long ADD_FAMILY = 2148916238L; } + public static class XBox400Exception extends AuthenticationException { + } + public static class NoMinecraftJavaEditionProfileException extends AuthenticationException { } diff --git a/FCLCore/src/main/java/com/tungsten/fclcore/util/io/HttpRequest.java b/FCLCore/src/main/java/com/tungsten/fclcore/util/io/HttpRequest.java index 016c5cdd..1abfd155 100644 --- a/FCLCore/src/main/java/com/tungsten/fclcore/util/io/HttpRequest.java +++ b/FCLCore/src/main/java/com/tungsten/fclcore/util/io/HttpRequest.java @@ -189,13 +189,18 @@ public abstract class HttpRequest { os.write(bytes); } + URL url = new URL(this.url); + if (responseCodeTester != null) { - responseCodeTester.accept(new URL(url), con.getResponseCode()); + responseCodeTester.accept(url, con.getResponseCode()); } else { if (con.getResponseCode() / 100 != 2) { if (!ignoreHttpCode && !toleratedHttpCodes.contains(con.getResponseCode())) { - String data = NetworkUtils.readData(con); - throw new ResponseCodeException(new URL(url), con.getResponseCode(), data); + try { + throw new ResponseCodeException(url, con.getResponseCode(), NetworkUtils.readData(con)); + } catch (IOException e) { + throw new ResponseCodeException(url, con.getResponseCode(), e); + } } } } diff --git a/FCLCore/src/main/java/com/tungsten/fclcore/util/io/ResponseCodeException.java b/FCLCore/src/main/java/com/tungsten/fclcore/util/io/ResponseCodeException.java index 14690b8c..c9f2fd30 100644 --- a/FCLCore/src/main/java/com/tungsten/fclcore/util/io/ResponseCodeException.java +++ b/FCLCore/src/main/java/com/tungsten/fclcore/util/io/ResponseCodeException.java @@ -33,6 +33,13 @@ public final class ResponseCodeException extends IOException { this.data = null; } + public ResponseCodeException(URL url, int responseCode, Throwable cause) { + super("Unable to request url " + url + ", response code: " + responseCode, cause); + this.url = url; + this.responseCode = responseCode; + this.data = null; + } + public ResponseCodeException(URL url, int responseCode, String data) { super("Unable to request url " + url + ", response code: " + responseCode + ", data: " + data); this.url = url;