Facebook4j获得页面赞

时间:2014-09-18 13:21:52

标签: java facebook facebook-java-api

我正在使用facebook4j-core-2.1.0并试图让页面更喜欢。我不知道自己做错了什么,我就是新手。

    public static void main(String[] args) throws FacebookException
    {
        // Generate facebook instance.
        Facebook facebook = new FacebookFactory().getInstance();

        // access token ...

        AccessToken at = new AccessToken(accessTokenString);        

        // Set access token.
        facebook.setOAuthAccessToken(at);

        String facebookUserName = "Google";

        Page pgId = facebook.getPage(facebookUserName);
        Page pgL = facebook.getLikedPage(pgId.getId());
        System.out.println("Page Likes :" + pgL);
    }

http://www.facebook.com/Google表示它有 18,281,664个喜欢,但是当我运行上面的代码时,我得到了空值。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

我认为你应该尝试这样的事情:

String userName = "google";     
Page pgId = facebook.getPage(userName);
System.out.println("Page Likes :" + pgId.getLikes());

希望它有所帮助。

答案 1 :(得分:-1)

page.getLikes()方法返回null给我,所以我选择了这个:

RawAPIResponse facebookJson = facebook.callGetAPI("google?fields=engagement");
JSONObject jsonObject = facebookJson.asJSONObject();
String likesCount = ((JSONObject)jsonObject.get("engagement")).getString("count");
System.out.println("likes :" + likesCount);

使用Reading和api v2.6了解如何在此处指定api版本http://facebook4j.org/en/faq.html#apiversion

Reading pageReading = new Reading();
pageReading.fields("name","id","fan_count");
Page page = facebook.getPage("google", pageReading);
System.out.println("likes :" + page.getFanCount());