Don’t Know If this is the correct approach but I fixed it using HttpClient (Java -11) -
@Override
public OktaToken getToken(String authToken) {
try {
HttpClient httpClient = HttpClient.newHttpClient();
URI uri= new URI(OktaTokenService.baseUrl+"/v1/token/" +"?client_id="+"my-client-id"+"&"
+"client_secret="+"my-client-secret"+"&"
+"grant_type="+"authorization_code"+"&"
+"redirect_uri="+"http://localhost:8181/api/v1/helloToken"+"&"
+"code="+authToken);
HttpRequest request = HttpRequest.newBuilder(
uri)
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(HttpRequest.BodyPublishers.ofString(uri.toString()))
.build();
HttpResponse<String> httpResponse = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(httpResponse.body());
JsonNode jsonNode = new ObjectMapper().readTree(httpResponse.body());
if (jsonNode !=null && !jsonNode.isEmpty() && !jsonNode.hasNonNull("error") ) {
OktaToken token = new OktaToken();
token.setTokenType(jsonNode.get("token_type").toString());
token.setExpiresInSec(Integer.parseInt(jsonNode.get("expires_in").toString()) );
token.setAccessToken(jsonNode.get("access_token").toString());
token.setScope(jsonNode.get("scope").toString());
token.setIdToken(jsonNode.get("id_token").toString());
return token;
}
} catch (URISyntaxException e) {
} catch (IOException e) {
} catch (InterruptedException e) {
}
return null;
}