Lorenzo
Lorenzo

Reputation: 153

API Share count Facebook Graph deprecated?

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.

Upvotes: 5

Views: 10207

Answers (4)

quick
quick

Reputation: 1194

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

Upvotes: 10

Niko Jojo
Niko Jojo

Reputation: 1226

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

Upvotes: 4

Taku Yoshi
Taku Yoshi

Reputation: 141

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

Upvotes: 14

Anton Lukin
Anton Lukin

Reputation: 307

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.

Upvotes: 5

Related Questions