Javascript 如何检测用户在登录到我的应用程序后从Facebook注销?
我的应用程序使用Facebook身份验证:Javascript 如何检测用户在登录到我的应用程序后从Facebook注销?,javascript,facebook,facebook-ui,Javascript,Facebook,Facebook Ui,我的应用程序使用Facebook身份验证: FB.init({ appId: config.fbAppId, status: true, cookie: true, // xfbml: true, // channelURL : 'http://WWW.MYDOMAIN.COM/channel.html', // TODO oauth : true }); // later... FB.login(function(response) { c
FB.init({
appId: config.fbAppId,
status: true,
cookie: true,
// xfbml: true,
// channelURL : 'http://WWW.MYDOMAIN.COM/channel.html', // TODO
oauth : true
});
// later...
FB.login(function(response)
{
console.log(response);
console.log("authId: " + response.authResponse.userID);
gameSwf.setLoginFacebook(response.authResponse.accessToken);
}, {scope:'email,publish_actions,read_friendlists'});
使用它时,人们可以在墙上张贴:
var obj = {
method: 'feed',
link: linkUrl,
picture: pictureUrl,
name: title,
caption: "",
description: message
};
function callback(response) {
// console.log("Post on wall: " + response);
}
FB.ui(obj, callback);
这很好,但有一个小问题。如果人们:
我可以让Facebook向用户显示登录提示吗。或者我可以检测到错误并告诉用户他不再登录Facebook吗?您可以尝试使用的是FB.getLoginStatus,如果用户已连接,这将允许他们完成墙柱。 如果他们没有连接,那么在他们可以在墙上张贴之前调用FB.login方法
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.
}
});
还有一些用于登录和注销的事件,您可以对这些响应进行监视和处理
FB.Event.subscribe('auth.login', function(response) {
// do something with response
});
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
});
回想一下,getLoginStatus只是强迫往返于Facebook。查看以下代码:
FB.getLoginStatus(function(response) {
// some code
}, true);
查看最后一个参数设置为true,以强制往返
来自JS SDK文档:
为了提高应用程序的性能,不是每次调用
检查用户的状态将导致请求Facebook的
服务器。在可能的情况下,将缓存响应。第一次
调用FB.getLoginStatus的当前浏览器会话,或JS
SDK已初始化,状态为:true,响应对象将由缓存
SDK。对FB.getLoginStatus的后续调用将从
这是缓存的响应
这可能会导致用户登录(或退出)时出现问题
自上次完整会话查找后的Facebook,或者如果用户
在他们的帐户设置中删除了你的应用程序
要解决这个问题,您可以使用第二个
参数设置为true以强制往返到Facebook-有效
刷新响应对象的缓存。
()
这似乎很有希望,但在注销
getLoginStatus
后仍然返回connected。我还订阅了auth.logout
和auth.statusChange
,但当我注销或尝试制作墙贴时,都不会被炒鱿鱼。奇怪的是,它们怎么不会被炒鱿鱼。只要FB.Event.subscribe方法包含在$(document).ready(function())中,它们就应该激发.$(document).ready(function(){FB.Event.subscribe('auth.login',function(response){console.log(auth.login”);};});当然,我假设它们不包含在$(document).ready中(function(),但通过测试,这些事件似乎总是与其他事件一起触发。