dongzhu7329 2012-11-11 09:49
浏览 72
已采纳

登录Facebook App重定向到画布URL

So I've started learning facebook application and got my first obstacle. Whenever i log into my simple hello user application, i'm redirected into canvas URL (App content opens on server directly, instead inside facebook iFrame).

Here's the code

<?php
require_once("php-sdk/facebook.php");

$config = array(
    'appId' => '',
    'secret' => ''
);

$facebook = new Facebook($config);

$user_id = $facebook->getUser();
$params = array(
    'scope' => 'read_stream, friends_likes',
    'redirect_uri' => 'https://apps.facebook.com/401822713222945/'
);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>

    <body>
<?php
if ($user_id){
    try {
        $user_profile = $facebook->api('/me','GET');
        echo 'Welcome ' . $user_profile['name'] . '!';
    } catch(FacebookApiException $e) {
        $login_url = $facebook->getLoginUrl();
        echo 'Please <a href="' . $login_url . '" onclick="top.location.href = \'' . $login_url . '\'">log in.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
    }
} else {
    $login_url = $facebook->getLoginUrl();
    echo 'Please <a href="' . $login_url . '" onclick="top.location.href = \'' . $login_url . '\'">log in.</a>';
}
?>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • dongshiran7000 2012-11-11 10:01
    关注

    Try the following right after the <body> tag.

    <script type="text/javascript">
    if(top === self){
        document.location = "<?php echo $params['redirect_uri'];?>";
    }
    </script>
    

    It will check if the current window is the most top window, if you're accessing the page directly it will then redirect you to the iframed page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化