douwu8251 2012-12-19 08:03
浏览 52
已采纳

应用程序授权后Facebook重定向到画布URL而不是画布页面

I got help from this post to figure out how to redirect to Facebook authorization page when someone access canvas page who has not yet authorized the app.

Now, I was expecting that after user authorizes the permissions FB will redirect to the canvas page (https://app.facebook.com/myapp). But it is redirecting to the canvas url (https://myhostingapp.com/game.php?...)

Is this expected or is there any thing we can do to control it. How do I tell the API to redirect to the canvas page after authorization?

Right now I am able to think of using the $_SERVER[HTTP_REFERER] to see if I am coming from authorization page and if yes the redirect the page to the canvas page again. But I am hoping there could be much better way of doing it

Canvas url code:

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;

}

Snipet from 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;
        }
    }
  • 写回答

1条回答 默认 最新

  • duanlaiyin2356 2012-12-19 09:16
    关注

    check documentation here.

    Two errors in the code

    Error#1:

    $scope = "scope = email,read_stream,read_friendlists,publish_stream";
    

    Error#2:

    As per the documentation here (as on now)

    req_params is 'scope'

    Not sure about the 'canvas' and 'fbconnect' parameters which might be potentially ignored as they are not supported any more

    $redirect = $facebook->getLoginUrl(
                        array(
                            'scope' => $scope,
                            'redirect_uri' => 'https://app.facebook.com/myapp'
                            )
                        );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案