没有访问令牌而调用FB.logout()
FB.logout() called without an access token
我想退出一个网站,我已经与Facebook集成创建。登录工作正常,但当我想注销Firebug总是给我这个错误:
没有访问令牌的FB.logout()
被调用。
我使用的是Facebook JavaScript SDK,我要注销的代码看起来像这样:
$(document).ready($(function () {
$("#fblogout").click(facebooklogout);
}));
function facebooklogout() {
FB.logout(function (response) {
}
)};
这是在Facebook开发者文档中指定的注销代码,只是给一个按钮分配了document.ready
上的方法在此代码之前,我有FB.init()
方法,这一切都运行良好。
如果有人有一个解决方案,为什么FB。注销没有访问令牌,非常感谢。
要从使用facebook graph API的应用程序注销,请在<form>
标签之后的注销页面上使用此JavaScript:
window.onload=function()
{
// initialize the library with your Facebook API key
FB.init({ apiKey: 'b65c1efa72f570xxxxxxxxxxxxxxxxx' });
//Fetch the status so that we can log out.
//You must have the login status before you can logout,
//and if you authenticated via oAuth (server side), this is necessary.
//If you logged in via the JavaScript SDK, you can simply call FB.logout()
//once the login status is fetched, call handleSessionResponse
FB.getLoginStatus(handleSessionResponse);
}
//handle a session response from any of the auth related calls
function handleSessionResponse(response) {
//if we dont have a session (which means the user has been logged out, redirect the user)
if (!response.session) {
window.location = "/mysite/Login.aspx";
return;
}
//if we do have a non-null response.session, call FB.logout(),
//the JS method will log the user out of Facebook and remove any authorization cookies
FB.logout(handleSessionResponse);
}
代码工作,并在我的网站上。
我选择了一个不那么简单的解决方案:
function facebookLogout(){
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
FB.logout(function(response) {
// this part just clears the $_SESSION var
// replace with your own code
$.post("/logout").done(function() {
$('#status').html('<p>Logged out.</p>');
});
});
}
});
}
试了这么多次才明白。
一般response.authResponse.accessToken
包含token。所以,它的错误是关于accessToken不在那里。
从逻辑上思考,这个响应在你的代码中来自哪里?不知从哪里冒出来的
我们需要从一个函数中获取那个响应对象并让它工作。我不知道别人是怎么做的,但这对我有用。
只需将代码替换为
function logout(){
FB.getLoginStatus(function(response) {
FB.logout(function(response){
console.log("Logged Out!");
window.location = "/";
});
});
}
我们在这里所做的是,如果用户已登录,则获取登录状态,并获得相应的响应作为返回,其中包含所有必要的令牌和数据。一旦获取到这个,就使用令牌注销用户。
我试过这样做:
function fbLogout(){
if(typeof FB.logout == 'function'){
if (FB.getAuthResponse()) {
FB.logout(function(response) { window.location.href = PROJECT_PATH + '/index/logout'; });
return;
}
};
window.location.href = PROJECT_PATH + '/index/logout';
return;
}
应该更像这样。JS API有一个变化,你必须使用authResponse而不仅仅是session。
//handle a session response from any of the auth related calls
function handleSessionResponse(response) {
//if we dont have a session (which means the user has been logged out, redirect the user)
if (!response.authResponse) {
return;
}
//if we do have a non-null response.session, call FB.logout(),
//the JS method will log the user out of Facebook and remove any authorization cookies
FB.logout(response.authResponse);
}
错误说明您没有访问令牌,您必须使用FB.getAccessToken()函数检查是否有访问令牌
如果没有访问令牌,函数返回null。请看下面的例子:
function facebooklogout() {
try {
if (FB.getAccessToken() != null) {
FB.logout(function(response) {
// user is now logged out from facebook do your post request or just redirect
window.location.replace(href);
});
} else {
// user is not logged in with facebook, maybe with something else
window.location.replace(href);
}
} catch (err) {
// any errors just logout
window.location.replace(href);
}
}
在Typescript中,这个函数可以很好地工作。
signOutFacebook(): void {
/*SIGN OUT USER FACEBOOK.*/
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
FB.logout(function (response) {
console.log("Logged Out!");
});
} else {
console.log("The person is not logged into your webpage or we are unable to tell. !");
}
});
}/*FINAL 'signOutFacebook()'. */
与FB一起使用访问令牌。方法注销时,您需要确保在登录过程中获得了访问令牌,并且在您想要注销用户时它可用。
FB.logout(function(response) {
if ( 'connected' === response.status ) {
window.location.href = '/';
} else {
console.log( 'There has been an error.' );
}
}, {
access_token: accessToken
});
- issue with FB.Event.subscribe
- FB.login访问令牌facebook javascript SDK
- 使用FB.ui的Facebook Feed对话给出API错误191
- Facebook FB.init”;访问被拒绝”;在Internet Explorer 11中
- 为什么是FB.注销用户功能不会重定向到我告诉它的 URL
- FB.login Invalid redirect_uri (xd_proxy.php)
- 类似FB的按钮显示在页面上,但不会在同一页面上弹出
- Problems with FB.api
- 使用FB.api在Facebook上发布消息
- 浏览器窗口中的应用内FB页面插件链接
- 对auth.logout的Facebook事件订阅仅选择性地将用户重定向到url
- Facebook Javascript fb.ui活动将照片发布在朋友墙上
- FB.login有效,但弹出窗口不起作用;t关闭
- FB未定义为javascript
- FB.Event.subscribe auth.statusChange未在Angular JS中启动
- FB.logout() - 无访问令牌 - “以其他用户身份登录”
- Phonegap fabebook SDK - FB.logout not working
- 等待onclick事件中的FB.logout()
- 没有访问令牌而调用FB.logout()
- 页面打开时运行FB.logout()