javascript Facebook JS API 无法注销

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22331373/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:56:41  来源:igfitidea点击:

Facebook JS API can't logout

javascriptfacebookoauth-2.0facebook-javascript-sdk

提问by Enthusiast

I have a localhostwebsite and script with FB.logout(). After some actions, it fails to log me out and I see next error message in console:

我有一个localhost网站和脚本FB.logout()。执行一些操作后,它无法将我注销,我在控制台中看到下一条错误消息:

Refused to display 'https://www.facebook.com/home.php' in a frame because it set 'X-Frame-  Options' to 'DENY'.

I searched all StackOverflow and didn't find any working solution. Network inspector shows cancelled near home.php query.
So I understand that the script tries to load the Facebook homepage in a frame, but it can't because it is prohibited. So how can I fix it? Why doesn't logout()work for me?

我搜索了所有 StackOverflow 并没有找到任何可行的解决方案。网络检查器显示取消了 home.php 附近的查询。
所以我知道脚本试图在框架中加载 Facebook 主页,但它不能,因为它被禁止。那么我该如何解决呢?为什么logout()对我不起作用?

My code

我的代码

// Facebook Basic Example

window.fbAsyncInit = function() {
  FB.init({
    appId      : '579651098766258',
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });

  FB.Event.subscribe('auth.authResponseChange', function(response) {
    if (response.status === 'connected') {
      testAPI();
    } else if (response.status === 'not_authorized') {
      FB.login();
    } else {
      FB.login();
    }
  });
};

(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));



function testAPI() {
  FB.api('/me', function(res) {
    console.log(res);
    setTimeout(function(){
      FB.logout(); // <-- ERROR
    }, 2000);
  });
}

回答by SoldierCorp

This solution worked for me:

这个解决方案对我有用:

  1. Go to Settings tab in your facebook application and in Basic tab scrolldown to +Add Platform
  2. Click on +Add Platform and choose Website
  3. Add your url of your website (for me, was the same url added on Advanced tab on Redirect URIs)
  4. Save changes and test again.
  1. 转到 Facebook 应用程序中的“设置”选项卡,然后在“基本”选项卡中向下滚动到“+添加平台”
  2. 点击+添加平台并选择网站
  3. 添加您网站的网址(对我来说,与重定向 URI 的“高级”选项卡上添加的网址相同)
  4. 保存更改并再次测试。

Also I made a youtube video to solve this problem: https://www.youtube.com/watch?v=7CNpLgwa0-c

我还制作了一个 youtube 视频来解决这个问题:https: //www.youtube.com/watch?v=7CNpLgwa0-c

enter image description here

在此处输入图片说明

回答by vivanov

I had to set the "App Domains" field in the Settings tab within the Facebook's App control panel in order for it to work:

我必须在 Facebook 的应用程序控制面板的“设置”选项卡中设置“应用程序域”字段才能使其正常工作:

enter image description here

在此处输入图片说明

回答by Hymany

Update by 2019 May 2nd:

2019 年 5 月 2 日更新:

1) The https now is enforced, so you must use https://localhost. http will no longer be available.

1) 现在强制使用https,因此您必须使用https://localhost。http 将不再可用。

2) You need to whitelist your localhost by going to Settings > Basic > Add Plat form and choose "Website"

2)您需要通过转到设置>基本>添加平台表单并选择“网站”将本地主机列入白名单

回答by Shane Davies

Update for 2018

2018 年更新

From the App Dashboard, under the Facebook Login product, click on Quickstart and follow the instructions. The Logout API call should start working!

在 App Dashboard 的 Facebook Login 产品下,单击 Quickstart 并按照说明进行操作。Logout API 调用应该开始工作了!

Note:I selected WWW and entered http://localhost:5757/in the Site URL.

注意:我选择了 WWW 并输入http://localhost:5757/了站点 URL。

回答by Colinch

I'm not sure how your HTML looks like, but for me this worked on my logout button:

我不确定你的 HTML 是什么样子,但对我来说这对我的注销按钮有效:

onclick="javascript:FB.logout(function() { window.location.reload() }); return false;"

I hope this will help someone out, at least this fixed my problem.

我希望这会帮助别人,至少这解决了我的问题。

回答by Milk Man

I have found that with www.example.com I get this error and without the www (example.com) there is no error...

我发现使用www.example.com 时出现此错误,而没有 www (example.com) 则没有错误...

回答by The_PratikshaK

This solution worked for me -

这个解决方案对我有用 -

  1. Go to facebook Login -> quickstart -> www -> set app origin url "https://localhost:4200".
  1. 转到 Facebook 登录 -> 快速入门 -> www -> 设置应用程序来源网址“ https://localhost:4200”。

enter image description here

在此处输入图片说明