0

I read all my likes using FQL with Spring Social Facebook

here is the method:

public List<LikeObject>  startF(String token){
    Facebook facebook = new FacebookTemplate(token);
    FacebookProfile profile = facebook.userOperations().getUserProfile();
    //System.out.println(facebook.isAuthorized());
    System.out.println("Authenticcated user: "+profile.getFirstName()+" "+profile.getLastName());
    PagedList<String> friendListIds =  facebook.friendOperations().getFriendIds();

    List<LikeObject> resultsLikes = facebook.fqlOperations().query("SELECT object_id, object_id_cursor,object_type, post_id, post_id_cursor, user_id "+
            "FROM like "+
            "WHERE user_id =me() ",  new FqlResultMapper<LikeObject>(){
        public LikeObject mapObject(FqlResult result) {
            LikeObject like = new LikeObject();
            like.object_id = result.getString("object_id");
            like.object_id_cursor =  result.getString("object_id_cursor");
            like.object_type = result.getString("object_type");
            like.post_id = result.getString("post_id"); 
            like.post_id_cursor = result.getString("post_id_cursor");
            like.user_id = result.getString("user_id");
            return like;
        }
    });
    return resultsLikes;
}       

Here results:

LikeObject [object_id=578416.., object_id_cursor=null, object_type=status, post_id=, post_id_cursor=null, user_id=10217..]

Then I would like to parse like.object_id and convert it to java object. But I've no idea how to do it using spring social facebook.

I've tried facebook.fetchObject(like.object_id, PostType.STATUS) . But it seems to be a wrong way.

Is there any possible way to parse an "object_id" in spring social without parsing raw JSON response from GET query?

I've tried this:

LinkPost post = facebook.fetchObject(obj_id, LinkPost.class);

I believe that obj_id has type link, I've checked it

and it cause following exception:

Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'postType' that is to contain type id  (for class org.springframework.social.facebook.api.LinkPost)
 at [Source: java.io.ByteArrayInputStream@6ca79a6a; line: 1, column: 11114]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'postType' that is to contain type id  (for class org.springframework.social.facebook.api.LinkPost)
 at [Source: java.io.ByteArrayInputStream@6ca79a6a; line: 1, column: 11114]
    at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:171)
    at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:163)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:94)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:460)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:228)
    at org.springframework.social.facebook.api.impl.FacebookTemplate.fetchObject(FacebookTemplate.java:202)
    at com.repost.facebook.FbConnection.getLikedPosts(FbConnection.java:58)
    at com.repost.facebook.MainClass.main(MainClass.java:15)
Ivan T
  • 1,046
  • 1
  • 10
  • 22
  • 1
    What do you mean "parse an 'object_id'?" Are you asking if there's a way to determine what type of object is behind that id? – Craig Walls Jun 03 '13 at 14:22
  • Hi Craig! I know the type of the object. In my example Facebook returns me it as object_type=status. I would like to parse it like object of StatusPost class by using the reference link given by object_id – Ivan T Jun 03 '13 at 14:38

1 Answers1

2

I'm still unclear what you mean by "parse". But let me attempt to answer this anyway...

I think you simply want to be able to fetch the object as a Post (or LinkPost) object...is that right? If so, then there's some special magic that I unfortunately had to bake into the API to be able to both grab the post type and do polymorphic deserialization into a specific type of Post (e.g., LinkPost, StatusPost, etc).

You can see the setup taking place in https://github.com/SpringSource/spring-social-facebook/blob/master/spring-social-facebook/src/main/java/org/springframework/social/facebook/api/impl/FeedTemplate.java. In the deserializePost() method, you see that I read the "type" property and copy it into a new "postType" field. One of those fields is used to populate the Post's type property and the other is used to determine which specific subclass of Post should be created. It's a hack to work around a problem I had with Jackson 1.9.x...I think that Jackson 2 will make that hack unnecessary, but I've not tried it yet.

Therefore, I see only one practical way of doing this: Don't do all the work yourself. Instead use facebook.feedOperations().getPost(objectId). It knows how to do the magic under the covers to give you a Post object. From there, if you know the specific kind of Post, you can cast it as (and if) needed to LinkPost, StatusPost, etc.

The only other option I see is to go even lower level and make the request through facebook.restOperations() and to handle the binding for yourself. That's obviously a lot more work on your part.

Craig Walls
  • 2,080
  • 1
  • 12
  • 13
  • Thank you! That is exactly my question. Sorry for being unclear. – Ivan T Jun 06 '13 at 17:06
  • Craig, FYI I'm seeing something that appears to be similar (a parsing problem on the FB response). My code is attempting to post a reply to a comment: CommentOperations co = template.commentOperations(); String replyId = co.addComment( commentId, commentResponse ); The result of this is a HttpMessageNotReadableException from the MappingJackson2HttpMessageConverter class. Per your suggestion to use the facebook.restOperations() mechanism, I can work around this error but I wanted to report what I'm seeing. – Alex Oct 28 '13 at 14:07