Here's the code I've been testing with:
<?php
$accessToken = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials');
$accessToken = explode('=', $accessToken);
$accessToken = $accessToken[1];
?>
<div id="fb-root"></div>
<script>
w开发者_开发技巧indow.fbAsyncInit = function()
{
FB.init({
appId: 'APP_ID',
status: true,
cookie: true,
xfbml: true
});
FB.api('/me?access_token=<?php echo $accessToken; ?>', function(user)
{
if (user != null)
{
console.log(user);
}
});
};
(function()
{
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol + '//connect.facebook.net/en_GB/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
I'm getting the following error returned:
An active access token must be used to query information about the current user.
I registered my site URL with Facebook to get my ID/Secret and am running the above script within the directory I specified when registering.
What am I doing wrong?
You are mixing things up here, trying to access user information with an App access_token
!
Have a read of the Authentication document, the User login
section. Also I would strongly recommend the use the Facebook PHP-SDK, where the library will take care of all the authentication and authorization exchange process.
I think you are using old rest api to get access token why you dont use graph api to get access token?
define('FACEBOOK_APP_ID', 'Your API ID'); define('FACEBOOK_SECRET', 'YOUR SECRET'); function get_facebook_cookie($app_id, $application_secret) { $args = array(); parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args); ksort($args); $payload = ''; foreach ($args as $key => $value) { if ($key != 'sig') { $payload .= $key . '=' . $value; } } if (md5($payload . $application_secret) != $args['sig']) { return null; } return $args; } $cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET); $name=json_decode(file_get_contents('https://graph.facebook.com/me?access_token='.$cookie['access_token']));
I think its prettey simple
精彩评论