我正在尝试使用由oauth2保护的REST API。 我无法为其生成令牌。 我可以通过邮递员来做同样的事情,但是不能从Spring boot上做。
请找到以下Oauth2Client代码:
@Bean 公共OAuth2RestOperations restClientOnlyTemplate(){
OAuth2RestTemplate template = new OAuth2RestTemplate(authorizationCodeRestTemplate(),
new DefaultOAuth2ClientContext(new DefaultAccessTokenRequest()));
AuthorizationCodeAccessTokenProvider accessTokenProvider = new AuthorizationCodeAccessTokenProvider();
accessTokenProvider.setTokenRequestEnhancer(new AcceptJsonRequestEnhancer());
template.setAccessTokenProvider(accessTokenProvider);
return template;
}
@Bean
public OAuth2ProtectedResourceDetails authorizationCodeRestTemplate() {
AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
details.setId("1");
details.setClientId("81mol1mn9nwh00");
if (client_secret != null && !client_secret.isEmpty())
details.setClientSecret("dlY9rY4WRQ5YmspC");
details.setAccessTokenUri(access_token_uri);
details.setPreEstablishedRedirectUri("http://localhost:8080/linkedin");
details.setUserAuthorizationUri(authorization_uri);
details.setScope(Arrays.asList(scopes));
details.setGrantType("Authorization Code");
details.setClientAuthenticationScheme(AuthenticationScheme.form);
return details;
}
static class AcceptJsonRequestEnhancer implements RequestEnhancer {
@Override
public void enhance(AccessTokenRequest request,
OAuth2ProtectedResourceDetails resource,
MultiValueMap<String, String> form, HttpHeaders headers) {
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
}
}
我遇到以下错误:
org.springframework.security.oauth2.client.resource.UserRedirectRequiredException:需要重定向才能获得用户批准
我尝试阅读以下链接: Springframework "A redirect is required to get the users approval" https://gitter.im/spring-projects/spring-security-oauth/archives/2017/02/01
但是仍然没有运气:(