我希望开发一个 Spring OAuth2RestTemplate 代码,并使用 2 legged Oauth 2.0 和 Apache OauthClient 从访问令牌中获取参考。
给出了两个建议,第一个建议使用 Apache Oltu 对我来说非常好。现在,我正在寻找使用 Spring Oauth2 RestTemplate 开发的第二个选项。
我得到的错误: -
WARN : org.springframework.web.client.RestTemplate - POST request for "https://graph.facebook.com/oauth/access_token" resulted in 400 (Bad Request); invoking error handler
Exception in thread "main" 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:142)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:118)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:221)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:173)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.createRequest(OAuth2RestTemplate.java:105)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:564)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:128)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:329)
at com.apache.oltu.RestFacebookController.authenticate(RestFacebookController.java:46)
at com.apache.oltu.RestFacebookController.main(RestFacebookController.java:52)
Caused by: org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport$AccessTokenErrorHandler.handleError(OAuth2AccessTokenSupport.java:244)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:615)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:573)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:537)
at org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport.retrieveToken(OAuth2AccessTokenSupport.java:137)
... 12 more
我尝试调试该应用程序数周,最后决定将其发布在 stackoverflow.com 上。这是我开发的代码
import java.util.Arrays;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/restfacebook")
public class RestFacebookController {
private static final Logger logger = LoggerFactory.getLogger(RestFacebookController.class);
private String CLIENT_SECRET = "33b17e044ee6a4fa383f46ec6e28ea1d";
private String CLIENT_ID = "233668646673605";
@RequestMapping(value = "/auth", method = RequestMethod.GET)
public void authenticate() {
logger.debug("In a Authenticate() method");
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
resourceDetails.setClientSecret(CLIENT_SECRET);
resourceDetails.setClientId(CLIENT_ID);
resourceDetails.setAccessTokenUri("https://graph.facebook.com/oauth/access_token");
resourceDetails.setScope(Arrays.asList("email,offline_access,user_about_me,user_birthday,read_friendlists"));
resourceDetails.setTokenName("code");
JSONObject request = new JSONObject();
request.put("resourceDetails", resourceDetails);
OAuth2RestTemplate oAuthRestTemplate = new OAuth2RestTemplate(resourceDetails);
HttpHeaders headers = new HttpHeaders();
headers.setContentType( MediaType.APPLICATION_JSON );
// Sample POST Method
HttpEntity<String> reqEntity = new HttpEntity<String>(resourceDetails.toString(), headers);
String postUri = "https://www.facebook.com/dialog/oauth";
String postResult = oAuthRestTemplate.postForObject(postUri, reqEntity, String.class);
System.out.println(postResult);
}
}
}
如果我按照下面 Dave 的建议代码进行操作,Dave 的代码也似乎可以正常工作,但为什么不这样呢?我没有看到任何概念上的差异。
private String CLIENT_SECRET = "33b17e044ee6a4fa383f46ec6e28ea1d";
private String CLIENT_ID = "233668646673605";
@RequestMapping(value = "/auth", method = RequestMethod.GET)
public void authenticate() {
logger.debug("In a Authenticate() method");
AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
details.setId("facebook");
details.setClientId(CLIENT_ID);
details.setClientSecret(CLIENT_SECRET);
details.setAccessTokenUri("https://graph.facebook.com/oauth/access_token");
details.setUserAuthorizationUri("https://www.facebook.com/dialog/oauth");
details.setTokenName("oauth_token");
details.setAuthenticationScheme(AuthenticationScheme.query);
details.setClientAuthenticationScheme(AuthenticationScheme.form);
OAuth2RestTemplate oAuthRestTemplate = new OAuth2RestTemplate(details);
HttpHeaders headers = new HttpHeaders();
headers.setContentType( MediaType.APPLICATION_JSON );
// Sample POST Method
HttpEntity<String> reqEntity = new HttpEntity<String>(details.toString(), headers);
String postUri = "https://www.facebook.com/dialog/oauth";
String postResult = oAuthRestTemplate.postForObject(postUri, reqEntity, String.class);
System.out.println(postResult);
}
然后我得到了另一个错误,这似乎是意料之中的,现在我们如何才能克服这个错误?
Exception in thread "main" org.springframework.security.oauth2.client.resource.UserRedirectRequiredException: A redirect is required to get the users approval
at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.getRedirectForAuthorization(AuthorizationCodeAccessTokenProvider.java:347)
at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.obtainAccessToken(AuthorizationCodeAccessTokenProvider.java:194)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:142)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:118)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:221)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:173)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.createRequest(OAuth2RestTemplate.java:105)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:564)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:128)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:329)
at com.apache.oltu.RestFacebookController.authenticate(RestFacebookController.java:48)
at com.apache.oltu.RestFacebookController.main(RestFacebookController.java:54)
pom.xml:
<properties>
<org.springframework-version>4.1.5.RELEASE</org.springframework-version>
<org.aspectj-version>1.8.5</org.aspectj-version>
</properties>
<dependencies>
<!-- Spring Context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Web MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Module for providing OAuth2 support to Spring Security -->
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.0.7.RELEASE</version>
</dependency>
<!-- Apache Log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>