Facebook PHP SDK:api('/me') 奇怪的行为
因为我真的不明白 $user = $facebook->getUser();函数(即使用户已登录,它也会返回 NULL...)我正在尝试使用此代码。问题是,它给出了“必须使用活动访问令牌来查询有关当前用户的信息”。 OAuthException 并且如果我将登录重定向代码也放在 Catch 部分中,它会进入无限的重定向循环...有人可以帮助我吗?
谢谢你! :)
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>';
}
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>';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将活动访问令牌传递给 Graph API。每个访问令牌都有过期时间,过期后您需要重新进行身份验证。您应该检查 facebook 的 PHP SDK 中是否存在访问令牌 (
$facebook->getAccessToken();
)。如果不存在 - 尝试重新验证自己的身份。如果 id 没有帮助,请尝试使用另一个新的 Facebook 应用程序。整个身份验证如下所述: http://developers.facebook.com/docs/authentication/上面的链接很好地描述了手动获取访问权限,而不使用 PHP SDK 令牌。
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.
解决方案是将所有代码放入一个index.php文件中
solution was to put all the code into one index.php file
循环是这样发生的:
似乎在令牌过期或用户取消对应用程序授权的时间与oauth脚本实际获取更改的时间之间存在延迟。
也许是虫子?
The loop happens like this:
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?