Javascript FB.logout() 在没有访问令牌的情况下调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8430474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FB.logout() called without an access token
提问by martinthebeardy
I'm trying to log out of a website i've created with Facebook integrated. Logging in works fine, but when I want to log out Firebug consistently gives me this error:
我正在尝试退出我创建的与 Facebook 集成的网站。登录工作正常,但是当我想注销 Firebug 时,始终会出现此错误:
FB.logout()
called without an access token.
FB.logout()
在没有访问令牌的情况下调用。
I'm using the Facebook JavaScript SDK, and the code I've got to logout looks like this:
我正在使用 Facebook JavaScript SDK,我必须注销的代码如下所示:
$(document).ready($(function () {
$("#fblogout").click(facebooklogout);
}));
function facebooklogout() {
FB.logout(function (response) {
}
)};
This is the logout code specified at the Facebook Developers Documentationjust with a button being assigned the method on document.ready
这是Facebook 开发人员文档中指定的注销代码,只是一个按钮被分配了 document.ready 上的方法
Before this code I have the FB.init()
method, that all runs fine.
在此代码之前,我有FB.init()
方法,一切正常。
If anyone's got a solution as to why FB.logout doesn't have an access token, it'd be appreciated.
如果有人有关于为什么 FB.logout 没有访问令牌的解决方案,我们将不胜感激。
采纳答案by Balaji Birajdar
To logout from the application which uses facebook graph API, use this JavaScript on the logout page just after the <form>
tag:
要从使用 facebook 图形 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);
}
The code works and is live on my site.
该代码有效并在我的网站上运行。
回答by Ozzy
I went for the less trivial solution:
我选择了不那么琐碎的解决方案:
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>');
});
});
}
});
}
回答by Marcelo Cintra de Melo
I've tried something like this:
我试过这样的事情:
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;
}
回答by Eric Novins
Should be something more like this. There was a change to the JS API where you have to use authResponse instead of just session.
应该更像这样。JS API 发生了变化,您必须使用 authResponse 而不仅仅是会话。
//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);
}
回答by Mihai Cr?i??
The error says that you don't have an access token, you have to check for one using the FB.getAccessToken() function.
该错误表示您没有访问令牌,您必须使用 FB.getAccessToken() 函数检查一个。
If there is no access token the function returns null. See example below:
如果没有访问令牌,则该函数返回 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);
}
}
回答by Sumit Badsara
Figured it out after so many tries.
Actually, we are passing response
to FB.logout.
试了好多次才搞定。实际上,我们正在传递response
给 FB.logout。
Generally response.authResponse.accessToken
contains token. So, its error about the accessToken not being there.
一般response.authResponse.accessToken
包含token。因此,它关于 accessToken 不存在的错误。
Think logically, where does that response come from in your code? Out of nowhere.
合乎逻辑地思考,该响应来自您的代码中的哪里?从哪儿冒出来。
So, we need to get that response object from a function and pass that to logout function. I don't know how it worked for others, but this worked for me.
因此,我们需要从函数中获取该响应对象并将其传递给注销函数。我不知道它对其他人如何,但这对我有用。
Just replace the code with this
只需用这个替换代码
function logout(){
FB.getLoginStatus(function(response) {
FB.logout(function(response){
console.log("Logged Out!");
window.location = "/";
});
});
}
What we do here is, get the login status if the user is logged in and get the corresponding response in return, which contains all the necessary tokens and data. And, then we pass this response to logout function.
我们在这里做的是,如果用户已登录,则获取登录状态并返回相应的响应,其中包含所有必要的令牌和数据。然后,我们将此响应传递给注销函数。