2012-06-19 50 views
0

我嘗試配置Facebook集成。但我沒有正確地接受accesToken。我收到null。記錄器顯示變量AccesToken和UserID,它爲空。通過Spring-Social進行facebook集成

所以在這裏我的配置。

<?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

試試這個。它應該適合你:

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

+0

爲什麼我的示例不起作用? –

+0

我在這段代碼中收到了什麼......配置 什麼是userPrinc.name?請幫助我,請手動不清楚.. –

+0

什麼是authorizationCode? –