mar*_*rdy 33 javascript facebook facebook-javascript-sdk
我正试图退出我用Facebook集成创建的网站.登录工作正常,但是当我想要注销时,Firebug一直给我这个错误:
FB.logout() 没有访问令牌调用.
我正在使用Facebook JavaScript SDK,我必须注销的代码如下所示:
$(document).ready($(function () {
$("#fblogout").click(facebooklogout);
}));
function facebooklogout() {
FB.logout(function (response) {
}
)};
Run Code Online (Sandbox Code Playgroud)
这是在Facebook开发人员文档中指定的注销代码,只是在document.ready上分配了一个按钮
在此代码之前我有FB.init()方法,一切运行正常.
如果有人知道为什么FB.logout没有访问令牌,那么我们将不胜感激.
Bal*_*dar 19
要从使用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);
}
Run Code Online (Sandbox Code Playgroud)
代码可以在我的网站上运行.
Ozz*_*zzy 12
我选择了不那么简单的解决方案:
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>');
});
});
}
});
}
Run Code Online (Sandbox Code Playgroud)
试了好多次才搞定。
一般response.authResponse.accessToken包含token。因此,它关于 accessToken 不存在的错误。
合乎逻辑地思考,该响应来自您的代码中的哪里?从哪儿冒出来。
因此,我们需要从函数中获取该响应对象并使其正常工作。我不知道它对其他人如何,但这对我有用。
只需用这个替换代码
function logout(){
FB.getLoginStatus(function(response) {
FB.logout(function(response){
console.log("Logged Out!");
window.location = "/";
});
});
}
Run Code Online (Sandbox Code Playgroud)
我们在这里做的是,如果用户已登录,则获取登录状态并返回相应的响应,其中包含所有必要的令牌和数据。获取此信息后,该令牌将用于注销用户。