Jump to content

How to log into Minecraft inside of Eclipse


Sun_Minecraft

Recommended Posts

I was trying to log into Minecraft inside of Eclipse so could try some mods out on servers. 
I’ve tried adding

—username username

And —password password

into the program arguments but in the console it shows that the argument password is being ignored and I’m not logged in.

Does anyone know any other way to log into my Minecraft Account inside of Eclipse?

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...
On 3/5/2022 at 8:28 AM, Luis_ST said:

this is no longer possible with a Mojang Account,
irrc you need to use the access token form the Launcher

also why do you want to login in dev environment?
there is no reason to be logged in

This isn't true HOWEVER its super complicated to do and you have to change the command every time. 

Below is a code snippet from a custom minecraft launcher I'm working on so there may be some unnecessary steps focus mainly on the HTTP connections, request types, and request bodys/url endpoints. Forewarning this is incredibly complicated and i would advise against it. If you need to test your mod on a server just put it in offline mode.
 

Spoiler


public static void signUserIn() throws Exception {	
        String content;
        JSONObject playerJson = new JSONObject();
        if (AppUtils.accountsStatus == 1) {
            SignInFrame sif = new SignInFrame().showSignIn();
            while (sif.code == null) {
                Thread.sleep(1);
            }

            content = String.format(
                    "client_id=%s" +
                            "&code=%s" +
                            "&grant_type=authorization_code" +
                            "&redirect_uri=%s",
                    "[Redacted]", //You'll need to regester an app with Azure to get this code
                    sif.code,
                    "https://login.live.com/oauth20_desktop.srf"
            );
        } else {
            content = String.format(
                    "client_id=%s" +
                            "&refresh_token=%s" +
                            "&grant_type=refresh_token" +
                            "&redirect_uri=%s",
                    "[Redacted]",
                    AppUtils.accountsInfo.getJSONObject(AppUtils.activeAccount).getString("refresh_token"),
                    "https://login.live.com/oauth20_desktop.srf"
            );
        }

        CloseableHttpClient client = HttpClients.createDefault();

        HttpPost msaLogin = new HttpPost("https://login.live.com/oauth20_token.srf");
        msaLogin.setEntity(new StringEntity(content, ContentType.APPLICATION_FORM_URLENCODED));

        CloseableHttpResponse msaResponse = client.execute(msaLogin);
        JSONObject msaResponseJSON = new JSONObject(new BufferedReader(new InputStreamReader(msaResponse.getEntity().getContent())).lines().collect(Collectors.joining()));

        content = String.format(
                "{" +
                        "\"Properties\": {" +
                        "\"AuthMethod\": \"RPS\"," +
                        "\"SiteName\": \"user.auth.xboxlive.com\"," +
                        "\"RpsTicket\": \"d=%s\"" +
                        "}," +
                        "\"RelyingParty\": \"http://auth.xboxlive.com\"," +
                        "\"TokenType\": \"JWT\"" +
                        "}",
                msaResponseJSON.getString("access_token")
        );

        HttpPost xblLogin = new HttpPost("https://user.auth.xboxlive.com/user/authenticate");
        xblLogin.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
        xblLogin.setHeader(HttpHeaders.ACCEPT, "application/json");

        CloseableHttpResponse xblResponse = client.execute(xblLogin);
        JSONObject xblResponseJSON = new JSONObject(new BufferedReader(new InputStreamReader(xblResponse.getEntity().getContent())).lines().collect(Collectors.joining()));

        content = String.format(
                "{\n" +
                        "\"Properties\": {" +
                        "\"SandboxId\": \"RETAIL\"," +
                        "\"UserTokens\": [" +
                        "\"%s\"" +
                        "]" +
                        "}," +
                        "\"RelyingParty\": \"rp://api.minecraftservices.com/\"," +
                        "\"TokenType\": \"JWT\"" +
                        "}",
                xblResponseJSON.getString("Token")
        );

        HttpPost xstsPost = new HttpPost("https://xsts.auth.xboxlive.com/xsts/authorize");
        xstsPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
        xstsPost.setHeader(new BasicHeader(HttpHeaders.ACCEPT, "application/json"));

        CloseableHttpResponse xstsResponse = client.execute(xstsPost);
        JSONObject xstsResponseJSON = new JSONObject(new BufferedReader(new InputStreamReader(xstsResponse.getEntity().getContent())).lines().collect(Collectors.joining()));

        content = String.format(
                "{" +
                        "\"identityToken\" : \"XBL3.0 x=%s;%s\"," +
                        "\"ensureLegacyEnabled\" : true" +
                        "}",
                xstsResponseJSON.getJSONObject("DisplayClaims").getJSONArray("xui").getJSONObject(0).getString("uhs"),
                xstsResponseJSON.getString("Token")
        );

        HttpPost mctPost = new HttpPost("https://api.minecraftservices.com/authentication/login_with_xbox");
        mctPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));

        CloseableHttpResponse mctResponse = client.execute(mctPost);
        JSONObject mctResponseJSON = new JSONObject(new BufferedReader(new InputStreamReader(mctResponse.getEntity().getContent())).lines().collect(Collectors.joining()));

        HttpGet mcGet = new HttpGet("https://api.minecraftservices.com/minecraft/profile");
        mcGet.setHeader(new BasicHeader(HttpHeaders.AUTHORIZATION, String.format(
                "Bearer %s",
                mctResponseJSON.getString("access_token")
        )));
        CloseableHttpResponse mcResponse = client.execute(mcGet);
        JSONObject mcResponseJSON = new JSONObject(new BufferedReader(new InputStreamReader(mcResponse.getEntity().getContent())).lines().collect(Collectors.joining()));
        System.out.println(mcResponseJSON.toString());

        JSONObject playerInfo = new JSONObject();
        playerInfo.put("refresh_token", msaResponseJSON.getString("refresh_token"));
        playerInfo.put("ms_id", mctResponseJSON.getString("username"));
        for (Object sk : mcResponseJSON.getJSONArray("skins")) {
            JSONObject skj = ((JSONObject) sk);
            if (skj.getString("state").equalsIgnoreCase("active")) {
                playerInfo.put("skin_url", skj.getString("url"));
                break;
            }
        }
        playerInfo.put("user_name", mcResponseJSON.getString("name"));
        AppUtils.accountsInfo.put(mcResponseJSON.getString("id"), playerInfo);
        AppUtils.accountsInfo.put("active_account", mcResponseJSON.getString("id"));
        AppUtils.activeAccount = mcResponseJSON.getString("id");
        AppUtils.accountsStatus = 0;
        AppUtils.updateSkinImage();
    }

    public static void signSecondaryUserIn() throws Exception {
        String content;
        JSONObject playerJson = new JSONObject();
        SignInFrame sif = new SignInFrame().showSignIn();
        while (sif.code == null) {
            Thread.sleep(1);
        }

        content = String.format(
                "client_id=%s" +
                        "&code=%s" +
                        "&grant_type=authorization_code" +
                        "&redirect_uri=%s",
                "60ea6a7c-d173-43bd-97af-8b8ea1108a31",
                sif.code,
                "https://login.live.com/oauth20_desktop.srf"
        );

        CloseableHttpClient client = HttpClients.createDefault();

        HttpPost msaLogin = new HttpPost("https://login.live.com/oauth20_token.srf");
        msaLogin.setEntity(new StringEntity(content, ContentType.APPLICATION_FORM_URLENCODED));

        CloseableHttpResponse msaResponse = client.execute(msaLogin);
        JSONObject msaResponseJSON = new JSONObject(new BufferedReader(new InputStreamReader(msaResponse.getEntity().getContent())).lines().collect(Collectors.joining()));

        content = String.format(
                "{" +
                        "\"Properties\": {" +
                        "\"AuthMethod\": \"RPS\"," +
                        "\"SiteName\": \"user.auth.xboxlive.com\"," +
                        "\"RpsTicket\": \"d=%s\"" +
                        "}," +
                        "\"RelyingParty\": \"http://auth.xboxlive.com\"," +
                        "\"TokenType\": \"JWT\"" +
                        "}",
                msaResponseJSON.getString("access_token")
        );

        HttpPost xblLogin = new HttpPost("https://user.auth.xboxlive.com/user/authenticate");
        xblLogin.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
        xblLogin.setHeader(HttpHeaders.ACCEPT, "application/json");

        CloseableHttpResponse xblResponse = client.execute(xblLogin);
        JSONObject xblResponseJSON = new JSONObject(new BufferedReader(new InputStreamReader(xblResponse.getEntity().getContent())).lines().collect(Collectors.joining()));

        content = String.format(
                "{\n" +
                        "\"Properties\": {" +
                        "\"SandboxId\": \"RETAIL\"," +
                        "\"UserTokens\": [" +
                        "\"%s\"" +
                        "]" +
                        "}," +
                        "\"RelyingParty\": \"rp://api.minecraftservices.com/\"," +
                        "\"TokenType\": \"JWT\"" +
                        "}",
                xblResponseJSON.getString("Token")
        );

        HttpPost xstsPost = new HttpPost("https://xsts.auth.xboxlive.com/xsts/authorize");
        xstsPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
        xstsPost.setHeader(new BasicHeader(HttpHeaders.ACCEPT, "application/json"));

        CloseableHttpResponse xstsResponse = client.execute(xstsPost);
        JSONObject xstsResponseJSON = new JSONObject(new BufferedReader(new InputStreamReader(xstsResponse.getEntity().getContent())).lines().collect(Collectors.joining()));

        content = String.format(
                "{" +
                        "\"identityToken\" : \"XBL3.0 x=%s;%s\"," +
                        "\"ensureLegacyEnabled\" : true" +
                        "}",
                xstsResponseJSON.getJSONObject("DisplayClaims").getJSONArray("xui").getJSONObject(0).getString("uhs"),
                xstsResponseJSON.getString("Token")
        );

        HttpPost mctPost = new HttpPost("https://api.minecraftservices.com/authentication/login_with_xbox");
        mctPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));

        CloseableHttpResponse mctResponse = client.execute(mctPost);
        JSONObject mctResponseJSON = new JSONObject(new BufferedReader(new InputStreamReader(mctResponse.getEntity().getContent())).lines().collect(Collectors.joining()));

        HttpGet mcGet = new HttpGet("https://api.minecraftservices.com/minecraft/profile");
        mcGet.setHeader(new BasicHeader(HttpHeaders.AUTHORIZATION, String.format(
                "Bearer %s",
                mctResponseJSON.getString("access_token")
        )));
        CloseableHttpResponse mcResponse = client.execute(mcGet);
        JSONObject mcResponseJSON = new JSONObject(new BufferedReader(new InputStreamReader(mcResponse.getEntity().getContent())).lines().collect(Collectors.joining()));
        System.out.println(mcResponseJSON.toString());

        JSONObject playerInfo = new JSONObject();
        playerInfo.put("refresh_token", msaResponseJSON.getString("refresh_token"));
        playerInfo.put("ms_id", mctResponseJSON.getString("username"));
        for (Object sk : mcResponseJSON.getJSONArray("skins")) {
            JSONObject skj = ((JSONObject) sk);
            if (skj.getString("state").equalsIgnoreCase("active")) {
                playerInfo.put("skin_url", skj.getString("url"));
                break;
            }
        }
        playerInfo.put("user_name", mcResponseJSON.getString("name"));
        AppUtils.accountsInfo.put(mcResponseJSON.getString("id"), playerInfo);
        AppUtils.accountsInfo.put("active_account", mcResponseJSON.getString("id"));
        AppUtils.activeAccount = mcResponseJSON.getString("id");
        AppUtils.accountsStatus = 0;
        AppUtils.updateSkinImage();

Sif is a JavaFX panel with Microsoft website embedded into it:

public class SignInFrame extends JFrame {

    WebEngine we;
    public String code;
    WebView wv;

    public SignInFrame showSignIn() throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        this.setSize(450, 500);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setTitle("Sign in to minecraft");
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException classNotFoundException) {
                    classNotFoundException.printStackTrace();
                }
                JOptionPane.showMessageDialog(new JFrame(), "You must sign in to continue", "Sign In", JOptionPane.WARNING_MESSAGE);

            }
        });


        String redirectUrl = URLEncoder.encode("https://login.live.com/oauth20_desktop.srf", StandardCharsets.UTF_8);
        URL url = new URL("https://login.live.com/oauth20_authorize.srf?client_id=[Redacted]&response_type=code&redirect_uri=" + redirectUrl + "&scope=XboxLive.signin%20offline_access");

        JFXPanel jfxPanel = new JFXPanel();
        this.add(jfxPanel);
        Platform.runLater(() -> {
            Platform.setImplicitExit(true);
            wv = new WebView();
            we = wv.getEngine();
            we.setJavaScriptEnabled(true);
            Scene sc = new Scene(wv);
            jfxPanel.setScene(sc);
            we.load(url.toString());


            we.locationProperty().addListener((obs, oldLocation, newLocation)->{
                if (newLocation.contains("https://login.live.com/oauth20_desktop.srf")) {
                    code = newLocation.split("code=")[1].split("&")[0];
                    this.dispose();
                }
            });
        });

        return this;
    }
}

Learn more about API authentication here: https://mojang-api-docs.netlify.app/authentication/msa.html

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.