facebook应用程序在循环中重新加载,登录注销事件在window.fbAsyninit内自动触发

facebook应用程序在循环中重新加载,登录注销事件在window.fbAsyninit内自动触发,facebook,facebook-graph-api,Facebook,Facebook Graph Api,我正在开发一个facebook(iframe)应用程序 我在页面底部包含了以下代码: <script> window.fbAsyncInit = function() { FB.Canvas.setAutoResize(); FB.init({ appId : '<?php echo FACEBOOK_APP_ID; ?>', session : '<?php ech

我正在开发一个facebook(iframe)应用程序

我在页面底部包含了以下代码:

<script>
    window.fbAsyncInit = function() {

        FB.Canvas.setAutoResize();  

        FB.init({
            appId   : '<?php echo FACEBOOK_APP_ID; ?>',
            session : '<?php echo json_encode($session); ?>',
            status  : true,
            cookie  : true,
            xfbml   : true
        });

        // whenever the user logs in / out, we refresh the page
        FB.Event.subscribe('auth.login', function() {               
            window.location.reload();               
        });

        FB.Event.subscribe('auth.logout', function() {
            window.location.reload();
        });
    };

    (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    }());

</script>

window.fbAsyninit=函数(){
FB.Canvas.setAutoResize();
FB.init({
appId:“”,
会话:“”,
状态:正确,
曲奇:是的,
xfbml:对
});
//每当用户登录/注销时,我们都会刷新页面
FB.Event.subscribe('auth.login',function(){
window.location.reload();
});
FB.Event.subscribe('auth.logout',function(){
window.location.reload();
});
};
(功能(){
var e=document.createElement('script');
e、 src=document.location.protocol+'//connect.facebook.net/en_US/all.js';
e、 异步=真;
document.getElementById('fb-root').appendChild(e);
}());
这里的问题是,似乎auth.login和auth.logout事件在window.fbAsyninit函数中立即被触发,即使我没有从我的facebook个人资料注销(或如果注销,则登录)

我没有做这样的操作,即使这些事件似乎被触发,这会导致它们的处理程序函数重新加载我的应用程序(window.location.reload();),并且应用程序进入无限重新加载循环

请引导我


根据我的经验,感谢您不用auth.logout和auth.login事件,您可以尝试以下代码:

FB.Event.subscribe('auth.authResponseChange', function(response) {

  FB.getLoginStatus(function(response) {

    if (response.status === 'connected') {
      // the user is logged in and connected to your
      // app, and response.authResponse supplies
      // the user's ID, a valid access token, a signed
      // request, and the time the access token 
      // and signed request each expire
      var uid = response.authResponse.userID;
      var accessToken = response.authResponse.accessToken;

    } else if (response.status === 'not_authorized') {
      // the user is logged in to Facebook, 
      //but not connected to the app
    } else {
      // the user isn't even logged in to Facebook.
    }

  });

});

根据我的经验,您可以尝试以下代码,而不是使用auth.logout和auth.login事件:

FB.Event.subscribe('auth.authResponseChange', function(response) {

  FB.getLoginStatus(function(response) {

    if (response.status === 'connected') {
      // the user is logged in and connected to your
      // app, and response.authResponse supplies
      // the user's ID, a valid access token, a signed
      // request, and the time the access token 
      // and signed request each expire
      var uid = response.authResponse.userID;
      var accessToken = response.authResponse.accessToken;

    } else if (response.status === 'not_authorized') {
      // the user is logged in to Facebook, 
      //but not connected to the app
    } else {
      // the user isn't even logged in to Facebook.
    }

  });

});

几天来,我一直在努力解决这个bug。问题是,在加载SDK时,即使您已经连接,auth.login事件也会被触发,从而创建一个无限循环。当我让代码侦听authlogin事件并仅在用户未连接时重新加载页面时,我已经解决了这个问题

<script type="text/javascript">
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '<?php echo AppInfo::appID(); ?>', // App ID
      channelUrl : '//<?php echo $_SERVER["HTTP_HOST"]; ?>/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true // parse XFBML
    });

    // Listen to the auth.login which will be called when the user logs in
    // using the Login button
    FB.getLoginStatus(function(response) {
        console.log (response.status);
        //this is useful if you want to avoid the infinite loop bug
        //it only reloads page when you log in
        if (response.status != 'connected') {
            FB.Event.subscribe('auth.login', function(response) {
                // We want to reload the page now so PHP can read the cookie that the
                // Javascript SDK sat. But we don't want to use
                // window.location.reload() because if this is in a canvas there was a
                // post made to this page and a reload will trigger a message to the
                // user asking if they want to send data again.
                //window.location = window.location;
                //Im using the window.location.reload()
                window.location.reload();
            });
        }
    });
    FB.Canvas.setAutoGrow();
  };

  // Load the SDK Asynchronously
  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));

</script>

window.fbAsyninit=函数(){
FB.init({
appId:“”,//应用ID
channelUrl:'///channel.html',//通道文件
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
xfbml:true//解析xfbml
});
//收听auth.login,用户登录时将调用它
//使用登录按钮
FB.getLoginStatus(函数(响应){
console.log(response.status);
//如果您想避免无限循环错误,这是很有用的
//它仅在您登录时重新加载页面
如果(response.status!=“已连接”){
FB.Event.subscribe('auth.login',函数(响应){
//我们希望现在重新加载页面,以便PHP能够读取
//Javascript SDK。但是我们不想使用
//因为如果这是在画布中,则有一个
//对该页面进行post并重新加载将触发向
//询问是否要再次发送数据的用户。
//window.location=window.location;
//我正在使用window.location.reload()命令
window.location.reload();
});
}
});
FB.Canvas.setAutoGrow();
};
//异步加载SDK
(功能(d、s、id){
var js,fjs=d.getElementsByTagName[0];
if(d.getElementById(id))返回;
js=d.createElement;js.id=id;
js.src=“//connect.facebook.net/en_US/all.js”;
fjs.parentNode.insertBefore(js,fjs);
}(文档“脚本”、“facebook jssdk”);
希望这能奏效

无论如何,这不是我唯一的问题。请查看此帖子,如果可以,请帮助我

几天来,我一直在努力解决这个错误。问题是,在加载SDK时,即使您已经连接,auth.login事件也会被触发,从而创建一个无限循环。当我让代码侦听authlogin事件并仅在用户未连接时重新加载页面时,我已经解决了这个问题

<script type="text/javascript">
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '<?php echo AppInfo::appID(); ?>', // App ID
      channelUrl : '//<?php echo $_SERVER["HTTP_HOST"]; ?>/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true // parse XFBML
    });

    // Listen to the auth.login which will be called when the user logs in
    // using the Login button
    FB.getLoginStatus(function(response) {
        console.log (response.status);
        //this is useful if you want to avoid the infinite loop bug
        //it only reloads page when you log in
        if (response.status != 'connected') {
            FB.Event.subscribe('auth.login', function(response) {
                // We want to reload the page now so PHP can read the cookie that the
                // Javascript SDK sat. But we don't want to use
                // window.location.reload() because if this is in a canvas there was a
                // post made to this page and a reload will trigger a message to the
                // user asking if they want to send data again.
                //window.location = window.location;
                //Im using the window.location.reload()
                window.location.reload();
            });
        }
    });
    FB.Canvas.setAutoGrow();
  };

  // Load the SDK Asynchronously
  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));

</script>

window.fbAsyninit=函数(){
FB.init({
appId:“”,//应用ID
channelUrl:'///channel.html',//通道文件
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
xfbml:true//解析xfbml
});
//收听auth.login,用户登录时将调用它
//使用登录按钮
FB.getLoginStatus(函数(响应){
console.log(response.status);
//如果您想避免无限循环错误,这是很有用的
//它仅在您登录时重新加载页面
如果(response.status!=“已连接”){
FB.Event.subscribe('auth.login',函数(响应){
//我们希望现在重新加载页面,以便PHP能够读取
//Javascript SDK。但是我们不想使用
//因为如果这是在画布中,则有一个
//对该页面进行post并重新加载将触发向
//询问是否要再次发送数据的用户。
//window.location=window.location;
//我正在使用window.location.reload()命令
window.location.reload();
});
}
});
FB.Canvas.setAutoGrow();
};
//异步加载SDK
(功能(d、s、id){
var js,fjs=d.getElementsByTagName[0];
if(d.getElementById(id))返回;
js=d.createElement;js.id=id;
js.src=“//connect.facebook.net/en_US/all.js”;
fjs.parentNode.insertBefore(js,fjs);
}(文档“脚本”、“facebook jssdk”);
希望这能奏效

无论如何,这不是我唯一的问题。请查看此帖子,如果可以,请帮助我