Trying to get access token using Webclient in a spring boot application

I am new to Okta world.

I am trying to get access token using a POST call in a spring boot app but every time I get “ Connection refused: no further information”.

Below is what I have done:-

  1. Created an application using okta developer console.

  2. I have tested my resources ( another spring boot application) by following authorization code flow. It means I am getting the auth code from oidcdebugger site and using that auth code when I trigger below command in HTTPie demo site , I get the access token. PFB command -
    " http -f https://{my-domain}/oauth2/default/v1/token
    code={auth code got from }
    redirect_uri=http://localhost:8181/api/v1/helloToken "

  3. I can even get access token from POSTMAN too , where I set header ‘content-type’ as ‘application/x-www-form-urlencoded’ and do a POST call to below URL -
    https://{my-doamin}/oauth2/default/v1/token?client_id={my-client-id}&client_secret={client-secret}&grant_type=authorization_code&redirect_uri=http://localhost:8181/api/v1/helloToken&code={auth code}

  4. Now I want to get token from Spring Boot app using POST call, for that I have done below code

My ServiceImpl

public class OktaTokenServiceImpl implements OktaTokenService{

public OktaToken getToken(String authToken) {
	WebClient webClient = WebClient
	URI uri;
	try {
		uri = new URI("/v1/token/" +"?client_id="+"my-client-id"+"&"
		System.out.println("complete URL" +uri.toString());
		Mono<OktaToken> oktaToken = webClient
		if (oktaToken.blockOptional().isPresent()) {
			return oktaToken.blockOptional().get();
		} else return null;
	} catch (URISyntaxException e) {
		// TODO Auto-generated catch block
	return null;


My URL which I hit from POSTMAN


Exception which I get

2021-04-25 18:37:25.860 ERROR 15288 — [nio-8181-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: no further information: /; nested exception is$AnnotatedConnectException: Connection refused: no further information: /] with root cause Connection refused: no further information

Please help me to get this resolved and please share your experience if you have faced same issue.

Don’t Know If this is the correct approach but I fixed it using HttpClient (Java -11) -

	public OktaToken getToken(String authToken) {
		try  {
			HttpClient httpClient = HttpClient.newHttpClient();
			URI uri= new URI(OktaTokenService.baseUrl+"/v1/token/" +"?client_id="+"my-client-id"+"&"
			HttpRequest request = HttpRequest.newBuilder(
					.header("Content-Type", "application/x-www-form-urlencoded")
			HttpResponse<String> httpResponse = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
			JsonNode jsonNode = new ObjectMapper().readTree(httpResponse.body());
			if (jsonNode !=null && !jsonNode.isEmpty() && !jsonNode.hasNonNull("error") ) {
				OktaToken token = new OktaToken();
				token.setExpiresInSec(Integer.parseInt(jsonNode.get("expires_in").toString()) );
				return token;
} catch (URISyntaxException e) {
		} catch (IOException e) {
		} catch (InterruptedException e) {
		return null;
1 Like

Thanks for the update @vidhanchandrauk :grinning:

1 Like

:pray::grin: . But is it the correct way to do it?

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.