Guest User

Untitled

a guest
Feb 19th, 2021
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. public void getToken(String code_verifier) throws IOException, InterruptedException {
  2.  
  3.         String url = "https://myanimelist.net/v1/oauth2/token";
  4.         Scanner scanner = new Scanner(System.in);
  5.         String code = scanner.nextLine();
  6.         code = code.trim();
  7.  
  8.  
  9.         String code_encoded = URLEncoder.encode(code,StandardCharsets.UTF_8);
  10.         String code_verifier_encoded = URLEncoder.encode(code_verifier,StandardCharsets.UTF_8);
  11.         String client_id_encoded = URLEncoder.encode(CLIENT_ID,StandardCharsets.UTF_8);
  12.         String grant_type_encoded = URLEncoder.encode("authorization_code",StandardCharsets.UTF_8);
  13.  
  14.  
  15.         String requestBody = "code="+code_encoded+"&code_verifier="+code_verifier_encoded+"&grant_type"+grant_type_encoded+"&client_id"+client_id_encoded;
  16.         System.out.println(requestBody);
  17.  
  18.  
  19.         HttpClient client = HttpClient.newBuilder()
  20.                 .build();
  21.  
  22.  
  23.         HttpRequest request = HttpRequest.newBuilder()
  24.                 .POST(HttpRequest.BodyPublishers.ofString(requestBody))
  25.                 .uri(URI.create(url))
  26.                 .headers("Content-Type", "application/x-www-form-urlencoded")
  27.                 .build();
  28.  
  29.  
  30.         HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
  31.         System.out.println(response.statusCode());
  32.         System.out.println(response.body());
  33.  
  34.  
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment