TesUser476797
TesUser476797

Reputation: 23

Spring Boot APIGEE OAuth authorization with "BearerToken"

I'm trying to access a resource from my Spring Application using OAuth2.0 authorization I'm able to connect and get the access token as follows,

{ refresh_token_expires_in=0, organization_name=abc, token_type=BearerToken, issued_at=1716401090878, client_id=xxxxxxxxx, access_token=xxxxxxxxxxxxxxxxxx, application_name=zzzzzzzzzzzzzzz, scope=OAuth, expires_in=3599, refresh_count=0, status=approved }

Below is the code snippet I tried for creating the webclient

@Bean
    public WebClient.Builder loadBalancedWebClientBuilder(ReactiveClientRegistrationRepository clientRegistrations,
                                                          ObjectMapper objectMapper,
                                                          ServerOAuth2AuthorizedClientRepository clientRepository) {

        ServerOAuth2AuthorizedClientExchangeFilterFunction oauth2ClientFilter = new ServerOAuth2AuthorizedClientExchangeFilterFunction(
                clientRegistrations,
                clientRepository);
        oauth2ClientFilter.setDefaultClientRegistrationId("apigee");
        WebClient.Builder builder = WebClient.builder();
        builder.defaultHeader("Content-Type", MediaType.APPLICATION_JSON.toString());
        builder.defaultHeader("Accept", MediaType.APPLICATION_JSON.toString());
        builder.filter(oauth2ClientFilter);
        return builder;
    }

and all the required Client Id, Secret and Access Token URL have been passed on application.yml file

Now, in my Spring Controller class I use this webclient and access the resource that needed,

return loadBalancedWebClientBuilder.baseUrl(clientUrl).
                defaultHeaders(
                        httpHeaders -> {
                            httpHeaders.set("Content-Type", "application/json");
                          }).build().post().syncBody(tmp).retrieve().bodyToMono(String.class);

Upto this I'm able to connect to the APIGEE and get the access token but while parsing I'm getting exception as,

ClientAuthorizationException: [invalid_token_response] An error occurred parsing the Access Token response: Token type must be Bearer
at com.nimbusds.oauth2.sdk.token.BearerAccessToken.parse(BearerAccessToken.java:192) ~[oauth2-oidc-sdk-8.36.1.jar:8.36.1]
    at com.nimbusds.oauth2.sdk.token.AccessToken.parse(AccessToken.java:273) ~[oauth2-oidc-sdk-8.36.1.jar:8.36.1]
    at com.nimbusds.oauth2.sdk.token.Tokens.parse(Tokens.java:198) ~[oauth2-oidc-sdk-8.36.1.jar:8.36.1]
    at com.nimbusds.oauth2.sdk.AccessTokenResponse.parse(AccessTokenResponse.java:198) ~[oauth2-oidc-sdk-8.36.1.jar:8.36.1]
    at com.nimbusds.oauth2.sdk.TokenResponse.parse(TokenResponse.java:75) ~[oauth2-oidc-sdk-8.36.1.jar:8.36.1]
    at org.springframework.security.oauth2.core.web.reactive.function.OAuth2AccessTokenResponseBodyExtractor.parse(OAuth2AccessTokenResponseBodyExtractor.java:68)

In the starting if you see the token_type comes as BearerToken but here in Spring it's expecting Bearer, how to resolve this or if there's anyother way to implement please let me know. Thanks in Advance.

Upvotes: 0

Views: 204

Answers (0)

Related Questions