简体   繁体   中英

Getting facebook page followers' count with Graph Api and Javascript SDK

I have django app and I need to show the number of followers of its facebook page. In my base.html I have hardcoded the number of FB followers, but this is not a feasible longterm solution.

I've been experimenting with inline javascript, and this is the state of my code:

 <script> window.fbAsyncInit = function() { FB.init({ appId : 'NumOfMyId', xfbml : true, version : 'v2.4' }); FB.api( '/MyWebSite', 'GET', {"fields":"likes"}, function(response) { console.log(response); //here should go something so that the number of people liking the page gets appended to an html element by id; }); }; (function(d, s, id){ var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) {return;} js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_US/sdk.js"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); </script> 

I need the facebook api to give me the number of followers, which is not a problem in the graph api explorer, but I have no clue how to accomplish it in my tag.

Any example/advice or redirecting to similar solved questions/issues/github project would be super appreciated!

~good vibes to y'all

If you're using jQuery on your page, you can do something like this:

<p>The total number of likes in my page is <span id="number-of-likes"></span>.</p>


function(response) {
    console.log(response);
    $('#number-of-liles').html(response.NUMBER_OF_LIKES_FIELD);
});

Where response.NUMBER_OF_LIKES_FIELD is the correct field name that has the number of likes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM