Results 1 to 9 of 9

Thread: Accessing Facebook Social Failed, help please

  1. #1
    Join Date
    Nov 2010
    Posts
    30

    Exclamation Accessing Facebook Social Failed, help please

    I was using this tutorial to learn how to access social network:
    http://www.emforge.net/web/akakunin/...-spring-social

    Both Twitter and LinkedIn work link charm, thanks, but when I tried facebook, I was stuck and don't know how to solve.

    In Status.jsp, It is said to use this code:
    <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>

    But Where is <fb:login-button> tag? I can not find it. So I used this instead obtained from facebook:
    <script src="http://connect.facebook.net/en_US/all.js#appId=b227323312f4fcb29337f87891811a0e&amp; xfbml=1"></script>
    <fb:login-button perms="email,publish_stream,offline_access" onlogin="$('#fb_signin').submit();" v="2" length="long"></fb:login-button>

    I don't know if this is right or not.

    After I click the fb button, a popup come up, asking for authorization. AFter Allow, the popup closed, and the original form is submit.

    I the controller,
    @RequestMapping(value="/connect/facebook", method=RequestMethod.POST)
    public String connectAccountToFacebook(@FacebookAccessToken String accessToken,
    @FacebookUserId String facebookUserId) {
    System.out.println("come back from fb2 " + facebookUserId +" token is " + accessToken);
    if (facebookUserId != null && accessToken != null) {
    // store facebook information
    String userName = getCurrentUser().getName();
    userService.updateFacebookAuthentication(userName, accessToken, facebookUserId);
    }
    return "redirect:/status";
    }

    I did not get any facebookuserid or accessToken.

    In the applicationContext.xml, I already added this one;

    <!-- Initialize Facebook Argument Resolver -->
    <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter">
    <property name="customArgumentResolver" ref="facebookWebArgResolver"/>
    </bean>

    <bean id="facebookWebArgResolver" class="org.springframework.social.facebook.Faceboo kWebArgumentResolver">
    <constructor-arg name="apiKey" value="${facebook.appId}"/>
    </bean>


    Can anyone tell me why the fbId and accessToken has not been retrieved?

    Thanks
    Last edited by xhe; Dec 13th, 2010 at 09:04 PM.

  2. #2
    Join Date
    Aug 2004
    Posts
    1,099

    Default

    Quote Originally Posted by xhe View Post
    But Where is <fb:login-button> tag? I can not find it.
    It's part of Facebook's XFBML tag library. XFBML is a JavaScript-parsed tag library, not a JSP tag library.

    So I used this instead obtained from facebook:
    <script src="http://connect.facebook.net/en_US/all.js#appId=b227323312f4fcb29337f87891811a0e&amp; xfbml=1"></script>
    That's one way of getting it, but I've not seen it done by passing in the appId as part of the URl before...not sure if that will work or not. (And it's hard to tell from Facebook's documentation, because they still have some old information in there.) Instead, I recommend using Spring Social's Facebook JSP tag library.

    Code:
    <%@ taglib uri="http://www.springframework.org/spring-social/facebook/tags" prefix="facebook" %>
    ...
    <facebook:init /> <!-- should be near bottom of page -->
    I recommend that you have a look at the Greenhouse example, specifically the facebookConnect.jsp page to see how we use the Facebook JSP tag library to setup the Facebook JS-parsed tags.

    Can anyone tell me why the fbId and accessToken has not been retrieved?
    On the surface, it looks like you've done everything else correctly. But I'd be curious to know whether or not the FacebookWebArgumentResolver is ever called. If it is, then it means that the argument resolver can't find what it needs in the cookie that's being written (or the cookie isn't being written) upon authorization...probably because the Facebook JS library isn't being initialized correctly.

    Another thing to look for: After authorization (after you click "Allow"), do you have any cookies for your domain whose name starts with "fb_" and is followed by your application's API key? That cookie is where FacebookWebArgumentResolver goes to find the user ID and access token. If it's not there, then FacebookWebArgumentResolver isn't going to be able to resolve them--and it's a sign that the Facebook JS library isn't being initialized correctly.
    Craig Walls
    Spring Social Project Lead

  3. #3
    Join Date
    Dec 2010
    Posts
    1

    Default

    Same problem here, following the same tutorial. I extended WebArgumentResolver and placed breakpoints in the overridden constructor and resolveArgument method. The constructor gets called with the correct api key, but the facebook callback goes straight to the controller method without calling WebArgumentResolver.resolveArgument. Therefore, both accessToken and userId are null. The fbs_<apikey> cookie is there. So the problem is with the AnnotationMethodHandlerAdapter not calling the WebArgumentResolver. No idea why, until I find out I will just check the cookie by hand in the controller.

  4. #4
    Join Date
    Aug 2004
    Posts
    1,099

    Default

    Hmmm...what I don't see here is where you've either used <mvc:annotation-driven> or have declared a DefaultAnnotationHandlerMapping bean along with the AnnotationMethodHandlerAdapter bean. Could that be what's missing?
    Craig Walls
    Spring Social Project Lead

  5. #5
    Join Date
    Aug 2004
    Posts
    1,099

    Default

    To be more precise, if you are using <mvc:annotation-driven>, you must also include a BeanPostProcessor bean that registers the argument resolver with the AnnotationMethodHandlerAdapter bean. Optionally, you could *not* use <mvc:annotation-driven> and instead manually register your own DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans, injecting the argument resolver into AnnotationMethodHandlerAdapter's customArgumentResolver property (or customArgumentResolvers if you have multiple argument resolvers to register).
    Craig Walls
    Spring Social Project Lead

  6. #6

    Default Same problem for me too

    Hi,
    I am also facing the same problem. After successful logon into FaceBook the popup is closing but the values for facebookUserId and facebookAccessToken are null s.

    I have this configuration in applicationContext.xml:
    Code:
    <context:component-scan base-package="net.emforge.updatestatus">
    			<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
            </context:component-scan>
    ................
    ................
    ................
    <!-- Initialize Facebook Argument Resolver -->
    	   	<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>
    Please help on this.
    Thanks,
    K. Siva Prasad Reddy

  7. #7

    Default

    Any update on this issue?

  8. #8
    Join Date
    Aug 2004
    Location
    Melbourne, FL
    Posts
    2,794

    Default

    Here is an example of a BeanPostProcessor that registers custom WebArgumentResolvers, including the Facebook one:

    http://git.springsource.org/greenhou...Processor.java

    This is from the <a href="http://www.springsource.org/greenhouse">Greenhouse</a> reference application. I would recommend this approach as it allows you to continue using mvc:annotation-driven which sets up a lot of useful stuff for you.

    In Spring 3.1 the mvc:annotation-driven element will have a mvc:web-argument-resolvers sub-element that will make this a lot more obvious.

    Keith
    Keith Donald
    Core Spring Development Team

  9. #9

    Smile

    Quote Originally Posted by Keith Donald View Post
    Here is an example of a BeanPostProcessor that registers custom WebArgumentResolvers, including the Facebook one:

    http://git.springsource.org/greenhou...Processor.java

    This is from the <a href="http://www.springsource.org/greenhouse">Greenhouse</a> reference application. I would recommend this approach as it allows you to continue using mvc:annotation-driven which sets up a lot of useful stuff for you.

    In Spring 3.1 the mvc:annotation-driven element will have a mvc:web-argument-resolvers sub-element that will make this a lot more obvious.

    Keith

    Really nice posting.......
    Thanks for sharing this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •