1

I need help with the error when trying to access the oAuth2 token using OAuth2RestTemplate

My java code is provided below.

I am using Spring version 4.3.4 and Spring Security oAuth version 2.3.4

Thank you!

ClientCredentialsResourceDetails returnValue = new   ClientCredentialsResourceDetails();
returnValue.setAccessTokenUri(xxx);
returnValue.setClientId(yyy);
returnValue.setGrantType("client_credentials");
returnValue.setClientSecret(zzz);

AccessTokenRequest atr = new DefaultAccessTokenRequest();
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(returnValue , new DefaultOAuth2ClientContext(atr));
restTemplate.setRequestFactory(getClientHttpRequestFactory());

logger.debug(restTemplate.getAccessToken());

When I create a JUnit test class and run this, I get the following error trace. The oAuth2 REST API works using SOAP UI.

    error="access_denied", error_description="Error requesting access token."
    at org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport.retrieveToken(OAuth2AccessTokenSupport.java:145)
    at org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider.obtainAccessToken(ClientCredentialsAccessTokenProvider.java:44)
    at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:148)
    at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:121)
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:221)
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:173)
Jazib
  • 1,343
  • 13
  • 27

1 Answers1

0

Did you remember to add the content type? (Of course, for your mock response side)

.withHeader(header("Content-Type", "application/json;charset=UTF-8"))
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Song
  • 1