FB.getLoginStatus返回状态未知

Die*_*yol 19 javascript facebook facebook-javascript-sdk

使用有效的Facebook App调用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>
Run Code Online (Sandbox Code Playgroud)


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


这是Facebook App Settings的屏幕截图. Facebook应用程序设置

Fli*_*mzy 16

我在Chrome中发生这种情况,原因是Chrome配置为阻止第三方Cookie和数据.

一旦我进行了配置更改,FaceBook就能够毫无问题地将我登录到我的应用程序中.

  • Chrome设置
  • 显示高级设置...
  • 隐私
  • 内容设置...
  • 取消选中阻止第三方Cookie和网站数据

  • 您如何知道代码的最终用户不会遇到同样的问题?你会建议使用你的应用程序的每个人取消选中该框吗? (18认同)
  • 我没有检查它,但我仍然遇到同样的问题 (2认同)

Nag*_*ani 5

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

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

造成此问题的根本原因是,在FB.logout()Chrome 上,它并未删除cookie fblo_<your-app-id>,而cookie 却以某种方式影响了FB.getLoginStatus()函数的返回unknown

修复:在调用时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;';
}
Run Code Online (Sandbox Code Playgroud)


Sah*_*tal 4

当我检查时,状态已显示"not_authorized",这很好,因为我尚未授权该应用程序。

要完成该流程,您应该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!
      }
   });
}
Run Code Online (Sandbox Code Playgroud)

  • 你的代码和我的一样。您可以发现它在[此处](http://media.tuentifotos.com/sahil_mittal_test.html)运行,返回未知的response.status。 (2认同)