我有一个facebook iframe应用程序,它可以正确登录并授权该应用程序,但getUser()只能在第一页上工作。一旦用户单击iframe中新页面的链接,getUser()就会返回0。
奇怪的是,同样的代码适用于另一个应用程序。。。我完成了我想要的所有点击,getUser()返回一个有效的ID。
不起作用的应用程序:https://apps.facebook.com/celestial_glory/
这样做的(相同的代码库):https://apps.facebook.com/uprisingstlouis/
这是我正在使用的代码:
require_once ('fb/facebook.php');
// snip... set $app_id, $secret, and $canvas_page
// first, try normal facebook getUser(). If that works, awesome.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
));
$signed_request = $_REQUEST['signed_request'];
// Get User ID
$user = $facebook->getUser();
if ($user != '0') return 'fb=' . $user; // works once
// getUser() didn't work. Try oAuth. Maybe user needs to log in or
// authorize the game?
$auth_url = 'http://www.facebook.com/dialog/oauth?client_id='
. $app_id . '&redirect_uri=' . urlencode($canvas_page);
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo '<a target="_top" href="' . $auth_url . '">Login to Facebook</a>';
exit;
// normally we would auto-redirect, but with a uid of 0, this just auto-redirects
// echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
return 'fb=' . $data['user_id'];
}
有什么想法吗?我已经三次检查了应用程序ID和机密以及画布页面。如果这些都是错误的,我预计任何一页,甚至第一页,都不会起作用。
将Facebook PHP-SDK初始化更改为:
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie' => true // this!
));
getUser
在第一个页面上工作,因为它可以从signed_request
获取用户(由Facebook发布到画布页面URL)。因此,一旦用户开始在应用程序中进行更深层次的导航,您就需要某种方法来跟踪他。您可以以某种方式自行传递signed_request
,也可以按照上面的建议简单地启用内置的PHP-SDK cookie支持。