1

尝试使用 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)
4

1 回答 1

0

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

.withHeader(header("Content-Type", "application/json;charset=UTF-8"))
于 2021-05-24T09:20:15.303 回答