Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 911
Lorenzo Avatar asked Apr 19 '19 11:04

Lorenzo


People also ask

Is Facebook Graph API deprecated?

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.

Is Facebook Graph API restful?

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.

Does Facebook use graph API?

The Graph API is the primary way for apps to read and write to the Facebook social graph.


3 Answers

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

like image 127
Taku Yoshi Avatar answered Oct 22 '22 20:10

Taku Yoshi


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

like image 10
quick Avatar answered Oct 22 '22 20:10

quick


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.
like image 5
Anton Lukin Avatar answered Oct 22 '22 19:10

Anton Lukin