我想知道为什么好像response.status === 'not authorized'
的“其他如果”案例
并且“其他”案例(“未知”,即用户未登录到Facebook)未被执行。当我登录到我的应用程序时,response.stats ===
'connected'
中会相应地调用testAPI()。但是,当我在Chrome中打开隐身模式时,其他情况下的testAPI()函数未被调用。谁知道为什么?
window.fbAsyncInit = function() {
FB.init({
appId : 'appnumberhere', // App ID
channelUrl : '//localhost/appname/channel.php', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.status === 'connected') {
testAPI(); // works
} else if (response.status === 'not_authorized') {
testAPI(); // not called. Nothing in console.
FB.login();
} else {
testAPI(); // not called. Nothing in console.
FB.login();
}
});
}); // end async init function
testAPI函数:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
}
作为旁注,如果我没有登录,我也没有看到Facebook登录对话框弹出,这就是我认为fb.login()应该调用的内容。
额外注意事项
另外,奇怪的是,我需要在FB SDK下面<script src="//connect.facebook.net/en_US/all.js"></script>
包含<script>(function(d){ var js,....</script>
并在我的频道文件中让我的应用运行。否则,某些部件不会加载。不确定这是否相关,但这很奇怪。
答案 0 :(得分:1)
正如this question所解释的那样,auth.authResponseChange
和auth.statuschange
在用户未登录时不会触发。当用户加载页面时,用户以“未知”开始州;但是一旦用status:true
调用FB.init,Facebook就会检查登录状态并确定用户没有登录 - 这也称为“未知”状态!从技术上讲,状态没有变化,因此不会调用状态变化回调。 (这似乎是一个主要的API设计缺陷,特别是因为他们自己的示例代码不起作用。)
一个解决方案是该问题的可接受答案,是将FB.init
中的状态检查设置为false,而是使用FB.getLoginStatus()
手动检查状态,以便我们自行回复。如果用户未登录,则订阅登录事件。如果用户已经登录,那很好。
这是我正在使用的代码。 login()
和logout()
是我定义的函数,用于为登录状态和未登录状态呈现页面部分。 logout()
显示登录按钮,login()
显示用户特定信息。
FB.init({
appId: '...',
channelUrl: window.location.protocol + '//' + window.location.host + '/channel.html',
status: false,
[... other settings ...]
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
login();
} else {
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
login();
});
logout();
}
});
如果用户最初没有登录,请注意window.location.reload();
,然后自行登录。这是为了避免与您遇到的问题类似Blocked a frame...
错误,在我的情况下是{ {1}} vs http://
不匹配。看看你是否需要它。如果您仍然遇到https://
错误,则可能是由于您在Facebook中配置应用设置的方式。尝试将Blocked a frame...
作为“使用Facebook登录的网站”下的网站网址,并访问该网站以查看您的应用。