我正在尝试在我的网站上实现FB JS SDK,既可用于登录,也可用于共享/喜欢内容。
这是我的代码:
// called by FB SDK once loaded (all.js)
window.fbAsyncInit = function () {
FB.init({
appId : '913550365396947',
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse social plugins on this page
version: 'v2.5',
status: false // don't check login status
});
FB.getLoginStatus(function (response) {
console.log('FB.getLoginStatus callback response', response);
});
// auth.authResponseChange event is fired for any authentication related change, such as login, logout or session refresh
FB.Event.subscribe('auth.authResponseChange', function (response) {
console.log('auth.authResponseChange callback response: ', response);
// Here we specify what we do with the response anytime this event occurs.
if (response.status === 'connected') {
if (!shopWise.loggedIn) { // if the user isn't logged in to PW, but they are logged in to facebook
console.warn('Logged in with facebook, but not logged in to shopwise. Reloading the page..');
location.reload();
}
} else if (response.status === 'not_authorized') {
} else {
}
});
};
我遇到的问题是auth.authResponseChange
回调正在每个页面加载时执行,而(如果我的理解是正确的)它应该只在用户的登录状态实际发生变化时执行(例如记录在,或退出)。
因为每次加载页面时都会调用该事件,所以我的页面刷新代码会在每次页面加载时执行,这意味着页面处于无限刷新循环中。
奇怪的是,如果我删除FB.getLoginStatus()
来电,则不会触发auth.authResponseChange
回调。
我在这里做错了什么?为什么调用FB.getLoginStatus()
会导致auth.authResponseChange
回调被触发?