开发者

Is it possible to get the number of facebook likes for a url?

开发者 https://www.devze.com 2023-02-06 06:09 出处:网络
Ideally I would like to do this using javascript client side scripting only.If that is not possible then I\'d like to do this in such a way as to have the server do the least amount of work.开发者_高级

Ideally I would like to do this using javascript client side scripting only. If that is not possible then I'd like to do this in such a way as to have the server do the least amount of work.

开发者_高级运维

Any help is appreciated.


Actually, https://graph.facebook.com/?ids=http://www.stackoverflow.com will return the number of SHARES for that url, which is distinct from the number of likes because it also includes comments and other forms of sharing. This may or may not be an important difference.

With Graph API 2.1+ there are two methods...though both now require access tokens. See below on how to get an access token that lasts forever and is not related to a user session.

Like count for URLs:

  1. First, you need to get the ID of the URL: https://graph.facebook.com/?id=http://www.stackoverflow.com (you do not need an access token for this)
  2. Once you have the id, you can get the like count via the likes endpoint. You can access the total count in the summary part of the json response. Here's an example request: https://graph.facebook.com/10150180465825637/likes/?summary=true&access_token=TOKEN_GOES_HERE (you need a valid access token here, but you can use app access token - see below)

Like count for Facebook Pages:

  1. You can get the like count directly via: https://graph.facebook.com/FacebookDevelopers/?fields=likes&access_token=TOKEN_GOES_HERE (you need a valid access token here, but you can use app access token - see below)

How to get a perpetual Access Token (App Access Token):

You can use a user access token to get these results, but often for like-count scripts that is not an option.

You'll need to use the App Access Token instead, which will allow you to run the script "offline" without an active user access token session. Previously you had to request an app access token, now it is as simple as concatenating the app id and secret with | as follows:

MY_APP_ID|MY_APP_SECRET

In otherwords, the full request from above would be something like:

https://graph.facebook.com/10150180465825637/likes/?summary=true&access_token=MY_APP_ID|MY_APP_SECRET

Keep in mind that since the app secret is required for this request you should always perform this request server-side. Client-side requests would expose your app secret to everyone.


In previous versions of Graph API (2.0 and lower) you could use FQL:

SELECT like_count FROM link_stat WHERE url="http://www.stackoverflow.com"

Here is the actual link. There about 200 likes and 2000+ shares as of this writing, so it's a big difference...


This way returns a json object:

http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27http://www.imdb.com/title/tt0638423/%27&format=json


It's pretty simple, several other anwsers on SO already showed good examples. Basically use the Graph API: http://developers.facebook.com/docs/api to query info on your URL, for example: https://graph.facebook.com/?ids=http://www.stackloverflow.com . The API retuns a JSON object which should be easy to use with Javascript.

0

精彩评论

暂无评论...
验证码 换一张
取 消