【问题标题】:How can I get a user authenticated in my page tab如何在我的页面选项卡中获得经过身份验证的用户
【发布时间】:2013-04-11 20:13:52
【问题描述】:
在我寻找答案的过程中,我只找到了关于问题所在的线索,没有找到可行的解决方案,或者有类似问题的人。
当我在 facebook 之外浏览我的应用程序时,我可以毫无问题地进行身份验证,并且所有 facebook 交互都可以正常工作。
当我在页面选项卡中安装我的应用程序时,由于以下错误,我无法进行身份验证:
拒绝显示
'https://www.facebook.com/dialog/oauth?client_id=175164365974824&redirect_ur…er%2F&state=6790b76872277c825f5bb749ed167152&scope=publish_actions%2Cemail'
在一个框架中,因为它将“X-Frame-Options”设置为“DENY”。
我可以从中得到的是,facebook 不允许它的身份验证页面显示在我的页面标签 iframe 中。
如何让用户在我的页面标签中通过身份验证并在身份验证后重定向到我的页面标签? (我不希望将我的应用用作独立网站)。
其他重要信息:我正在使用 PHP SDK 进行身份验证和获取用户数据。
【问题讨论】:
标签:
oauth-2.0
facebook-php-sdk
x-frame-options
【解决方案1】:
我发现单独使用 PHP SDK 是不可能的。我需要结合 Javascript SDK 和 PHP SDK。我使用 Javascript SDK 来处理登录,使用 PHP SDK 来获取用户数据并发布到墙操作等。
我在 PHP SDK Git 中找到了这个示例:https://github.com/facebook/facebook-php-sdk/blob/master/examples/with_js_sdk.php
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '344617158898614',
'secret' => '6dc8ac871858b34798bc2488200e503d',
));
// See if there is a user from a cookie
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<?php if ($user) { ?>
Your user profile is
<pre>
<?php print htmlspecialchars(print_r($user_profile, true)) ?>
</pre>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
</body>
</html>
【解决方案2】:
很遗憾,您不能使用 PHP 来执行此操作,因为您必须将他重定向到同一窗口上的新 url,您必须使用 Java 脚本来访问活动窗口。
if (!array_key_exists('oauth_token', $sdata)){
//set loginURL and params to get Permissions
$params = array( //offline acces needed to post to users wall (dunno why)
'redirect_uri' => $my_url, //your redirect URI - facebook.com/yourpage/_appid
'client_id' => $app_id[path],
'scope' => 'email, publish_stream', //your permissions
);
$loginUrl = $facebook->getLoginUrl($params);
echo "<script>
top.location.href='".$loginUrl."';
</script>";
}else{
//if the user already authed send him here
header ('location: nextfile.php');
}