5

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.

ekad
  • 14,436
  • 26
  • 44
  • 46
Lorenzo
  • 163
  • 2
  • 9

4 Answers4

14

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..

Taku Yoshi
  • 141
  • 6
  • Hello, thanks for the clarification, i will follow your indication....Yesterday it was possible to get share count without the access token key.It was perfect. That's why i'm looking for a way to get it without access token key. Maybe users will help us. – Lorenzo Apr 19 '19 at 15:13
  • I develop and distribute a WordPress Theme. I don't want to hard-code my personal token in the Theme. I'm looking for another way. Let's see what other people have to say... – Taku Yoshi Apr 19 '19 at 15:15
  • Do you know what the rate limit is? – Union find Aug 29 '19 at 15:27
  • 1
    thanks a lot, this is the simplest explanation I have found. For Python users you need to `import requests` and then store the `url = "blahblah.com/something"` and `token = ... `. Then just run `requests.get(f'https://graph.facebook.com/?id={url}&fields=engagement&access_token={token}').text` – Matt Jun 03 '20 at 08:55
10

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

quick
  • 1,104
  • 10
  • 19
  • Perfect.Thank you. – Lorenzo Jun 08 '19 at 17:52
  • This doesn’t get the share count. It actually gets “likes, shares and comments” combined. If you want to get separate values use field url parameter as `fields=og_object,engagement` respectively `https://graph.facebook.com?id=&fields=og_object,engagement` – Gardelin Sep 09 '20 at 12:04
  • Yes, you are right. Engagement count is the sum of reactions, comments, shares count. Advantage of this solution is you do not need the API token. – quick Sep 09 '20 at 13:11
  • 1
    Yep. "message": "(#2) Service temporarily unavailable". – Artem P Sep 26 '20 at 05:27
  • Do you know an alternative solution? – Miguel Dec 22 '20 at 18:57
5

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.

  1. You need an access token. Navigate to https://developers.facebook.com/ and make an app.

  2. Go to Graph explorer and copy the token. To obtain permanent token follow this short guide

  3. 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;
    }

    ...
}
  1. Now you can make response replacing graph.facebook.com with your custom domain.

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/

  1. Pay attention to facebook api limits. If you have a large number of requests you can try to use page token. For each engagement user to your page you can make 4800 requests to graph api per day.
Anton Lukin
  • 307
  • 2
  • 8
  • Your solution is very clever, thanks.But i need to simplify all the process.It was only an ajax call before.I need to find another way. – Lorenzo Apr 22 '19 at 09:05
4

If you have app in facebook, its very simple without login, you can get it.

https://graph.facebook.com/?id={URL}&fields=engagement&access_token={your-app_id}|{your-app_secret}

Response will be like :

{
  "engagement": {
    "reaction_count": 36,
    "comment_count": 2,
    "share_count": 20,
    "comment_plugin_count": 3
  },
  "id": "https://www.example.com"
}

Ref : https://developers.facebook.com/docs/facebook-login/access-tokens

Niko Jojo
  • 1,204
  • 2
  • 14
  • 31