我在我的网站上按照railscast添加“登录Facebook”功能,登录没有问题。但是当尝试注销时,似乎FB.getLoginStatus即使在我在Facebook开发者应用程序设置中禁用沙盒模式时也没有起火(如其他一些讨论所示):
(function() {
jQuery(function() {
$('body').prepend('<div id="fb-root"></div>');
return $.ajax({
url: "" + window.location.protocol + "//connect.facebook.net/en_US/all.js",
dataType: 'script',
cache: true
});
});
window.fbAsyncInit = function() {
FB.init({
appId: 'xxxxxomittedxxxxx',
cookie: true,
status: true,
xfbml: true,
oauth: true
});
$('#sign_in').click(function(e) {
e.preventDefault();
return FB.login(function(response) {
if (response.authResponse) {
return window.location = '/auth/facebook/callback';
}
});
});
return $('#sign_out').click(function(e) {
FB.getLoginStatus(function(response) {
if (response.authResponse) {
return FB.logout(response.authResponse);
}
});
return true;
});
};
}).call(this);
我知道FB.getLoginStatus永远不会进入(或不起作用)的原因是我用以下内容替换正文:
FB.getLoginStatus(function(response) {
return alert("I am here!");
});
当“sign_out”点击时,我无法看到我的警报。
我正在运行具有相同行为的Chrome和Firefox。有人可以帮助发现我错过了什么吗?非常感谢。
让我更具体地描述我遇到的“行为”:
我希望我的网站能够表现:“当从mysite.com/users/n退出并再次从mysite.com登录Facebook时,Facebook登录窗口将弹出”
任何人都可以提供帮助吗?非常感谢。
修改
进一步调查发现“根”原因可能仍然是:退出是在路线中的不同路线(或页面)下,而FB.getLoginStatus只是不能在mysite.com/signout下触发。来自firebug的错误消息表明“应用程序配置不允许使用给定URL:应用程序的设置不允许使用一个或多个给定的URL。它必须与网站URL或Canvas URL匹配,或者域必须是应用程序域之一的子域名。“
为了证明这是路线问题,我在同一路线(页面)中放置一个退出链接作为路线,这是根据“Facebook登录网站”中指定的根路线mysite.com,一切正常可以按预期退出:
<%= link_to "sign out facebook", "#" , id: "sign_out" %>
顺便修改sign_out js以摆脱FB.logout(response.authResponse)未捕获[object Object]错误,因为FB.logout期望函数作为参数:
return $('#sign_out').click(function(e) {
FB.getLoginStatus(function(response) {
if (response.authResponse) {
FB.logout();
}
}, true);
});
};
所以,底线:FB.getLoginStatus可能仍然有一个错误,它无法处理来自不同于路由的路由的呼叫。 (我使用Chrome,Firefox和Safari进行了测试,并且所有行为都相同但不适用于IE10。不管怎样,IE10甚至可以在不同的路线注销。)
有类似问题的人的任何评论?请指教。非常感谢你提前。
答案 0 :(得分:0)
尝试将true作为第二个参数添加到getLoginStatus,如FB dev doc中所述:
FB.getLoginStatus(function(response) {
// this will be called when the roundtrip to Facebook has completed
}, true);
这应该避免缓存。 另一种选择是订阅事件:
FB.Event.subscribe('auth.login', function(response) {
// do something with response
});
全部来自here
如果您有疑问,请发表评论。
修改强>
我稍微修改了你的脚本,删除了不需要的代码部分。你有太多不需要的回报。我尝试在这个修改过的脚本中注销,它可以根据需要运行。 订阅活动仅供查阅。
<head>
<title>Exam entry</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
<div id="fb-root"></div>
<input type="button" value="Sign in" id="sign_in" />
<input type="button" value="Sign out" id="sign_out" />
<script type="text/javascript">
window.fbAsyncInit = function () {
FB.init({
appId: '586844044669652',
cookie: true,
status: true,
xfbml: true
});
// Here we subscribe to the auth.authResponseChange JavaScript event. This event is fired
// for any authentication related change, such as login, logout or session refresh. This means that
// whenever someone who was previously logged out tries to log in again, the correct case below
// will be handled.
FB.Event.subscribe('auth.authResponseChange', function (response) {
// Here we specify what we do with the response anytime this event occurs.
if (response.status === 'connected') {
// The response object is returned with a status field that lets the app know the current
// login status of the person. In this case, we're handling the situation where they
// have logged in to the app.
testAPI();
} /*else if (response.status === 'not_authorized') {
// In this case, the person is logged into Facebook, but not into the app, so we call
// FB.login() to prompt them to do so.
// In real-life usage, you wouldn't want to immediately prompt someone to login
// like this, for two reasons:
// (1) JavaScript created popup windows are blocked by most browsers unless they
// result from direct interaction from people using the app (such as a mouse click)
// (2) it is a bad experience to be continually prompted to login upon page load.
FB.login();
} else {
// In this case, the person is not logged into Facebook, so we call the login()
// function to prompt them to do so. Note that at this stage there is no indication
// of whether they are logged into the app. If they aren't then they'll see the Login
// dialog right after they log in to Facebook.
// The same caveats as above apply to the FB.login() call here.
FB.login();
}*/
});
$('#sign_in').click(function (e) {
e.preventDefault();
FB.login(function (response) {
if (response.authResponse) {
//return window.location = '/auth/facebook/callback';
}
});
});
$('#sign_out').click(function (e) {
FB.logout(function (response) {
console.log("Here logout response", response);
});
});
};
// Here we run a very simple test of the Graph API after login is successful.
// This testAPI() function is only called in those cases.
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
console.log('Good to see you, ' + response.name + '.');
});
}
// Load the SDK asynchronously
(function (d) {
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) { return; }
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
</body>