使用Facebook4j api从页面获取帖子

YYZ*_*YYZ 6 java facebook facebook-graph-api facebook4j

我想知道是否有办法使用Facebook4J API从Facebook页面获取所有(甚至最近的)帖子?

我知道可以从用户的墙上或Feed中获取所有帖子,但我在API或文档中找不到任何显示如何从页面获取帖子的内容.

看看http://facebook4j.org/en/api-support.html#page,看来实际上有一组与页面相关的方法,但点击其中任何一个只是刷新页面,让我觉得也许他们有计划但尚未实施?

我知道可以使用图形API从页面获取帖子,但如果可能的话,我真的更喜欢坚持使用Facebook4j.

任何投入将不胜感激!

Nik*_*enk 12

以下是您的问题的最小示例:请注意,您可以从https://developers.facebook.com/tools/explorer获取访问令牌和页面ID. 在您的代码中使用以下ID:

import facebook4j.Comment;
import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.PagableList;
import facebook4j.Post;
import facebook4j.Reading;
import facebook4j.ResponseList;
import facebook4j.auth.AccessToken;

public class PostsFromPageExtractor {

/**
 * A simple Facebook4J client which
 * illustrates how to access group feeds / posts / comments.
 * 
 * @param args
 * @throws FacebookException 
 */
public static void main(String[] args) throws FacebookException {

    // Generate facebook instance.
    Facebook facebook = new FacebookFactory().getInstance();
    // Use default values for oauth app id.
    facebook.setOAuthAppId("", "");
    // Get an access token from: 
    // https://developers.facebook.com/tools/explorer
    // Copy and paste it below.
    String accessTokenString = "PASTE_YOUR_ACCESS_TOKEN_HERE";
    AccessToken at = new AccessToken(accessTokenString);
    // Set access token.
    facebook.setOAuthAccessToken(at);

    // We're done.
    // Access group feeds.
    // You can get the group ID from:
    // https://developers.facebook.com/tools/explorer

    // Set limit to 25 feeds.
    ResponseList<Post> feeds = facebook.getFeed("187446750783",
            new Reading().limit(25));

        // For all 25 feeds...
        for (int i = 0; i < feeds.size(); i++) {
            // Get post.
            Post post = feeds.get(i);
            // Get (string) message.
            String message = post.getMessage();
                            // Print out the message.
            System.out.println(message);

            // Get more stuff...
            PagableList<Comment> comments = post.getComments();
            String date = post.getCreatedTime().toString();
            String name = post.getFrom().getName();
            String id = post.getId();
        }           
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 上面的代码不起作用,它给出了facebookexception:"graph.facebook.com FacebookException {statusCode = -1,errorType ='null',errorMessage ='null',errorCode = -1,errorSubcode = -1,version = 2.4.5}" (2认同)

小智 10

Facebook4J从2.0版开始支持Page API.
您可以通过Facebook#getFeed(PAGE_ID)从Facebook页面获取帖子.例:

    ResponseList<Post> feed = facebook.getFeed("eclipse.org");
Run Code Online (Sandbox Code Playgroud)

javadoc:http://facebook4j.org/javadoc/facebook4j/api/PostMethods.html#getFeed()