无法将应用程序重定向回其Facebook页面选项卡

时间:2012-03-29 21:05:46

标签: php facebook-graph-api facebook-php-sdk

我刚刚创建了一个Facebook应用程序,这是我的第一次尝试,在完成所有内容时,我被困在这种奇怪的情况:

在用户允许该应用程序进行身份验证后,该应用程序会将我的浏览器重定向到Facebook以外的网站页面,而不是仅仅重定向页面选项卡中的iframe。

这可能是因为在$redirect_uri我已经提供了托管该应用的网站地址,但是当我尝试将$redirect_uri更改为应用的页面标签地址时,facebook会出现此错误:

An error occurred. Please try again later.

<?php
    include ('src/facebook.php');
    $app_id = "160323532538730";
    $app_secret = "****************";
    $redirect_uri = "https://myhostingpage.com/landing.php";
    //$redirect_uri = "http://www.facebook.com/Mypage/app_160323532538730";

    $config = array();
    $config["appId"] = $app_id;
    $config["secret"] = $app_secret;
    $config["fileUpload"] = true;
    // optional

    $facebook = new Facebook($config);
    $user = $facebook -> getUser();

    if ($user) {
        $logoutUrl = $facebook -> getLogoutUrl();
        $user_profile = $facebook -> api('/me');
        $signed_request = $facebook -> getSignedRequest();

        list($encoded_sig, $payload) = explode('.', $_REQUEST["signed_request"], 2);
        $signed_request = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

        $access_token = null;
        if (!empty($_SESSION['access_token'])) {
            $access_token = $_SESSION['access_token'];
        } else if (!empty($signed_request['oauth_token'])) {
            $access_token = $signed_request['oauth_token'];
        }

        if ($access_token != null) {
            $id = $signed_request["user_id"];
            $authorized_code = $_GET["code"];
            $oauth_token = $signed_request["oauth_token"];
            $like_status = $signed_request["page"]["liked"];
            if ($like_status) {
                echo "hello";
            } else {
                echo "Like this page";
            }
        } else if (!empty($_GET["error"])) {
            echo "user hasn't authorized your app";
        } else if (!empty($_GET["code"])) {
            $authorized_code = $_GET["code"];
            $authenticate_url = "https://graph.facebook.com/oauth/access_token?client_id=" . $app_id . "&redirect_uri=" . $redirect_uri . "&client_secret=" . $app_secret . "&code=" . $authorized_code . "";
            $access_token = $facebook -> getAccessToken();
            $_SESSION['access_token'] = $access_token;
            header('Location: http://www.facebook.com/Mypage/app_160323532538730');
        }
    } else {
        $loginUrl = $facebook -> getLoginUrl(array('scope' => 'publish_stream email user_photos', 'redirect_uri' => $redirect_uri));
        echo("<script>top.location.href='" . $loginUrl . "' </script>");
    }
    ?>

1 个答案:

答案 0 :(得分:-1)

试试这个。

$redirect_uri必须是您的Facebook页面网址,
Facebook应用配置中的页面标签网址字段必须是您的主机路径。

微米。