Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private final static String authserver = "https://authserver.mojang.com";
- public static void main(String[] args) throws Exception
- {
- System.out.println(authenticate("emilien.*******@*******.net", "e**********0"));
- System.out.println(authenticate("e**********@gmail.com", "s***********8"));
- }
- public static String authenticate(String username, String password) throws Exception {
- String genClientToken = UUID.randomUUID().toString();
- // Setting up json POST request
- String payload = "{\"agent\": {\"name\": \"Minecraft\",\"version\": 1},\"username\": \"" + username
- + "\",\"password\": \"" + password + "\",\"clientToken\": \"" + genClientToken + "\"}";
- String output = postReadURL(payload, new URL(authserver + "/authenticate"));
- // Setting up patterns
- String authBeg = "{\"accessToken\":\"";
- String authEnd = "\",\"clientToken\":\"";
- // What we are looking for
- String authtoken = getStringBetween(output, authBeg, authEnd);
- return authtoken;
- }
- private static String postReadURL(String payload, URL url) throws Exception {
- HttpURLConnection con = (HttpURLConnection) (url.openConnection());
- con.setReadTimeout(15000);
- con.setConnectTimeout(15000);
- con.setRequestMethod("POST");
- con.setRequestProperty("Content-Type", "application/json");
- con.setDoInput(true);
- con.setDoOutput(true);
- OutputStream out = con.getOutputStream();
- out.write(payload.getBytes("UTF-8"));
- out.close();
- BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
- String output = "";
- String line = null;
- while ((line = in.readLine()) != null)
- output += line;
- in.close();
- return output;
- }
- private static String getStringBetween(String base, String begin, String end) {
- Pattern patbeg = Pattern.compile(Pattern.quote(begin));
- Pattern patend = Pattern.compile(Pattern.quote(end));
- int resbeg = 0;
- int resend = base.length() - 1;
- Matcher matbeg = patbeg.matcher(base);
- if (matbeg.find())
- resbeg = matbeg.end();
- Matcher matend = patend.matcher(base);
- if (matend.find())
- resend = matend.start();
- return base.substring(resbeg, resend);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement