Hi Vikrant,
These are arguments of the curl
command.
See more detailed explanation of the authentication process in the Authentication section.
In Java, you can authenticate in Jmix REST API using HttpClient
as follows:
public void test() {
authenticate("client", "secret", "admin", "admin");
}
private void authenticate(String client, String secret, String username, String password) {
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8080/oauth/token"))
.header("Authorization", getBasicAuthHeader(client, secret))
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(HttpRequest.BodyPublishers.ofString(getAuthRequestBody(username, password)))
.build();
try {
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == HttpStatus.OK.value()) {
String responseText = response.body();
System.out.println("Response: " + responseText);
// Using Jackson to convert JSON responce to a map:
Map<String, Object> map = new ObjectMapper().readValue(responseText, Map.class);
String accessToken = (String) map.get("access_token");
System.out.println("Access token: " + accessToken);
} else {
System.out.println("Error: " + response.statusCode());
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
private String getBasicAuthHeader(String client, String secret) {
String str = client + ":" + secret;
return "Basic " + Base64.getEncoder().encodeToString(str.getBytes());
}
private String getAuthRequestBody(String username, String password) {
return "grant_type=password&username=" + username + "&password=" + password;
}