自动登录FB.Event.subscribe auth登录不工作

Auto login FB.Event.subscribe auth.login doesn't work

本文关键字:登录 auth subscribe 工作 Event FB      更新时间:2023-09-26

由于某些原因,今天我为现有成员自动登录的代码坏了。通常情况下,这段代码工作完美,在firebug检查后,我发现有一个问题,寻找"auth"。Login '由于某种原因,因此调用window.location.reload();不是工作。(如果我手动刷新,然后成员显示为连接正常,问题只是自动登录之前的会话是需要一个页面刷新)我还想看看问题是否只是"自动"的。Login '并将其替换为'auth '。authResponseChange'当我这样做的时候,页面一直在刷新…所以我觉得问题出在auth身上。登录触发器。有人知道有什么特殊的原因或者facebook做出的任何改变会导致这个问题吗?

*update* BUG解决!这个问题已经被facebook修复了。

这个bug已经被facebook报告并回答了-

https://developers.facebook.com/bugs/524245490930206?browse=search_50f87d9e8869a5e06191882

顺便说一句,这是一个工作代码:

<div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
              appId  : '<?php =$APP_ID ?>',
              status : true, // check login status
              cookie : true, // enable cookies to allow the server to access the session
              xfbml  : true,  // parse XFBML
              oauth  : true
        });
      // Additional initialization code here
        FB.Event.subscribe('auth.login', function(response) {
          window.location.reload();
        });
      };
      // Load the SDK Asynchronously
      (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#xfbml=1";
         ref.parentNode.insertBefore(js, ref);
       }(document));
    </script>

我从auth改变了。登录到认证。statusChange,并且还包装了getLoginStatus中的所有内容,因此它仅在未连接时运行,它似乎工作正常。看起来像这样(里面的窗户)。fbAsyncInit = function() {)

  FB.getLoginStatus(function(response) {
    if (response.status != 'connected') {
      FB.Event.subscribe('auth.statusChange', function(response) {
        if (response.authResponse) {
          // whatever code runs when user logs in
        }
      });
    }
  });

看来我也遇到了同样的问题。

直到几个小时前,当一个用户(之前已经授权并登录过我的应用程序)打开我的网站时,cookie过期了,出现了登录按钮,但几秒钟后(JS SDK触发登录功能的时间),他自动登录,页面没有登录按钮,重新加载了用户信息。

现在,当一个已经注册的用户在cookie过期后打开我的网站时,登录按钮再次出现,但用户不会自动登录,页面也不会像以前那样重新加载。奇怪的是,如果我点击登录按钮,它也不工作!如果我现在手动刷新页面,则登录按钮消失,并显示用户信息,因此似乎用户实际上已登录,但没有捕捉到触发页面重新加载的事件。

如果我手动注销并再次登录(因此使用php sdk),一切工作

我的代码:
<script>
        window.fbAsyncInit = function() {
            FB.init({
                appId: '<?php echo $facebook->getAppID() ?>',
                cookie: true,
                xfbml: true,
                oauth: true
            });
            FB.Event.subscribe('auth.login', function(response) {
                window.location.reload();
            });
            FB.Event.subscribe('auth.logout', function(response) {
                window.location.reload();
                });
        };
        (function() {
            var e = document.createElement('script'); e.async = true;
            e.src = document.location.protocol +
                '//connect.facebook.net/en_US/all.js';
                document.getElementById('fb-root').appendChild(e);
        }());
        function facebookLogin() {
            FB.login(function(response) {
                // handle the response
            }, {scope: 'email, publish_stream, read_friendlists'});
        }
    </script>

UPDATE我添加了这几行到我的js代码:

FB.Event.subscribe('auth.statusChange', function(response) {
                alert('The status of the session is: ' + response.status);
            });

所以现在当用户在令牌过期后打开我的页面时:

  • php sdk检测到他没有登录并显示登录对话框(和以前一样)
  • javascript sdk触发登录函数(如)
  • 用户像以前一样定期登录(我可以知道)现在可以确定了,因为它会弹出警告session is:connected)
  • 但. .事件发生了。未触发登录->脚本不知道成功登录->页面不知道重载

我也有同样的问题。javascript SDK让用户自动登录并重新加载页面,如果他们连接我的应用程序,他们的访问令牌已经过期。

我通过切换到auth来解决它。statusChange而不是auth.login。然后我用PHP Facebook SDK检查用户是否登录,如果他是,我不订阅授权。javascript中的statusChange事件。(如果你包括了真实的。statusChange在javascript中,当用户登录时,你将结束在一个无限的页面刷新循环,至少这是我的实现的情况)。