Solved Mojang API - Playernames -> UUIDs

Discussion in 'Spigot Plugin Development' started by mohe2016, Jun 9, 2016.

Thread Status:
Not open for further replies.
  1. I'm trying to do something that's described at http://wiki.vg/Mojang_API#Playernames_-.3E_UUIDs.
    I've written the following code, but the server always returns 400 Bad Request. I don't know why.

    Code (Text):
    public static String[] getNicknames() {
            List<String> nicknames = new ArrayList<>();
            try {
                Scanner in = new Scanner(new File("nicknames.txt"));
                while (in.hasNextLine())
                    nicknames.add(in.nextLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return nicknames.toArray(new String[nicknames.size()]);
        }

        public static String[] getFreeNicknames() {
            int index = 0;
            String[] nicknames = getNicknames();
            List<String> freenicknames = new ArrayList<>();
            System.out.println(Arrays.toString(nicknames));
            try {
                while (index < nicknames.length) {
                    int maxcount = 0;
                    URL url = new URL(" https://api.mojang.com/profiles/minecraft");
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    con.setRequestProperty("Content-type", "application/json");
                    con.setRequestProperty("Accept", "application/json");
                    con.setRequestMethod("POST");
                    con.setDoOutput(true);
                    con.setDoInput(true);
                    OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
                    out.write("[\n");
                    System.out.print("[\n");
                    while (maxcount < 100 && index < nicknames.length) {
                        out.write("    \"" + nicknames[index] + "\"" + (index + 1 < nicknames.length ? "," : "") + "\n");
                        System.out.print("    \"" + nicknames[index] + "\"" + (index + 1 < nicknames.length ? "," : "") + "\n");
                        index++;
                        maxcount++;
                    }
                    out.write("]");
                    System.out.print("]");
                    System.out.println("Response Code: " + con.getResponseCode() + " " + con.getResponseMessage());

                    InputStreamReader in = new InputStreamReader(con.getInputStream());
                    int c;
                    while ((c = in.read()) != -1)
                        System.out.print((char) c);
                    out.close();
                    in.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return nicknames;
        }
    Can you help me, please?
     
  2. I am pretty sure the error is indicating that you have given some invalid JSON. Now this could be because your JSON formatting is off or that you haven't actually written any data at all! In the second case you should get an error message from the mojang api saying that the profileName is null.

    Try calling
    Code (Text):
    out.flush();
    before you start reading.

    Also I would suggest using the JSON classes provided by gson that are included with the spigot jar. That way you can clean this up a bit
    Code (Java):
    import com.google.gson.JsonArray;
            import com.google.gson.JsonElement;
            import com.google.gson.JsonParser;
            import com.google.gson.JsonPrimitive;

    [...]

            String[] nicknames = new String[]{"MrBlobman"};
            //Add the nicknames (up to 100 max) to the serializedNames list
            JsonArray serializedNames = new JsonArray();
            for (int i = 0; i < nicknames.length && i < 100; i++) {
                serializedNames.add(new JsonPrimitive(nicknames[i]));
            }

            URL url = new URL(" https://api.mojang.com/profiles/minecraft");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("Content-type", "application/json");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setDoInput(true);
            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());

            //Write the post parameters
            out.write(serializedNames.toString());
            out.flush();

            InputStreamReader in = new InputStreamReader(con.getInputStream());

            //Read the result
            JsonParser parser = new JsonParser();
            JsonElement result = parser.parse(in);
            //Temporary: print the result
            System.out.println(result.toString());

            out.close();
            in.close();
     
    • Informative Informative x 1
  3. Thank you very much, I'll try it later.



    EDIT: It works!
     
    #3 mohe2016, Jun 9, 2016
    Last edited: Jun 9, 2016
Thread Status:
Not open for further replies.