问题
Since i really don't understand the $user = $facebook->getUser(); function (it returns NULL even if the user is logged in...) I'm trying with this code. The problem is, that it gives the "An active access token must be used to query information about the current user." OAuthException and if i put the login redirect code also in the Catch section it gets into a endless redirecting loop... Can someone help me?
Thank you! :)
try {
$user_profile = $facebook->api('/me');
$scope = 'publish_stream,user_photos';
$scope_params = explode(',',$scope);
$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) && array_key_exists('user_photos', $permissions['data'][0])) {
// facebook logic
}else {
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Redirect</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script language=javascript>window.open('<?php echo $loginUrl ?>', '_parent', '')</script>
</head>
<body>
Redirecting...
</body>
</html>
<?php
exit;
}
}catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
}
回答1:
You need to pass active access token to the Graph API. Every access token has expiration time, and after it passes, you need to re-authenticate yourself. You should check for presence of access token in facebook's PHP SDK ($facebook->getAccessToken();
). If it's not there - try to re-authenticate yourself. If id does not help, try using another, fresh facebook application. Whole authentication is described here: http://developers.facebook.com/docs/authentication/
Acquiring access by hand, without using PHP SDK token is nicely described in the link above.
回答2:
solution was to put all the code into one index.php file
回答3:
The loop happens like this:
- Call API
- API says token expired / invalid
- GetLoginUrl() -> redirect
- Once redirected to the oauth page, it thinks that permissions and token still valid; so it redirects back to your app
- Loop occurs (back to step 1)
Seems there is a delay between the time a token expires or a user deauthorises an app and when the oauth script actually picking up the change.
Bug Maybe?
来源:https://stackoverflow.com/questions/8911773/facebook-php-sdk-api-me-strange-behavior