gpt4 book ai didi

javascript - 如何使用facebook javascript sdk在页面刷新后检查经过身份验证的用户的登录状态

转载 作者:行者123 更新时间:2023-11-29 23:59:25 25 4
gpt4 key购买 nike

我设法使用 facebook js sdk 开发了登录/注销。

登录后,我可以得到用户的邮箱。

我在刷新整个页面后尝试获取经过验证的用户的信息时遇到问题。

不知何故,FB 对象不再存在了。

我得到错误:

Uncaught ReferenceError: FB is not defined

如何初始化 FB 对象?

ps:我已经阅读并编写了解决方案:

FB is not defined javascript

但它不起作用。

在那个例子中,/me 被替换为/l214.animaux

我不知道用户个人资料 ID。

我的代码分为 2 个 php 文件:

索引.php:

<div class="fb-login-button" onlogin="checkLoginState();" data-scope="public_profile,email" data-max-rows="1" data-size="xlarge" data-show-faces="false" data-auto-logout-link="true"></div>

和 facebook_login.php:

<!DOCTYPE html>

<html lang="en">

<head>
<?php
$title = Yii::app()->name;
?>
<title><?php echo $title; ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>

<body class="inner-page page-produse">

<div id="fb-root"></div>
<script>

(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id))
return;
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.8&appId=1813020315643394";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function api_call_get_info_about_the_logged_in_user() {

console.log('Welcome! Fetching your information.... ');

FB.api('/me', {fields: 'email'}, function (response) {

console.log('Successful login for: ' + response.email);

});

}

window.fbAsyncInit = function () {
FB.init({
appId: '1813020315643394',
xfbml: true,
version: 'v2.8'
});

api_call_get_info_about_the_logged_in_user();
};

function checkLoginState() {
FB.getLoginStatus(
function (response) {
console.log('facebook get login status');
console.log(response);
if (response.status === 'connected') {

// Logged into your app and Facebook.

api_call_get_info_about_the_logged_in_user();

} else if (response.status === 'not_authorized') {

// The person is logged into Facebook, but not your app.

} else {

// The person is not logged into Facebook, so we're not sure if

// they are logged into this app or not.

}
}
);
}

</script>

<div class="wrapper">

<div class="site-wrapper">

<?php
echo $content;
?>

<div class="site-wrapper-overlay"></div>

</div> <!-- .site-wrapper -->

</div> <!-- .wrapper -->

</body>

</html>

最佳答案

经过多次尝试,我尝试调用 checkLoginState 函数,它成功了。

window.fbAsyncInit = function () {
FB.init({
appId: '1813020315643394',
xfbml: true,
version: 'v2.8'
});

//api_call_get_info_about_the_logged_in_user();
checkLoginState();
};

关于javascript - 如何使用facebook javascript sdk在页面刷新后检查经过身份验证的用户的登录状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40973030/

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