Getting the Facebook like/share count for a given

2019-01-07 02:24发布

I'm using the Facebook API to get the like/share count for given URLs. The strange thing is that it seems to be quite inconsistent in returning results. For example, this page returns results:

https://api.facebook.com/method/fql.query?query=select%20total_count,like_count,comment_count,share_count,click_count%20from%20link_stat%20where%20url='http://www.groupon.com/deals/seattlehelitourscom-by-classic-helicopter-corp'&format=json

Whereas, this one does not:

https://api.facebook.com/method/fql.query?query=select%20total_count,like_count,comment_count,share_count,click_count%20from%20link_stat%20where%20url='http://www.livingsocial.com/deals/278194-sunset-kayaking-hot-chowder'&format=json

The second page clearly has a share count on it, and when I inspect the HTML of the page, the URL which is being used to share is the one I've placed into the API request above. However, the API does not respond with any count information for either number of likes or shares.

Any clues on why the API might be responding for some URLs but not for others?

13条回答
▲ chillily
2楼-- · 2019-01-07 03:08

I don't think Facebook's Open Graph Object i.e. "og_object" provides anything more than comment_count & share_count for a URL. Try this; replace $YOUR_URL with the URL and $ACCESS_TOKEN with your access token in the below link https://graph.facebook.com/v2.5/$YOUR_URL?access_token=$ACCESS_TOKEN

For example:

https://graph.facebook.com/v2.5/http://espn.go.com/nfl/story/_/id/14424066/handing-holiday-gifts-all-32-nfl-teams-nfl?access_token=$ACCESS_TOKEN

{
  og_object: {
    id: "956517601094822",
    description: "Naughty or nice, every NFL team deserves something for Christmas. So in lieu of Santa Claus, Bill Barnwell is here to distribute some gifts.",
    title: "Barnwell: Handing out holiday gifts to all 32 teams",
    type: "article",
    updated_time: "2015-12-23T17:20:55+0000",
    url: "http://espn.go.com/nfl/story/_/id/14424066"
  },
  share: {
    comment_count: 0,
    share_count: 354
  },
  id: "http://espn.go.com/nfl/story/_/id/14424066/handing-holiday-gifts-all-32-nfl-teams-nfl"
}

Also, if you try to get likes, you would get the following error https://graph.facebook.com/http://rottentomatoes.com?fields=likes&summary=1&access_token=$ACCESS_TOKEN

{
  error: {
    message: "(#100) Tried accessing nonexisting field (likes) on node type (URL)",
    type: "OAuthException",
    code: 100,
    fbtrace_id: "H+KksDn+mCf"
  }
}
查看更多
做个烂人
3楼-- · 2019-01-07 03:11

You need the extended permission "read_stream", then you need to call the Facebook API endpoint, and add likes,shares to your fields.

This call

https://developers.facebook.com/tools/explorer?method=GET&path=me/feed?fields=likes,shares

will return a data array like this

{
   "data": [
    {
     "likes": {
        "data": [
                 {
                   "name": "name of user who liked status ",
                   "id": "id of user who liked status "
                 }
                ],
        "count": number of likes
     },
     "shares": {
      "count": number of shares 
     }, 
     "id": "post id",
     "created_time": "post creation time"
    }
   ]
}
查看更多
倾城 Initia
4楼-- · 2019-01-07 03:13

Your question is quite old and Facebook has depreciated FQL now but what you want can still be done using this utility: Facebook Analytics. However you will find that if you want details about who is liking or commenting it will take a long time to get. This is because Facebook only gives a very small chunk of data at a time and a lot of paging is required in order to get everything.

查看更多
甜甜的少女心
5楼-- · 2019-01-07 03:16

UPDATE: This solution is no longer valid. FQLs are deprecated since August 7th, 2016.


https://api.facebook.com/method/fql.query?query=select%20%20like_count%20from%20link_stat%20where%20url=%22http://www.techlila.com%22

Also http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.techlila.com will show you all the data like 'Share Count', 'Like Count' and 'Comment Count' and total of all these.

Change the URL (i.e. http://www.techlila.com) as per your need.

This is the correct URL, I'm getting right results.

EDIT (May 2017): as of v2.9 you can make a graph API call where ID is the URL and select the 'engagement' field, below is a link with the example from the graph explorer.

https://developers.facebook.com/tools/explorer/?method=GET&path=%3Fid%3Dhttp%3A%2F%2Fcomunidade.edp.pt%26fields%3Dengagement&version=v2.9

查看更多
Fickle 薄情
6楼-- · 2019-01-07 03:18

For latest 2.1 Graph API, an example to get likes for imdb.com will be

Using this to get the id https://developers.facebook.com/tools/explorer/?method=GET&path=%3Fid%3Dhttp%253A%252F%252Fwww.imdb.com%3Ffields%3Dlikes&version=v2.1

and then get the likes

https://developers.facebook.com/tools/explorer/?method=GET&path=414652589771%2Flikes&version=v2.1

Document

URL /?id={url}

Represents an external URL as it relates to the Facebook social graph - shares and comments from the URL on Facebook, and any Open Graph objects associated with the URL.

Reference http://harshtechtalk.com/how-get-likes-count-posts-comments-facebook-graph-api/

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-01-07 03:20

All previous answers have since been deprecated. This method works as of Aug 2016:


To get the like count of any URL:

GET request: https://graph.facebook.com/[url]/access_token=[access_token]

Then grab shares->share_count from the returned JSON object.


Fan count for a Facebook page:

GET request: https://graph.facebook.com/[url]/?fields=fan_count&access_token=[access_token]

Then grab the 'fan_count' field from the returned JSON object.


You can test this out and get your access token using the Graph API Explorer

查看更多
登录 后发表回答