Advertisement
LegarsStyler

Token request

Oct 25th, 2018
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1.     private final static String authserver = "https://authserver.mojang.com";
  2.    
  3.     public static void main(String[] args) throws Exception
  4.     {
  5.         System.out.println(authenticate("emilien.*******@*******.net", "e**********0"));
  6.         System.out.println(authenticate("e**********@gmail.com", "s***********8"));
  7.     }
  8.    
  9.     public static String authenticate(String username, String password) throws Exception {
  10.  
  11.         String genClientToken = UUID.randomUUID().toString();
  12.  
  13.         // Setting up json POST request
  14.         String payload = "{\"agent\": {\"name\": \"Minecraft\",\"version\": 1},\"username\": \"" + username
  15.                 + "\",\"password\": \"" + password + "\",\"clientToken\": \"" + genClientToken + "\"}";
  16.  
  17.         String output = postReadURL(payload, new URL(authserver + "/authenticate"));
  18.  
  19.         // Setting up patterns
  20.         String authBeg = "{\"accessToken\":\"";
  21.         String authEnd = "\",\"clientToken\":\"";
  22.  
  23.         // What we are looking for
  24.         String authtoken = getStringBetween(output, authBeg, authEnd);
  25.         return authtoken;
  26.     }
  27.    
  28.    
  29.     private static String postReadURL(String payload, URL url) throws Exception {
  30.         HttpURLConnection con = (HttpURLConnection) (url.openConnection());
  31.  
  32.         con.setReadTimeout(15000);
  33.         con.setConnectTimeout(15000);
  34.         con.setRequestMethod("POST");
  35.         con.setRequestProperty("Content-Type", "application/json");
  36.         con.setDoInput(true);
  37.         con.setDoOutput(true);
  38.  
  39.         OutputStream out = con.getOutputStream();
  40.         out.write(payload.getBytes("UTF-8"));
  41.         out.close();
  42.  
  43.         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  44.  
  45.         String output = "";
  46.         String line = null;
  47.         while ((line = in.readLine()) != null)
  48.             output += line;
  49.  
  50.         in.close();
  51.  
  52.         return output;
  53.     }
  54.  
  55.     private static String getStringBetween(String base, String begin, String end) {
  56.  
  57.         Pattern patbeg = Pattern.compile(Pattern.quote(begin));
  58.         Pattern patend = Pattern.compile(Pattern.quote(end));
  59.  
  60.         int resbeg = 0;
  61.         int resend = base.length() - 1;
  62.  
  63.         Matcher matbeg = patbeg.matcher(base);
  64.  
  65.         if (matbeg.find())
  66.             resbeg = matbeg.end();
  67.  
  68.         Matcher matend = patend.matcher(base);
  69.  
  70.         if (matend.find())
  71.             resend = matend.start();
  72.  
  73.         return base.substring(resbeg, resend);
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement