Getting a management API token using client_credentials grant with Spring WebFlux WebClient

Hello all,

I am trying to implement the client_credentials grant to get a token in my spring boot resource server. I have tried to do the request through postman and it works. The problem I am facing is that i have no way to add the missing audience parameter to the token request.

I have the client filter configured like this.

@Bean
WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations,
                    ServerOAuth2AuthorizedClientRepository authorizedClients) {
    ServerOAuth2AuthorizedClientExchangeFilterFunction oauth2 = new ServerOAuth2AuthorizedClientExchangeFilterFunction(
            clientRegistrations, authorizedClients);
    oauth2.setDefaultClientRegistrationId("auth0");
    return WebClient.builder()
            .filter(oauth2)
            .build();
}

I am injecting the instance and trying to do a request to get the user by email

 return this.webClient.get()
            .uri(this.usersUrl + "/api/v2/users-by-email?email={email}", email)
            .attributes(auth0ClientCredentials())
            .retrieve()
            .bodyToMono(User.class);

Is there a way to skip the need to add the audience parameter or a way to add it to the webclient request in the filter function?

Not sure about Spring’s ServerOAuth2AuthorizedClientExchangeFilterFunction, but in case it helps (or if you have no other choice :slight_smile: ) you can use auth0-java’s requestToken method to get a token: GitHub - auth0/auth0-java: Java client library for the Auth0 platform

AuthAPI auth = new AuthAPI("{YOUR_DOMAIN}", "{YOUR_CLIENT_ID}", "{YOUR_CLIENT_SECRET}");

AuthRequest request = auth.requestToken("https://{YOUR_DOMAIN}/api/v2/");
try {
    TokenHolder holder = request.execute();
} catch (APIException exception) {
    // api error
} catch (Auth0Exception exception) {
    // request error
}