0

我尝试配置 facebook 集成。但我没有正确接收 accessToken。我收到空值。记录器向我显示变量 AccesToken 和 UserID 并且它为空。

所以这里是我的配置。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="customArgumentResolver" ref="facebookWebArgResolver" />
    </bean>
    <bean id="facebookWebArgResolver"
        class="org.springframework.social.facebook.FacebookWebArgumentResolver">
        <constructor-arg name="apiKey" value="${facebook.appId}"/>
    </bean>
</beans>

这是我的带有 FacebookProvider 和 jsp 页面的回调控制器。

@Controller
public class SocialTutorial {
    private static final Logger LOG = Logger.getLogger(SocialTutorial.class);
    @Autowired
    FacebookProvider facebookProvider;

    @RequestMapping(value = "/connect/facebook", method = RequestMethod.POST)
    public String connectAccountToFacebook(@FacebookAccessToken String accessToken, @FacebookUserId String facebookUserId) {
        LOG.error("HERE facebook authnticate:" + accessToken + " And user id:" + facebookUserId);
        return "redirect:/pages/social.jsp";
    }

}

提供者

@Repository("facebookProvider")
public class FacebookProvider {
    @Value("${facebook.appId}")
    private String apiKey;// = "240362226072898";
    @Value("${facebook.appSecret}")
    private String appSecret;// = "     ";

    public String getApiKey() {
        return apiKey;
    }

    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }


    public FacebookTemplate createTemplate(String accesToken) {
        return new FacebookTemplate(accesToken);
    }
}

JSP 页面

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix='c' uri='http://java.sun.com/jstl/core_rt'%>
<%@ taglib prefix="sec"
    uri="http://www.springframework.org/security/tags"%>
<%@ taglib
    uri="http://www.springframework.org/spring-social/facebook/tags"
    prefix="facebook"%>

<%@ page contentType="text/html;charset=UTF-8" language="java"%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Update Status</title>


<script
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"
    type="text/javascript"></script>

</head>
<body>

    <a href="<c:url value='/j_spring_security_logout'/>">Logout</a>

    <h2>Services</h2>

    <!--  FACEBOOK -->

    <c:if test="${connectedToFacebook}">
    Connected to Facebook as <c:out
            value="${facebookProfile.firstName }" />
        <c:out value="${facebookProfile.lastName }" />
    </c:if>
    <c:if test="${!connectedToFacebook}">

        <%/* Connect to Facebook */ %>

        <form id="fb_signin" action="<c:url value="/connect/facebook" />"
            method="post">
            <div class="formInfo"></div>
            <div id="fb-root"></div>
            <p>
                <fb:login-button perms="email,publish_stream,offline_access"
                    onlogin="$('#fb_signin').submit();" v="2" length="long">Connect to Facebook</fb:login-button>
            </p>
        </form>

        <facebook:init />
    </c:if>

    <br />

</body>
</html>
4

1 回答 1

0

尝试这个。它应该适合你:

FacebookConnectionFactory connectionFactory = 
new FacebookConnectionFactory("clientId", "clientSecret");
OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();
OAuth2Parameters params = new OAuth2Parameters();
params.setRedirectUri("https://my-callback-url");
String authorizeUrl = oauthOperations.buildAuthorizeUrl(GrantType.AUTHORIZATION_CODE, params);
response.sendRedirect(authorizeUrl);

// upon receiving the callback from the provider:
AccessGrant accessGrant = oauthOperations.exchangeForAccess(authorizationCode,      "https://my-callback-url", null);
Connection<Facebook> connection = connectionFactory.createConnection(accessGrant);

有关更多信息,请尝试

于 2012-06-19T12:22:39.470 回答