douxiji8707 2012-06-03 19:10
浏览 39
已采纳

在facebook app身份验证后,$ _GET数组始终为空

I am trying to play around with authentication for an app I am working on atm. The app will be tab based. I am now strugling with an infinite loop while trying to authenticate the app. The authentication procces is like this:

if (!isset($_REQUEST["code"])) {
            $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
            $dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
                    . $appId . "&redirect_uri=" . urlencode($redirectUri) . "&state="
                    . $_SESSION['state'];

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

        if ($_REQUEST['state'] == $_SESSION['state']) {
            $code = $_GET["code"];
            $token_url = "https://graph.facebook.com/oauth/access_token?"
                    . "client_id=" . $appId . "&redirect_uri=" . urlencode($redirectUri)
                    . "&client_secret=" . $appSecret . "&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.");
        }

The $redirectUri is now set to "http://www.facebook.com/pages/DummyPage/PAGE_ID?sk=app_APP_ID" but once I am redirected back, after the authentication, I am unable to read $_GET or $_REQUEST arrays because they are empty (even though I can see that the url I get after auth. has "code", "state" etc. in it). This will result in infinite loop, because I am stuck in the first if statement.

What is interesting here is that when I change the $redirectUri to the url where my app sits in - right now I am testing it on localhost - so I put in "http://localhost/test/index.php" - the infinite loop wont happen because I am redirected outside facebook and I can read from $_GET array.

I need to stay on facebook when authenticating. Do you guys know how to get around this problem?

  • 写回答

1条回答 默认 最新

  • douhao5280 2012-06-04 08:00
    关注

    Please use the Facebook PHP SDK for things that you're trying right now.

    It makes the thing a lot easier and you probably get to your goal in less time.

    OR

    if you want to do manually and interested in learning the internals, you need to check page tab authentication and signed request

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示