+package com.jacobcasper.fflogs;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.util.Base64;
+
+public class ApiAuthClient {
+ private static final String TOKEN_URL = "https://www.fflogs.com/oauth/token";
+
+ private final String clientId;
+ private final String clientSecret;
+ private final HttpClient client;
+
+ public ApiAuthClient(final String clientId, final String clientSecret) {
+ this.clientId = clientId;
+ this.clientSecret = clientSecret;
+ this.client = HttpClient.newHttpClient();
+ }
+
+ public String getToken() throws URISyntaxException, IOException, InterruptedException {
+ final var req = HttpRequest.newBuilder()
+ .POST(HttpRequest.BodyPublishers.ofString("grant_type=client_credentials"))
+ .header("Content-Type", "application/x-www-form-urlencoded")
+ .header("Authorization", getBasicAuthenticationHeader(clientId, clientSecret))
+ .uri(new URI(TOKEN_URL))
+ .build();
+
+ return client.send(req, HttpResponse.BodyHandlers.ofString()).body();
+ }
+
+ private static String getBasicAuthenticationHeader(String username, String password) {
+ String valueToEncode = username + ":" + password;
+ return "Basic " + Base64.getEncoder().encodeToString(valueToEncode.getBytes());
+ }
+}