doubi6669 2011-05-30 10:32
浏览 154
已采纳

Facebook应用重定向

I have made a facebook application which when opened from apps.facebook.com/myapp points perfectly to my domain's index.php and can be seen in the facebook app window , but problem occurs when i try to redirect it to my register page ,it gets redirected to my domain , i want all the pages to be viewed in facebook app window . tried to use fb:redirect sdk function but site says they are going to deprecate these codes.

<?php
if(preg_match('/apps.facebook.com/',$_SERVER[HTTP_REFERER])){
    $app_id = '';
    $api_key = '';
    $app_secret = '';
    $canvas_page = 'mydomain/index.php';
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id=".
    $app_id."&redirect_uri=".urlencode($canvas_page)."&scope=email,user_birthday,user_interests,user_about_me";
    $signed_request = $_REQUEST["signed_request"];
    list($encoded_sig, $payload) = explode('.', $signed_request, 2);
    $data = json_decode(base64_decode(strtr($payload,'-_', '+/')), true);
    if (empty($data["user_id"])){
        echo("<script> top.location.href='".$auth_url ."'</script>");
    }else{
        $canvas_page="mydomain/register.php";
        echo ("<script> top.location.href='".$auth_url."'</script>");
    }
}else{
    echo "No facebook";
}
?>

the code works perfectly up till echo("<script> top.location.href='" .$auth_url ."'</script>") when $canvas_page is the one set in facebook app dev form ; but gets redirected to some other page when $canvas_page is changed to my register.php page . where am i going wrong ?

thanks in advance

  • 写回答

3条回答 默认 最新

  • duanhui1185 2011-05-30 17:44
    关注
    <?php
        //facebook application
        //set facebook application id, secret key and api key here
        $fbconfig['appid' ] = "123456392899383";
        $fbconfig['api'   ] = "97eb2asdfasdf3f20d4421b0fe8c1b2";
        $fbconfig['secret'] = "5c1d4asdfasdf71b59806b69c386b2ca";
    
        //set application urls here
        $fbconfig['baseUrl']    =   "http://www.your-url.com/";
        $fbconfig['appBaseUrl'] =   "http://apps.facebook.com/appname/"; 
    
        $uid            =   null; //facebook user id
    
        try{
            include_once "facebook.php";
        }
        catch(Exception $o){
            echo '<pre>';
            print_r($o);
            echo '</pre>';
        }
        // Create our Application instance.
        $facebook = new Facebook(array(
          'appId'  => $fbconfig['appid'],
          'secret' => $fbconfig['secret'],
          'cookie' => true,
        ));
    
        //Facebook Authentication part
        $session = $facebook->getSession();
        $loginUrl = $facebook->getLoginUrl(
                array(
                'canvas'    => 1,
                'fbconnect' => 0,
                'req_perms' => 'email,publish_stream,status_update,user_birthday,user_location,user_work_history'
                )
        );
    
        $fbme = null;
    
        if (!$session) {
            echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
            exit;
        }
        else {
            try {
                $uid      =   $facebook->getUser();
                $me     =   $facebook->api('/me');
                //PUT THE APPLICATION CODE HERE>
            } catch (FacebookApiException $e) {
                echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
                exit;
            }
        }
    
        function d($d){
            echo '<pre>';
            print_r($d);
            echo '</pre>';
        }
    ?>
    

    You can use this script to properly authorise the app an regular use it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测