创建OAuth2RestTemplate时,'error =“ access_denied”,error_description =“请求访问令牌时出错。”

时间:2019-08-21 14:35:49

标签: java oauth2resttemplate

当尝试使用OAuth2RestTemplate访问oAuth2令牌时,我需要有关错误的帮助

下面提供了我的Java代码。

我正在使用Spring 4.3.4版和Spring Security oAuth 2.3.4版

谢谢!

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());

当我创建一个JUnit测试类并运行它时,我得到以下错误跟踪。 oAuth2 REST API使用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)

1 个答案:

答案 0 :(得分:0)

您还记得添加内容类型吗? (当然,对于你的模拟响应方)

.withHeader(header("Content-Type", "application/json;charset=UTF-8"))