gpt4 book ai didi

javascript - 带有 Facebook 身份验证的 AJAX

转载 作者:行者123 更新时间:2023-11-29 18:34:15 28 4
gpt4 key购买 nike

我构建了一个完全基于 AJAX 的应用程序,它没有页面刷新并使用 AJAX 加载所有内容。现在我想以一种不会重定向用户来刷新页面的方式嵌入 facebook 身份验证。

目前facebook是这样工作的:

用户正在通过单击 Facebook Connect 按钮重定向到 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,publish_stream,sms,email,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 example本身:

<?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 调用以执行您正在做的任何事情。

关于javascript - 带有 Facebook 身份验证的 AJAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4794524/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com