我尝试配置facebook集成。但是我没有正确地回忆accessToken。我收到了null。记录器向我显示变量AccesToken和UserID,它为null。
所以这是我的配置。
<?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>
答案 0 :(得分: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);
有关详细信息,请尝试this。