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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题