0

I try to configure facebook integration. But i did't recive accesToken properly. I recieved null. Logger show me variables AccesToken and UserID and it null.

so here my config.

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

And this is my callback controller with FacebookProvider and jsp page.

@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";
    }

}

Provider

@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 page

<%@ 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>
Igor Masternoy
  • 436
  • 1
  • 11
  • 36

1 Answers1

0

Try this. It should work for you:

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);

For more information try this.

Abhendra Singh
  • 1,959
  • 4
  • 26
  • 46
  • And what i recieve in this code... And what about configs What is userPrinc.name? Help me please manual is not clear for me.. – Igor Masternoy Jun 19 '12 at 14:36
  • If you use GrantType.AUTHORIZATION_CODE then provider callback directly contains the access grant (no additional exchange is necessary). You can also use GrantType.IMPLICIT_GRANT, in this case you will receive accessToken with url. – Abhendra Singh Jun 20 '12 at 05:41
  • I received it. I reciev my url with tokken but how i should take the token from the url i have't found it in request params. – Igor Masternoy Jun 20 '12 at 15:48
  • You have get it from jQuery.. Try this code, it should work $(document).ready(function() { var pathname = window.location.pathname; }); – Abhendra Singh Jun 21 '12 at 11:22
  • You should use another way if you get it in java code then first get authorizationCode and then access token. I will suggest you the another way. – Abhendra Singh Jun 21 '12 at 11:42