我正在尝试使用Oauth2RestTemplate
Id
安全的rest API
尝试获取访问令牌时,客户端AppConfig.java
未设置
请帮助我获取示例代码或链接到工作代码
@Bean
@Primary
public OAuth2ProtectedResourceDetails getOAuth2ProtectedResourceDetails()
{
ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
details.setAccessTokenUri("External Url , Ex: https://XXX/oauth2/v0/token");
details.setClientId("*******");
details.setClientSecret("******");
details.setGrantType("client_credentials");
return details;
}
@Bean
@Primary
public OAuth2RestTemplate createOAuth2RestTemplate() {
DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(getOAuth2ProtectedResourceDetails(), new DefaultOAuth2ClientContext(accessToken));
return restTemplate;
}
ClientInvoker.java
在String URL= "Get the Accounts Url";
oAuth2RestTemplate.getForEntity("URL", Object.class)
{{1}}
异常
引起:org.springframework.beans.BeanInstantiationException: 无法实例化 [org.springframework.security.oauth2.client.OAuth2RestTemplate]: 工厂方法'createOAuth2RestTemplate'抛出异常;嵌套 异常是error =“access_denied”,error_description =“访问令牌 否认。“在 org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) 在 org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ... 25更多引起:error =“access_denied”, error_description =“拒绝访问令牌。”在 org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport.retrieveToken(OAuth2AccessTokenSupport.java:142)
答案 0 :(得分:0)
您的资源服务器似乎有问题。确认以下
此致