use of com.github.games647.craftapi.model.auth.AuthRequest in project CraftAPI by games647.
the class MojangResolver method authenticate.
@Override
public Account authenticate(String email, String password) throws IOException, InvalidCredentialsException {
HttpURLConnection conn = getConnection(AUTH_URL);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
try (OutputStream out = conn.getOutputStream();
OutputStreamWriter outWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8);
BufferedWriter writer = new BufferedWriter(outWriter)) {
writer.append(gson.toJson(new AuthRequest(email, password)));
}
AuthResponse authResponse = parseRequest(conn, in -> readJson(in, AuthResponse.class));
return new Account(authResponse.getSelectedProfile(), authResponse.getAccessToken());
}
use of com.github.games647.craftapi.model.auth.AuthRequest in project ChangeSkin by games647.
the class MojangAuthApi method authenticate.
public Optional<Account> authenticate(String email, String password) {
try {
HttpURLConnection httpConnection = CommonUtil.getConnection(AUTH_URL);
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(httpConnection.getOutputStream(), StandardCharsets.UTF_8))) {
writer.append(gson.toJson(new AuthRequest(email, password)));
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), StandardCharsets.UTF_8))) {
AuthResponse authResponse = gson.fromJson(reader, AuthResponse.class);
return Optional.of(new Account(authResponse.getSelectedProfile(), authResponse.getAccessToken()));
}
} catch (IOException ex) {
logger.error("Failed to authenticate user: {}", email, ex);
}
return Optional.empty();
}
Aggregations