无法在 IE 中进行身份验证
我成为 Facebook 开发人员已经有一段时间了,但我刚刚开始使用新的 Facebook 应用程序布局,并停留在第一步中。所以显然我旧的身份验证方法不再起作用了。
为了解决这个问题,我尝试了 http://developers.facebook.com/docs 中给出的示例/身份验证/。但是,它似乎在 Internet Explorer 上根本不起作用。
下面是我的代码:
<?php
$app_id = "-----";
$app_secret = "------";
$my_url = "http://apps.facebook.com/myapp/";
require('facebook-php-sdk-2343fca/src/facebook.php');
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,));
$session = $facebook->getSession();
session_start();
$code = $_REQUEST["code"];
echo $_REQUEST['state']." == ".$_SESSION['state'];
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) . "&state="
. $_SESSION['state'] . '&scope=publish_stream,read_stream,user_photos,friends_photos,user_events,friends_events';
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);
//My app goes here!
} else {
echo("The state does not match. You may be a victim of CSRF.");
}
?>
到目前为止,我所知道的是问题与 $_SESSION['state']
; 有关。
任何帮助将非常感激!
I've been a Facebook developer for a while now, but I'm starting fresh with the new Facebook App layout and stuck one of the first steps. So apparently my old method of authentication doesn't work anymore.
To solve this, I tried the one given as an example at http://developers.facebook.com/docs/authentication/ . However, it doesn't seem to work at all on internet explorer.
Here's my code right below:
<?php
$app_id = "-----";
$app_secret = "------";
$my_url = "http://apps.facebook.com/myapp/";
require('facebook-php-sdk-2343fca/src/facebook.php');
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,));
$session = $facebook->getSession();
session_start();
$code = $_REQUEST["code"];
echo $_REQUEST['state']." == ".$_SESSION['state'];
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) . "&state="
. $_SESSION['state'] . '&scope=publish_stream,read_stream,user_photos,friends_photos,user_events,friends_events';
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);
//My app goes here!
} else {
echo("The state does not match. You may be a victim of CSRF.");
}
?>
So far, all I know is the problem has to do with $_SESSION['state']
;
Any help would be much obliged!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,我成功了。对于 Internet Explorer 使用 iframe 处理 P3P cookie 来说,这是一个相当模糊的问题。简而言之,只需将其放在代码开头的某个位置即可:
您可以在此处阅读更多信息: 如何使用 Facebook PHP SDK 3.0 正确处理会话和访问令牌?
Hmm, I got it working. It's a quite obscure issue with P3P cookies handling by Internet Explorer with iframes. In short, just put this somewhere in the beginning of your code:
You can read more here: How to properly handle session and access token with Facebook PHP SDK 3.0?