I'm trying to output my Facebook feed to a div element.
Using this code:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'APP ID HERE',
xfbml : true,
version : 'v2.1'
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('Logged in.');
}
else {
console.log('initiate FB login...');
FB.login();
}
});
FB.api('/me/feed',function(response){
var idDiv=document.getElementById('result');
idDiv.textContent=JSON.stringify(response);
});
};
(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";
console.log(js.length);
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div id='result'></div>
I get "logged in" displayed on the console, so I know that FB.getLoginStatus()
has returned connected
.
The div
element gets populated with the following error:
{"error":{"message":"An active access token must be used to query information
about the current user.","type":"OAuthException","code":2500}}
If FB.getLoginStatus()
returned connected
, shouldn't there be an active access token?
You probably don't have the access token at the time the request to 'me/feed' is sent.
getLoginStatus does not only check if the user is authorized, it also refreshes the user session. that´s why it is important to do api calls after it is "connected".
Try putting the call to FB.api inside getLoginStatus.
This question is similar to the problem you're having:Facebook javascript sdk An active access token must be used to query information about the current user
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With