Facebook要求权限和身份验证

时间:2012-03-28 20:32:52

标签: php facebook

我目前正在制作一款Facebook应用,并且在使用权限时遇到了一些问题。我需要每个用户的许可才能在那里使用位置和朋友位置。我的代码过去曾有过工作,但最近似乎停止了工作。假设我的app_id和app_namespace已声明。所有发生的事情是我被重定向到if(empty($ code))块中的dialog_url,但据我所知,$ code不应为空。任何帮助将不胜感激。感谢。

require_once('sdk/src/facebook.php');
 require_once('AppInfo.php');
 require_once('utils.php');
 require_once('connection.php');

 $facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
 ));

 $user_id = $facebook->getUser();
       if(user has ran the app before)
    {


        mysql_close($connection);

        session_start();

        $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
        $dialog_url = "http://www.facebook.com/dialog/oauth?client_id"
        . $app_id . "&redirect_uri=" . urlencode($my_url2) . "&state="
        . $_SESSION['state'];

        echo("<script> top.location.href='" . $dialog_url . "'</script>");

    }
    else
    {

        mysql_close($connection);

        session_start();
        $code = $_REQUEST["code"];

        if(empty($code)) {
            $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
            $dialog_url = "http://www.facebook.com/dialog/oauth?client_id"
            . $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope= user_location, friends_location, offline_access" . "&state="
            . $_SESSION['state'];

            echo("<script> top.location.href='" . $dialog_url . "'</script>");
        }

        if($_REQUEST['state'] == $_SESSION['state']) {
            $token_url = "https://graph.facebook.com/oauth/access_token?"
            . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
            . "&client_secret=" . $app_secret . "&code=" . $code;

            $response = @file_get_contents($token_url);
            $params = null;
            parse_str($response, $params);

            $graph_url = "https://graph.facebook.com/me?access_token"
            . $params['access_token'];

            $user = json_decode(file_get_contents($graph_url));
            echo("Hello " . $user->name);
        }
        else
        {
            echo("The state does not match. You may be a victim of CSRF.");
        }
    }

1 个答案:

答案 0 :(得分:0)

看起来太简单了,但是你的oauth网址中有空格你已经分配给$ dialog_url?