春天社会如何获得联系

时间:2012-06-22 10:17:49

标签: java facebook spring-social

几天前,我使用spring的教程编写了facebook集成。这是我的代码

FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory("appId", "appSecret");
    OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();
    OAuth2Parameters params = new OAuth2Parameters("http://localhost:8080/shop/facebook");
    String authorizeUrl = oauthOperations.buildAuthorizeUrl(GrantType.IMPLICIT_GRANT, params);

    try {
        response.sendRedirect(authorizeUrl);
        LOG.error("ALL WORKING FINE>>>");
    } catch (IOException e) {
        LOG.error("Errorrrrr" + e);
    }
    return null;

然后我应该获得access_token,但它在URL的哈希部分(因此我应该使用JS获取access_token)来找我 - 错误的方式。之后我安装了另一个教程Spring-Social-quickstart,我无法理解它如何检索access_token?我应该写什么来获得服务器端的access_token。它应该是请求的一些拦截器,还是存在其他方式?

1 个答案:

答案 0 :(得分:1)

问题在于我期望access_token - 但这是客户端授权。所以我应该期望授权代码进行服务器端身份验证。

但是现在我在accessGrant行上遇到异常的问题。它是另一个控制器。我得到authCode但它显示异常400:Badrequest。

@RequestMapping(method = RequestMethod.GET) 
public String getAuthorisation(NativeWebRequest webRequest, HttpServletResponse response, HttpServletRequest request) {
    FacebookConnectionFactory connectionFactory = 
            new FacebookConnectionFactory("clientId", "clientSecret");
    OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();

String authCode = request.getParameter("code");     
    LOG.error("Facebook  controller works... 1");
    LOG.error(authCode);

    AccessGrant accessGrant = oauthOperations.exchangeForAccess(authCode, "http://localhost:8080/shop/pages/social.jsp", null);
    LOG.error("Facebook  controller works... 2");
    Connection<Facebook> connection = connectionFactory.createConnection(accessGrant);
    LOG.error("Facebook  controller works... 3");
    return null;

}