get the "instance_url" in the oauth2 salesforce response and use it in the webclient baseurl

132 Views Asked by At

I use a Bean webclient for my connection with salesforce

I created a configuration class with these 2 methods.

@Configuration
public class salesForceConfig {

    @Bean
    public OAuth2AuthorizedClientManager authorizedClientManager(
            ClientRegistrationRepository clientRegistrationRepository,
            OAuth2AuthorizedClientService authorizedClientService
    ) {
        OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder().password().build();
        AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientManager = new AuthorizedClientServiceOAuth2AuthorizedClientManager (clientRegistrationRepository, authorizedClientService );
        authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
        authorizedClientManager.setContextAttributesMapper(oAuth2AuthorizeRequest -> {
            if (SALESFORCE.equals(oAuth2AuthorizeRequest.getClientRegistrationId())) {
                return Map.of(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, SALESFORCE_USERNAME,OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, SALESFORCE_PASSORD
            );
            }
            return null;
        });
        return authorizedClientManager;
    }

    @Bean
    public WebClient salesforceWebClient(OAuth2AuthorizedClientManager authorizedClientManager) {
        ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client = new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);

        oauth2Client.setDefaultClientRegistrationId(SALESFORCE);

        return WebClient.builder()
                .baseUrl(SALESFORCE_BASE_PATH)
                .apply(oauth2Client.oauth2Configuration())
                .build();
    }
    }
`

during the authentication, I have a message with the token and other information like instance_url.

`{
    "access_token": "xxxxx",
    "instance_url": "https://xxxx.salesforce.com",
    "id": "https://login.salesforce.com/id/xxxxxx",
    "token_type": "Bearer",
    "issued_at": "xxxx",
    "signature": "xxxx"
}

I want to change the webclient baseurl address based on instance_url.

does anyone have any idea how to do this.

0

There are 0 best solutions below