0

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://megam.info&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.

1

If you do not want use access token or nginx proxy solution, see https://megam.info/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>"
}
  • Perfect.Thank you. – Lorenzo Jun 8 at 17:52
8

The API has changed indeed.

It should be like this.

https://graph.facebook.com/?id=https://megam.info&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..

  • 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 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 at 15:15
3

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.
  • 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 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.