I have the following script:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'app_id_of_one_of_my_apps',
xfbml : true,
version : 'v2.7'
});
FB.api("/{numerical_profile_id}/likes",
function (response) {
console.log(response);
if (response && !response.error) {
/* handle the result */
}
});
};
(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/pl_PL/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
All I get is error An access token is required to request this resource
How do I securely get Facebook fanpage likes count via javascript? Or do I need to make a PHP script that will do it for me?
I have the following script:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'app_id_of_one_of_my_apps',
xfbml : true,
version : 'v2.7'
});
FB.api("/{numerical_profile_id}/likes",
function (response) {
console.log(response);
if (response && !response.error) {
/* handle the result */
}
});
};
(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/pl_PL/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
All I get is error An access token is required to request this resource
How do I securely get Facebook fanpage likes count via javascript? Or do I need to make a PHP script that will do it for me?
Share Improve this question edited Sep 16, 2016 at 7:43 andyrandy 74k9 gold badges114 silver badges132 bronze badges asked Sep 15, 2016 at 21:23 GrzegorzGrzegorz 3,6084 gold badges30 silver badges48 bronze badges 3- 1 if you want to fetch information for a particular user you need that user's permission. The access_token represents that the user gave permission to your site to do so. – Benjamin Commented Sep 15, 2016 at 21:25
- On my site, i want to show Like count of my fanpage. Like "Like" button. – Grzegorz Commented Sep 15, 2016 at 21:28
- You either need to make the user login, so that you can use their user access token – or you need to do it on the server side, so that you can use your app or a page token. (Those two should never be exposed in client-side code.) Plus, doing it on the server side allows you to implement some basic form of caching, so that you don’t run into the API rate limits so easily. – C3roe Commented Sep 16, 2016 at 8:12
1 Answer
Reset to default 9This is the API to get the fan count:
/{page-id}?fields=fan_count
...or with the JavaScript SDK:
FB.api('/{page-id}', {fields: 'fan_count'}, (response) => {
console.log(response);
});
You can use an App Token if the page is not restricted by age or location, but you MUST use the Token server side. Tokens are always meant to be kept secret. No login is needed for the App Token, it is just a bination of App ID and App Secret (ID|Secret) and it is permanent.
More information about Tokens:
- https://developers.facebook./docs/facebook-login/access-tokens
- http://www.devils-heaven./facebook-access-tokens/
Side note: You should do that API call server side anyway and cache the result in your database, or you will definitely hit the API limit if you get a lot of users/hits.