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 “java.net.ConnectException: 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
    grant_type=authorization_code
    code={auth code got from }
    client_id={my-client-id}
    client_secret={my-secret}
    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

@Service
public class OktaTokenServiceImpl implements OktaTokenService{

@Override
public OktaToken getToken(String authToken) {
	WebClient webClient = WebClient
			.builder()
			.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
			.baseUrl(OktaTokenService.baseUrl)
			.build();
	URI uri;
	try {
		
		uri = new URI("/v1/token/" +"?client_id="+"my-client-id"+"&"
				+"client_secret="+"my-secret"+"&"
				+"grant_type="+"authorization_code"+"&"
				+"redirect_uri="+"http://localhost:8181/api/v1/helloToken"+"&"
				+"code="+authToken);
		System.out.println("complete URL" +uri.toString());
		Mono<OktaToken> oktaToken = webClient
				.post()
				.uri(uri)
				.retrieve()
				.bodyToMono(OktaToken.class);
		if (oktaToken.blockOptional().isPresent()) {
			return oktaToken.blockOptional().get();
		} else return null;
	
	} catch (URISyntaxException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}

}

My URL which I hit from POSTMAN

http://localhost:8181/api/v1/getToken/{my-auth-code}

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: /127.0.0.1:80; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /127.0.0.1:80] with root cause

java.net.ConnectException: 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) -

@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;
	}
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.