简体   繁体   中英

Facebook4j get page likes

I am using facebook4j-core-2.1.0 and trying to get the page likes. I don't know what I am doing wrong, I am kinda newbie at this.

    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 shows that it have 18,281,664 likes , but when I ran the above code I got null value.

What am I missing here?

I think you should try something like this:

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

Hope it helps.

The page.getLikes() method return null to me so I opted for this:

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

Using Reading and api v2.6 see how to specify api version here 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM