Since today when I try to get the share count the answer is :share field is deprecated for versions v2.9 and higher.
Ex with : https://graph.facebook.com/?id=https://stackoverflow.com&fields=share
Without &fields=share the json content is displayed but without the share value.
I need to get the share count Facebook from an url.
API Version Deprecations: As part of Facebook's Graph API and Marketing API, please note the upcoming deprecations: August 3, 2021: Graph API v3. 3 will be deprecated and removed from the platform. August 25, 2021 Marketing API v9.
Because the Facebook Graph API is a restful JSON API, you can run queries directly in any standard browser; you'd simply be making HTTP calls.
The Graph API is the primary way for apps to read and write to the Facebook social graph.
The API has changed indeed.
It should be like this.
https://graph.facebook.com/?id=https://stackoverflow.com&fields=engagement&access_token=user-access-token
You need an access token. If you have a Facebook, go to https://developers.facebook.com/ and make an app.
Graph API Explorer
Then click "Graph API Explorer".
Get Token
and "Get Token" (Get App Token). That's it.
If you use JavaScript for a count, it's will be something like this.
// split('#')[0] : Remove hash params from URL
const url = encodeURIComponent( window.location.href.split('#')[0] );
$.ajax( {
url : '//graph.facebook.com/?id=' + url + '&fields=engagement&access_token=user-access-token',
dataType : 'jsonp',
timeout: 5000,
success : function( obj ) {
let count = 0;
if ( typeof obj.engagement.reaction_count !== 'undefined' ) {
count = obj.engagement.reaction_count;
}
// do something with 'count'
},
error : function() {
// do something
}
} );
There are other count types such as comment_count and share_count.
See https://developers.facebook.com/docs/graph-api/reference/v3.2/url
Is there any way to receive a count without sending an access token?
I wanna know that myself lol
UPDATE:
Thanks to Anton Lukin.
Yeah. I shouldn't show an access token. It must be hidden. I feel very foolish.
So now quick's answer. This really works without the token!
My final (I hope will be final) answer is like this.
// split('#')[0] : Remove hash params from URL
const url = encodeURIComponent( window.location.href.split('#')[0] );
$.ajax( {
url: '//graph.facebook.com/?id=' + url + '&fields=og_object{engagement}',
dataType : 'jsonp',
timeout: 5000,
success : function( obj ) {
let count = 0;
try {
count = obj.og_object.engagement.count
} catch (e) {
console.log(e)
}
// do something with 'count'
},
error : function() {
// do something
}
} );
One point here is that when nobody has ever shared the targeted page, 'og_object.engagement' isn't even defined.
I thought I'd get 0 as a return valule. But that's not the case.
So let's use try-catch.
Now my only concern is API Limits. If your site gets a lot of pageviews, this updated version may not work..
If you do not want use access token or nginx proxy solution, see https://stackoverflow.com/a/45796935/2424880:
You can use the query
https://graph.facebook.com?id=<your-url>&fields=og_object{engagement}
The answer will be
{
"og_object": {
"engagement": {
"count": 197,
"social_sentence": "197 people like this."
},
"id": "895062470590407"
},
"id": "<your-url>"
}
UPDATE 2021: You need access token for this request. You can get temporary access token in Graph API Explorer or generate it with your custom app
Since you can't display your access token on front-end, I suggest you to proxy requests with nginx, hidding your access_token on your server.
You need an access token. Navigate to https://developers.facebook.com/ and make an app.
Go to Graph explorer and copy the token. To obtain permanent token follow this short guide
Add custom rule to your nginx config
http {
...
# Optional: set facebook cache zone
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=facebook:100m inactive=60m;
...
}
server {
server_name example.org;
...
location /facebook {
# Optional: don't log requests
access_log off;
log_not_found off;
# Allow get shares only for single domain (remove condition to allow all domains)
if ( $arg_id ~ "^https://example.org/" ) {
set $args"${args}&access_token=your_access_token_here";
}
# Set dns resolver address (you can change it with any dns server)
resolver 1.1.1.1;
proxy_pass https://graph.facebook.com?$args;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Optional: add cache for 30 minutes
proxy_ignore_headers Expires;
proxy_ignore_headers Cache-Control;
proxy_cache facebook;
proxy_cache_valid any 30m;
proxy_cache_key $host$uri$is_args$arg_id;
}
...
}
Before:
https://graph.facebook.com/?fields=engagement&callback=FB.Share&id=https://example.org/&access_token=your_access_token
After:
https://example.org/facebook?fields=engagement&callback=FB.Share&id=https://example.org/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With