【问题标题】:Facebook redirecting to canvas url instead of canvas page after app authorization应用授权后 Facebook 重定向到画布 url 而不是画布页面
【发布时间】:2012-12-19 08:03:20
【问题描述】:

我得到了this post 的帮助,以弄清楚当有人访问尚未授权该应用的画布页面时如何重定向到 Facebook 授权页面。

现在,我期待在用户授权后,FB 将重定向到画布页面 (https://app.facebook.com/myapp)。但它正在重定向到画布 url (https://myhostingapp.com/game.php?...)

这是预期的还是我们可以做些什么来控制它。授权后如何告诉 API 重定向到画布页面?

现在我可以考虑使用 $_SERVER[HTTP_REFERER] 来查看我是否来自授权页面,如果是,则再次将页面重定向到画布页面。但我希望有更好的方法来做到这一点

画布网址代码:

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;

}

来自 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;
        }
    }

【问题讨论】:

    标签: php facebook facebook-canvas


    【解决方案1】:

    检查文档here

    代码中有两个错误

    错误#1:

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

    错误#2:

    根据文档here(如现在)

    req_params 是“范围”

    不确定“canvas”和“fbconnect”参数可能会被忽略,因为它们不再受支持

    $redirect = $facebook->getLoginUrl(
                        array(
                            'scope' => $scope,
                            'redirect_uri' => 'https://app.facebook.com/myapp'
                            )
                        );
    

    【讨论】:

    • 我现在收到“发生错误。请稍后再试。”。它与过时的参数有关吗? developers.facebook.com/docs/reference/php/facebook-getLoginUrl 没有“canvas”和“fbconnect”参数
    • oops 看起来像 'req_perms' 现在只是 'scope'?
    • 我现在已经找出了确切的错误并编辑了您答案中的代码。一旦你接受我的编辑,我会接受这个:)
    猜你喜欢
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    相关资源
    最近更新 更多