For client credentials, basic auth, I am using this code:
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
String auth = "user name"+":"+"password";
byte[] encodedAuth = java.util.Base64.getEncoder().encode(auth.getBytes());
httpHeaders.set("Authorization", "Basic " + new String(encodedAuth));
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
httpHeaders.setAccept(List.of(MediaType.APPLICATION_JSON));
MultiValueMap<String, String> reqBodyData = new LinkedMultiValueMap<>();
reqBodyData.add("grant_type","client_credentials");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(reqBodyData, httpHeaders);
LinkedHashMap<String, String> result = (LinkedHashMap<String, String>) restTemplate.postForEntity("<<auth token end point>>",
requestEntity, Object.class).getBody();
String accessToken = result.get("access_token");
Will need to find a way for pub-private key insteasd of basic auth.