Search in sources :

Example 1 with EpicAuthorizationExchange

use of com.xilixir.fortniteapi.v2.Epic.EpicAuthorizationExchange in project FortniteAPI by Xilixir.

the class FortniteAPI method authenticate.

public void authenticate() throws IOException {
    log("Starting Authentication...");
    Gson gson = new Gson();
    // initial login
    GenericUrl tokenUrl = new GenericUrl(authTokenEndpoint);
    String initialLoginPayload = payloadBuilder(new String[] { "grant_type", "username", "password", "includePerms" }, new String[] { "password", encode(credentials.getEmail()), encode(credentials.getPassword()), "true" });
    ByteArrayContent initialLoginContent = new ByteArrayContent("application/x-www-form-urlencoded", initialLoginPayload.getBytes());
    HttpRequest initialLoginRequest = factory.buildPostRequest(tokenUrl, initialLoginContent);
    initialLoginRequest.getHeaders().setAuthorization("basic " + credentials.getBase64hashPair());
    // initial login response
    String initialLoginResponse = initialLoginRequest.execute().parseAsString();
    this.auth = gson.fromJson(initialLoginResponse, EpicAuthorization.class);
    // exchange-1
    String authExchangeEndpoint = "https://account-public-service-prod03.ol.epicgames.com/account/api/oauth/exchange";
    GenericUrl exchangeUrl = new GenericUrl(authExchangeEndpoint);
    HttpRequest exchangeRequest = factory.buildGetRequest(exchangeUrl);
    exchangeRequest.getHeaders().setAuthorization("bearer " + this.auth.getAccessToken());
    // exchange-1 response
    String exchangeResponse = exchangeRequest.execute().parseAsString();
    EpicAuthorizationExchange exchange = gson.fromJson(exchangeResponse, EpicAuthorizationExchange.class);
    // exchange-2
    String exchangeTokenPayload = payloadBuilder(new String[] { "grant_type", "exchange_code", "includePerms", "token_type" }, new String[] { "exchange_code", exchange.getCode(), "true", "eg1" });
    ByteArrayContent exchangeTokenContent = new ByteArrayContent("application/x-www-form-urlencoded", exchangeTokenPayload.getBytes());
    HttpRequest exchangeTokenRequest = factory.buildPostRequest(tokenUrl, exchangeTokenContent);
    exchangeTokenRequest.getHeaders().setAuthorization("basic " + credentials.getBase64hashPairClient());
    // exchange-2 response
    String exchangeTokenResponse = exchangeTokenRequest.execute().parseAsString();
    this.auth = gson.fromJson(exchangeTokenResponse, EpicAuthorization.class);
    log("Authenticated successfully!");
    this.scheduleTokenRefresh();
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) EpicAuthorization(com.xilixir.fortniteapi.v2.Epic.EpicAuthorization) Gson(com.google.gson.Gson) GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayContent(com.google.api.client.http.ByteArrayContent) EpicAuthorizationExchange(com.xilixir.fortniteapi.v2.Epic.EpicAuthorizationExchange)

Aggregations

ByteArrayContent (com.google.api.client.http.ByteArrayContent)1 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpRequest (com.google.api.client.http.HttpRequest)1 Gson (com.google.gson.Gson)1 EpicAuthorization (com.xilixir.fortniteapi.v2.Epic.EpicAuthorization)1 EpicAuthorizationExchange (com.xilixir.fortniteapi.v2.Epic.EpicAuthorizationExchange)1