The below console logs “An active access token must be used to query information about the current user.” Apparently my js isn’t getting the access token from php – how do I rectify this?The code below is a virtual clone of what FB posted on their blog:
https://developers.facebook.com/blog/post/534/
<?php
$basePath = "/usr/lib/fb";
// Include the fb sdk
require 'fb/src/facebook.php';
// Create our Application instance
$facebook = new Facebook(array(
'appId' => '#####', // I do repeat this in fb.js so I can externalize it
'secret' => '#####',
));
// See if there is a user from a cookie
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<?php if ($user_profile) { ?>
Your user profile is
<pre>
<?php print htmlspecialchars(print_r($user_profile, true)) ?>
</pre>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
FB.api('/me/permissions', function (response) {
console.log(response);
} );
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
</body>
</html>
You loaded and initiated the fb sdk, but you need to authenticate the user.
For js authentication alone you need to use the FB.login method, but if your php is in charge of authenticating the user then you can just call FB.getLoginStatus, like: