[英]AJAX with Facebook Authentication
我已经构建了一个完全基于AJAX的应用程序,该应用程序没有页面刷新,也不会使用AJAX加载所有内容。 现在,我想以一种不会重定向用户以刷新页面的方式嵌入facebook身份验证。
目前,facebook以这种方式工作:
通过单击Facebook连接按钮https://graph.facebook.com/oauth/authorize?client_id=xxxxxxxxxxxxxx&redirect_uri=http://www.xxxxxxxxxx.com/JoinFB.html?&type=user_agent&display=popup&scope=offline_access将用户重定向到Facebook ,publish_stream,短信,电子邮件,user_birthday,user_photos,user_photo_video_tags,user_checkins,friends_checkins
在用户操作之后,无论是“允许”还是“禁止”重定向到http://www.xxxxxxxxxx.com/JoinFB.html ,然后我都可以使用Javascript读取用户信息。
问题是我可以使用IFRAME或其他方法克服此页面重定向过程吗?
如何将IFRAME检测为允许的用户?
我将使用JS-SDK ,一个完美的例子是Facebook PHP-SDK例子本身:
<?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>
现在单击fb:login-button
:
<fb:login-button scope="offline_access,publish_stream,sms,email,user_birthday,user_photos,user_photo_video_tags,user_checkins,friends_checkins"></fb:login-button>
将打开一个Facebook弹出窗口,并且在授权过程之后,该弹出窗口将自动关闭并且auth.login
事件将被触发,我将更改重载方法window.location.reload();
ajax调用以执行您正在执行的任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.