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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分