Майкрософт авторизация + майнкрафт | Java

Пытался создать тестовую программу которая авторизировала бы пользователя (через майкрософт используя Azure) ну и запускало бы с этими данными лицензионный майн. Подскажите будет ли вообще так работать? + Как исправить ошибочку

Error during Microsoft authentication: java.lang.ClassCastException: class java.util.Collections$SingletonList cannot be cast to class java.lang.String (java.util.Collections$SingletonList and java.lang.String are in module java.base of loader 'bootstrap')

Код:

import com.microsoft.aad.msal4j.*;

import java.awt.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class MinecraftLauncherWithAuth {
    private static final int PORT = 8080;
    private static String gameFilePath;

    public static void main(String[] args) throws Exception {
        setupHttpServer();

        String authUrl = "https://login.live.com/oauth20_authorize.srf?client_id=_id&response_type=code&scope=user.read&redirect_uri=http://localhost:" + PORT;
        Desktop.getDesktop().browse(new URI(authUrl));
    }

    private static void setupHttpServer() throws IOException {
        com.sun.net.httpserver.HttpServer server = com.sun.net.httpserver.HttpServer.create(new java.net.InetSocketAddress(PORT), 0);
        server.createContext("/", new AuthHandler());
        server.setExecutor(null);
        server.start();
    }

    static class AuthHandler implements com.sun.net.httpserver.HttpHandler {
        @Override
        public void handle(com.sun.net.httpserver.HttpExchange exchange) throws IOException {
            String response = "Authorization successful. Please check the console for the token.";
            exchange.sendResponseHeaders(200, response.getBytes().length);
            OutputStream os = exchange.getResponseBody();
            os.write(response.getBytes());
            os.close();

            URI requestURI = exchange.getRequestURI();
            if (requestURI.getQuery() != null && requestURI.getQuery().contains("code=")) {
                // Get the code as a single-element list
                List<String> codeList = Collections.singletonList(requestURI.getQuery().split("=")[1]);
                acquireMicrosoftToken(codeList);
            }
            exchange.close();
        }
    }

    private static void acquireMicrosoftToken(List<String> code) {
        System.out.println("Code type: " + code.getClass().getName());
        try {
            PublicClientApplication app = PublicClientApplication.builder("client_id").build();

            if (code.size() > 1) {
                throw new RuntimeException("Unexpected number of codes received.");
            }

            String codeString = code.get(0);

            AuthorizationCodeParameters parameters = AuthorizationCodeParameters
                    .builder(codeString, new URI("http://localhost"))
                    .scopes(new HashSet<>(Arrays.asList("user.read")))
                    .build();

            IAuthenticationResult result = app.acquireToken(parameters).join();

            String accessToken = result.accessToken();
            if (accessToken != null && !accessToken.isEmpty()) {
                downloadUnpackStartGame(accessToken);
            } else {
                throw new RuntimeException("Access token is empty or null.");
            }
        } catch (Exception e) {
            System.err.println("Error during Microsoft authentication: " + e.getMessage());
        }
    }

    private static void downloadUnpackStartGame(String token) {
        try {
            System.out.println("Downloading Minecraft with token: " + token);
            String gameUrl = "https://launcher.mojang.com/v1/objects/5a00c353fff0c870f032a31e7cdfbecb11cd06f9/client.jar";
            gameFilePath = "minecraft-client-1.16.5.jar";

            URL url = new URL(gameUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            try (InputStream in = connection.getInputStream(); FileOutputStream out = new FileOutputStream(gameFilePath)) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = in.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
            }
            System.out.println("Game downloaded successfully.");

            try (ZipFile zipFile = new ZipFile(gameFilePath)) {
                Enumeration<? extends ZipEntry> entries = zipFile.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = entries.nextElement();
                    if (entry.getName().endsWith(".jar")) {
                        try (InputStream input = zipFile.getInputStream(entry); FileOutputStream output = new FileOutputStream(entry.getName())) {
                            byte[] buffer = new byte[1024];
                            int bytesRead;
                            while ((bytesRead = input.read(buffer)) != -1) {
                                output.write(buffer, 0, bytesRead);
                            }
                            System.out.println("Successfully extracted: " + entry.getName());
                        }
                    }
                }
            }

            System.out.println("Game files extracted successfully.");

            // Start the game
            ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "minecraft-client-1.16.5.jar");
            Process process = processBuilder.start();
            System.out.println("Minecraft launched successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Каюсь, использовал гпт


Ответы (0 шт):