我得到了帮助当有人访问尚未授权应用程序的画布页面时,这篇文章中获得了帮助,以弄清楚如何重定向到 Facebook 授权页面。
现在,我期待在用户授权后,FB 将重定向到画布页面(https://app.facebook.com/myapp)。但它正在重定向到画布网址(https://myhostingapp.com/game.php?..)
这是预期的还是我们可以做些什么来控制它。授权后如何告诉 API 重定向到画布页面?
现在我可以考虑使用 $_SERVER[HTTP_REFERER] 来查看我是否来自授权页面,如果是,则再次将页面重定向到画布页面。但我希望有更好的方法来做到这一点
画布网址代码:
if (Config::$fbAvailalbe){ //see below how this variable is derived
echo "fb is available";
$facebook = new Facebook(array(
'appId' => Config::$appId,
'secret' => Config::$appSecret,
));
$user = $facebook->getUser();
if ($user) {
echo "app installed";
try {
// Proceed knowing you have a logged in user who's authenticated.
$profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if (!$user) {
echo " app not installed gonna redirect";
$scope = "scope=email,read_stream,read_friendlists,publish_stream";
$redirect = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => $scope
)
);
echo '<script>top.location="' . $redirect . '";</script>';
exit();
//header("Location: $redirect".$scope);
}
if ($profile){
$firstName = $profile['first_name'];
$sid = $profile['id'];
var_dump($profile);
}else{
echo "unable to get profile";
}
}else{
echo "fb unavailable, using dmmy";
$firstName = "Dummy Name";
$sid = Config::$testsid;
}
来自 Config.php 的片段
//this is to derive the $fbAvailable variable
//we get this condition satisfied when we run the page runs from fb canvas
if (isset($_SERVER['HTTP_REFERER'])){
if (substr_count($_SERVER['HTTP_REFERER'],'apps.facebook.com')){
self::$fbAvailalbe = 1;
}
}