gpt4 book ai didi

facebook-javascript-sdk - FB.getLoginStatus弹出窗口内的FB.login被阻止

转载 作者:行者123 更新时间:2023-12-04 13:49:51 25 4
gpt4 key购买 nike

我正在尝试使用Facebook Javascript SDK将Facebook登录信息集成到我的网站中。根据Facebook开发人员文档here提供的分步说明,这是我编写的测试代码:

<script>
window.fbAsyncInit = function() {
FB.init({
appId : '{$MY_APP_ID}',
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse social plugins on this page
version : 'v2.1', // use version 2.1
status : true, // check FB login status
});
};

function fblogin() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert('Logged in.');
}
else {
FB.login();
}
}, true);
}
</script>
<button onclick="fblogin()">login</button>
<script src="//connect.facebook.net/en_US/sdk.js"></script>

抱歉,由于网站域名的限制,我无法将其摆弄。但是我尝试了自己的域,在Chrome和Firefox中弹出了Facebook登录窗口,但被Safari阻止了。这有点奇怪,Facebook开发人员文档提供的代码片段有什么问题吗?

最佳答案

正如我们在评论中已经讨论的那样,必须在用户交互时调用FB.login,文档中的示例是不正确的。

这应该是一个有效的示例,说明如何正确进行登录,这些步骤是从Facebook文档中的几篇文章一起复制而来的:

<script>
//call this on user interaction (click)
function doLogin() {
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email,user_friends'});
}

window.fbAsyncInit = function() {
FB.init({
appId : 'your-app-id',
xfbml : true,
version : 'v2.1'
});

FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
};

(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";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<button onclick="doLogin()">login</button>

更多说明: http://www.devils-heaven.com/facebook-javascript-sdk-login/

关于facebook-javascript-sdk - FB.getLoginStatus弹出窗口内的FB.login被阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26817023/

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