Java 使用Spring Social阅读Facebook帖子相关对象
我用FQL和 方法如下:Java 使用Spring Social阅读Facebook帖子相关对象,java,facebook,api,spring-social,Java,Facebook,Api,Spring Social,我用FQL和 方法如下: public List<LikeObject> startF(String token){ Facebook facebook = new FacebookTemplate(token); FacebookProfile profile = facebook.userOperations().getUserProfile(); //System.out.println(facebook.isAuthorized()); Sy
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;
}
我相信obj_id有类型链接,我已经检查过了
并导致以下例外情况:
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)
我还不清楚你说的“解析”是什么意思。但无论如何,让我尝试回答这个问题 我认为您只是希望能够将对象作为Post(或LinkPost)对象获取……是这样吗?如果是这样的话,那么不幸的是,我不得不在API中加入一些特殊的魔法,以便既能获取post类型,又能将多态反序列化为特定类型的post(例如LinkPost、StatusPost等) 您可以在中看到正在进行的设置。在反序列化post()方法中,您可以看到我读取了“type”属性并将其复制到一个新的“postType”字段中。其中一个字段用于填充Post的type属性,另一个字段用于确定应该创建Post的哪个特定子类。这是一个解决我在Jackson 1.9.x上遇到的问题的黑客。我认为Jackson 2会让这个黑客变得不必要,但我还没有尝试过 因此,我认为只有一种切实可行的方法:不要自己做所有的工作。而是使用facebook.feedOperations().getPost(objectId)。它知道如何在封面下施展魔法,给你一个贴子。从那里,如果您知道特定类型的帖子,您可以将其转换为(如果需要)链接帖子、状态帖子等
我看到的唯一的另一个选择是更低级,通过facebook.restOperations()发出请求,并自己处理绑定。这显然需要你做更多的工作。你说的“解析对象id”是什么意思?“你是在问有没有办法确定该id后面是什么类型的对象?嗨,克雷格!我知道物体的类型。在我的示例中,Facebook将其作为object_type=status返回给我。我想使用object_idu提供的引用链接来解析它,就像StatusPost类的object一样谢谢!这正是我的问题。很抱歉不清楚。Craig,仅供参考,我看到了一些类似的情况(FB响应中的解析问题)。我的代码试图发布对注释的回复:CommentOperations co=template.CommentOperations();字符串replyId=co.addComment(commentId,commentResponse);其结果是MappingJackson2HttpMessageConverter类中的HttpMessageOnTreadableException。根据您关于使用facebook.restOperations()机制的建议,我可以解决这个错误,但我想报告我看到的情况。
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)