Solved Mojang URL encoding

Discussion in 'Spigot Plugin Development' started by FrostedSnowman, Jun 3, 2018.

  1. FrostedSnowman

    Resource Staff

    ok so I am making requests here:

    http://wiki.vg/Mojang_API#Change_Skin

    however, it returns a 400 error code (bad request), because
    of this:
    [​IMG]

    I'm not sure how to encode the URL.

    I copied the exact one from the wiki
    (http%3A%2F%2Fassets.mojang.com%2FSkinTemplates%2Fsteve.png)
    and encoded it like this:

    Code (Java):
    UrlEscapers.urlFormParameterEscaper().escape("http://assets.mojang.com/SkinTemplates/steve.png")
    and i even outputted the value and got what was on the wiki, but still got the error code.

    The wiki says:
    The payload for this API consists of two url-encoded form fields (conjoined by '&').

    but I'm not sure what 'encoding' I'm suppose to specify here for the URL.
     
  2. The URL to Steve works fine in my browser. Show your full code please.
     
  3. FrostedSnowman

    Resource Staff

    @kangarko
    Code (Java):
    String authenticityToken = this.getAuthToken(this.minecraftAccount);

    try {
        System.out.println("Token: " + authenticityToken);

        HttpURLConnection httpURLConnection = Webs.getConnection(String.format(UPLOAD_SKIN_URL, UUIDTypeAdapter.fromUUID(uuid)));
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);

        httpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

        httpURLConnection.setRequestProperty("Accept-Charset", "UTF-8");

        httpURLConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0");

        httpURLConnection.addRequestProperty("Authorization", "Bearer " + authenticityToken);
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), StandardCharsets.UTF_8))) {
            writer.write("model=");
            if (!normal) {
                writer.write("slim");
            }

            writer.write("&url=" + UrlEscapers.urlFormParameterEscaper().escape("http://assets.mojang.com/SkinTemplates/steve.png"));
     
            if (httpURLConnection.getResponseCode() >= 400) {
                System.out.println(IOUtils.toString(httpURLConnection.getErrorStream(), StandardCharsets.UTF_8));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    } catch (Exception exception) {
        exception.printStackTrace();
    }
     
    UUID type adapter is provided by the mojang auth package itself, just removes the dashes.
     
  4. FrostedSnowman

    Resource Staff

    solved.

    solution: used postman to debug & inspected sent headers to mojang api
     
    • Friendly Friendly x 1