javascript FB.getLoginStatus 返回状态未知

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

FB.getLoginStatus returns status unknown

javascriptfacebookfacebook-javascript-sdk

提问by Diego Cerdan Puyol

When calling FB.getLoginStatus using a valid Facebook App the response status is always unknown. Exact response is {authResponse: undefined, status: "unknown"}.

使用有效的 Facebook 应用程序调用 FB.getLoginStatus 时,响应状态始终未知。确切的响应是 {authResponse: undefined, status: "unknown"}。

<html>
<body>
    <div id="fb-root"></div>

    <script>
    window.fbAsyncInit = function(){
        FB.init({ appId:'1484134468466778', status:true,  cookie:true, xfbml:true});
        FB.getLoginStatus(function(response){
        console.log(response);
        });
    };

    (function(d){
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
    }(document));
    </script>
</body>
</html>



Example URL: http://media.tuentifotos.com/simple_test.html

示例网址:http: //media.tuentifotos.com/simple_test.html



Here a screenshot of the Facebook App Settings. Facebook App Settings

这是 Facebook 应用设置的屏幕截图。 Facebook 应用程序设置

回答by Flimzy

This was happening for me in Chrome, and was because Chrome was configured to block third-party cookies and data.

这在 Chrome 中发生在我身上,是因为 Chrome 被配置为阻止第三方 cookie 和数据。

Once I made that configuration change, FaceBook is able to log me into my app without a problem.

一旦我进行了配置更改,FaceBook 就可以毫无问题地让我登录我的应用程序。

  • Chrome Settings
  • Show advanced settings...
  • Privacy
  • Content settings...
  • uncheck Block third-party cookies and site data
  • 铬设置
  • 显示高级设置...
  • 隐私
  • 内容设置...
  • 取消选中阻止第三方 cookie 和站点数据

回答by Nagesh Andani

I too faced this problem in Chrome. However, in Firefox it worked as expected with the status returned as connectedwhen the user had logged in previously.

我在 Chrome 中也遇到了这个问题。但是,在 Firefox 中,它按预期工作,返回的状态connected与用户之前登录时的状态相同。

I found a clue about this from an answer to the similar question here.

我从这里对类似问题的回答中找到了一条线索。

The root cause of this issue is, on FB.logout(), Chrome is not removing the cookie fblo_<your-app-id>which is somehow affecting FB.getLoginStatus()function to return unknown

此问题的根本原因是,FB.logout()Chrome 没有删除fblo_<your-app-id>以某种方式影响FB.getLoginStatus()函数返回的 cookieunknown

Fix: On calling FB.logout(), you may programmatically delete the cookie fblo_<your-app-id>

修复:调用时FB.logout(),您可以通过编程方式删除 cookiefblo_<your-app-id>

FB.logout(function(response) {
  deleteCookie("fblo_" + fbAppId); // fblo_yourFBAppId. example: fblo_444499089231295
});

function deleteCookie(name) {
  document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

回答by rogerdpack

For me this meant "on my login page" I needed to specify cookies.

对我来说,这意味着“在我的登录页面上”我需要指定 cookie。

  window.fbAsyncInit = function() {
   FB.init({
      version: 'v2.8',
      cookie     : true,
    });
  };

But don't ask me whythis was the case. It also fixed it needing to click twice on the login button to actually login, and doesn't even require an appId, seemingly, FWIW...

但不要问我为什么会这样。它还修复了需要在登录按钮上单击两次才能实际登录的问题,甚至不需要 appId,看起来,FWIW ......

回答by Sahil Mittal

When I checked, the status is showing "not_authorized"and that's fine, since I've not authorized the app yet.

当我检查时,状态正在显示"not_authorized",这很好,因为我还没有授权该应用程序。

To complete the flow, you should add the FB.loginwhenever user id is not authorized or not logged-in to facebook:

要完成流程,您应该FB.login在用户 ID 未获授权或未登录到 facebook 时添加:

window.fbAsyncInit = function(){
    FB.init({ appId:'{APP-ID}', status:true,  cookie:true, xfbml:true});
    FB.getLoginStatus(function(response){
        if (response.status === 'connected') {
           //proceed
        } else if (response.status === 'not_authorized') {
           login();
        } else {
          login();
        }
    });
};

function login(){
   FB.login(function(response) {
      if (response.authResponse) {
         // proceed
      } else {
         // not auth / cancelled the login!
      }
   });
}

回答by Simon_Weaver

The final answer

最终答案

Ok so I think I've finally figured this damn issue out.

好吧,我想我终于解决了这个该死的问题。

What you need to know:

你需要知道的:

1) You authenticate with Facebook via an app ID. This sets various cookies, where your app ID is tagged on the end:

1) 您通过应用 ID 向 Facebook 进行身份验证。这会设置各种 cookie,最后标记您的应用 ID:

  • fblo_000000000000
  • fbm_000000000000
  • fbsr_000000000000
  • fblo_000000000000
  • fbm_000000000000
  • fbsr_000000000000

2) If you delete these cookies, you're still authenticated to facebookas a regular user (unless you log out completely). And on Facebook's servers they still know you are authorized on this app.

2) 如果您删除这些 cookie,您仍然可以作为普通用户向 facebook 进行身份验证(除非您完全退出)。在 Facebook 的服务器上,他们仍然知道您在此应用程序上已获得授权

  • So when you run FB.getLoginStatus()again it will just recreate them and put them back again. This is not what your user expects. This is bad. They clicked 'Log out'.
  • 因此,当您FB.getLoginStatus()再次运行时,它只会重新创建它们并将它们放回原处。这不是您的用户所期望的。这不好。他们点击了“退出”。

3) The docs explicitly say this:

3)文档明确指出

The user is either not logged into Facebook or explicitly logged out of your application so it doesn't attempt to connect to Facebook and thus, we don't know if they've authenticated your application or not. (unknown)

用户要么未登录 Facebook,要么未明确退出您的应用程序,因此它不会尝试连接到 Facebook,因此,我们不知道他们是否已对您的应用程序进行了身份验证。(未知)

So they don't even TRY to check if this cookie is set. That's why you get null or undefined. So the fblocookie is considered like an 'opt-out'. You're NOT ACTUALLY LOGGED OUT by what any regular person would consider being logged out as to mean. There's just a cookie saying you are!Of course if the user logs back in with the login button then the cookie will be deleted, which is what you and your user wants.

所以他们甚至不尝试检查是否设置了这个 cookie。这就是为什么你得到 null 或 undefined 的原因。因此,fblocookie 被视为“选择退出”。你实际上并没有被任何普通人认为被注销的意思所注销。只有一个饼干说你是!当然,如果用户使用登录按钮重新登录,则 cookie 将被删除,这正是您和您的用户想要的。

Therefore I believe the only thing that makes sense to do (if you truly need to know the user's status) is to:

因此我相信唯一有意义的事情(如果你真的需要知道用户的状态)是:

  • Manually check the existance of fblo_<APPID>cookie before you run FB.getLoginStatus.
  • If the cookie doesn't exist then do nothing and run your normal flow.
  • If the cookie does exist you have several options :
  • fblo_<APPID>在运行之前手动检查cookie的存在FB.getLoginStatus
  • 如果 cookie 不存在,那么什么都不做并运行您的正常流程。
  • 如果 cookie 确实存在,您有多种选择:

1) Option 1

1) 选项 1

Do absolutely nothing. You now understand the issue, you understand not to delete the cookie and perhaps you don't need to do anything other than show the Facebook login button.

绝对什么都不做。您现在明白了这个问题,您明白不要删除 cookie,也许除了显示 Facebook 登录按钮之外,您不需要做任何事情。

2) Option 2

2) 选项 2

You assume the person is a user of your app, and do whatever you need to do to show the UI. But you won't be running your full normal logic - this will be application specific to you .

您假设此人是您的应用程序的用户,并执行您需要执行的任何操作来显示 UI。但是您不会运行完整的正常逻辑 - 这将是特定于您的应用程序。

3) Option 3

3) 选项 3

  • Manually set the cookie value fblo_<APPID>to n(instead of 'y'). Remember this cookie is set on your domain so you're allowed to change it. *Depending upon your development environment this will vary, but it needs to be done client side, and you may need to specify path of `/' and your cookie domain).
  • Run the getLoginStatus(..., true)- it will not be blocked now because the cookie is now n. However you must not run your normal logic here- because all you want to do is check if the user is actually a Facebook user and/or still authenticated with your app.
  • Manually set the cookie value back to y
  • 手动将 cookie 值设置fblo_<APPID>n(而不是“y”)。请记住,此 cookie 是在您的域中设置的,因此您可以更改它。*根据您的开发环境,这会有所不同,但需要在客户端完成,您可能需要指定`/'的路径和您的cookie域)。
  • 运行getLoginStatus(..., true)- 它现在不会被阻止,因为 cookie 现在是n。但是你不能在这里运行你的正常逻辑- 因为你想要做的就是检查用户是否真的是 Facebook 用户和/或仍然通过你的应用程序进行身份验证。
  • 手动将 cookie 值设置回 y

Unfortunately I can't recommend this solution due to weird race conditions.It almost almost works, but FB could deliberately or accidentally break it at any time - and it actually ends up still getting confused thinking you're really logged in when you aren't. Plus it could have all kinds of complications with other plugins I haven't even observed.

不幸的是,由于奇怪的竞争条件,我不能推荐这个解决方案。它几乎可以正常工作,但 FB 随时可能有意或无意地破坏它 - 实际上最终仍然会感到困惑,认为您真的登录了,而实际上却没有登录。此外,它可能会与我什至没有观察到的其他插件一起出现各种复杂情况。

I wish the getLoginStatuswould just always run and give accurate information, but this is the only true solution I see. And it's been this way for 8 years so I think we're stuck with it.

我希望getLoginStatus它总是运行并提供准确的信息,但这是我看到的唯一真正的解决方案。8 年来一直如此,所以我认为我们一直坚持下去。

4) Option 4 - Set your own cookie

4) 选项 4 - 设置您自己的 cookie

  • You always have the option to set your own cookies to indicate certain things, like 'this user has at some point logged into Facebook' or 'this user is authenticated with my app'. But I'm cautious about doing this because it can quickly get complicated.
  • 您始终可以选择设置自己的 cookie 以指示某些内容,例如“此用户已在某个时间登录 Facebook”或“此用户已通过我的应用程序进行身份验证”。但我对这样做很谨慎,因为它很快就会变得复杂。

Incidentally I revisited this issue because I'm using the Messenger plugin for my website and I don't want it to display at all unless I know the person has a Facebook account. So I need an accurate response to login status.

顺便说一下,我重新讨论了这个问题,因为我在我的网站上使用了 Messenger 插件,我根本不希望它显示,除非我知道这个人有一个 Facebook 帐户。所以我需要对登录状态的准确响应。

回答by Emmanuel David

This was happening to me until I turn on my browser to allow third-party websites to save and read cookies.

这种情况一直发生在我身上,直到我打开浏览器以允许第三方网站保存和读取 cookie。

To do this go to

要做到这一点去

settings > advanced > site settings > Cookies and site data

Then uncheck the option blocking third-party websites from saving and reading cookies. This might not be a good option but it solves these issues for me. image

然后取消选中阻止第三方网站保存和读取 cookie 的选项。这可能不是一个好的选择,但它为我解决了这些问题。 图片

回答by magento4u_com

You have to test it from the live domain (which you provided in the app).

您必须从实时域(您在应用程序中提供)中对其进行测试。

I had the same problem when testing locally (using local domain).

我在本地测试时遇到了同样的问题(使用本地域)。

回答by Dennis Calla

I had the same issue in IE. Flimzy's answer got me thinking. I tried running IE as administrator and it worked.

我在 IE 中遇到了同样的问题。Flimzy 的回答让我思考。我尝试以管理员身份运行 IE,它奏效了。

回答by divyanshu

I had the same problem, I fixed this by clearing all cache and cookies.

我遇到了同样的问题,我通过清除所有缓存和 cookie 解决了这个问题。