异步facebook功能

asynchronous facebook functions

本文关键字:功能 facebook 异步      更新时间:2023-09-26

当javascript函数完成时,我似乎对正确的顺序有一些问题。我有一个页面,检查用户是否登录了facebook。如果有人登录,它会注销该人,并重定向到包含类似按钮的其他页面。然而,当我在没有重定向的情况下运行网页时,用户会正确注销。如果重定向已到位,则不再强制注销。

您可以在此网站上测试代码:http://quiet-depths-9481.herokuapp.com

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"     "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html>
  <head>
    <script src="src/jquery.js"></script>
  </head>
  <body style="background-color:#ff6600">
    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
    <script type="text/javascript">
      var loggedout = false;
      window.fbAsyncInit = function() {
      FB.init({
      appId      : 'xxxxxxxxxxxxx', // App ID
      channelUrl : '', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });
      fbApiInit = true;
  }; 
 (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/nl_NL/all.js#xfbml=1&appId=511896318825154";
     ref.parentNode.insertBefore(js, ref);
   }(document));
    function fbEnsureInit(callback) 
   {
      if(!window.fbApiInit) {
          setTimeout(function() {fbEnsureInit(callback);}, 50);
      } else {
          if(callback) {
              callback();
          }
      }
   }
   function fbEnsureLogout(callback) 
   {
      if(!window.loggedout) {
          setTimeout(function() {fbEnsureLogout(callback);}, 50);
      } else {
          if(callback) {
              callback();
          }
      }
   }
   fbEnsureInit(function()
   {
      console.log("this will be run once FB is initialized");
      checkLogout();
   });
   fbEnsureLogout(function()
   {
    console.log("this will run if logout is ensured");
    window.location = "http://quiet-depths-9481.herokuapp.com/like.php"
    document.write('<div class="fb-like" data-href="https://www.facebook.com/accentjobs" data-width="450" data-show-faces="true" data-stream="true" data-header="true"></div>');
   });
   function checkLogout()
   {
     console.log('checkLogout');
     FB.getLoginStatus(function(response) 
     {
       if (response.status === 'connected')          
       {
         var uid = response.authResponse.userID;
         var accessToken = response.authResponse.accessToken;
         console.log('logged in & authenticated');
         console.log('trying to logout now.');
         FB.logout(function(response) 
         {  
           console.log('LOGGED OUT!');
           loggedout = true;
         });
       } 
       else if (response.status === 'not_authorized') 
       {
         console.log('logged in but no authentication');
         console.log('trying to logout now.');
         FB.logout(function(response) 
         {  
           console.log('LOGGED OUT!');
           loggedout = true;
         });
       }
       else
       {
         console.log('Not logged in to facebook!');
         loggedout = true;
       }
     });
   }  
</script> 

我认为你不应该在没有应用程序身份验证的情况下注销facebook用户。你可以直接在控制台上阅读,facebook的all.js返回

FB.logout() called without an access token. 

这意味着,你必须调用他们的api(FB.login或FB.getLoginStatus)。无论哪种方式,如果没有你的应用程序/网站的适当用户授权,你都不会获得访问令牌。

你必须让用户使用你的应用程序登录,然后注销。这可能对你有用,因为你是应用程序的所有者,所以基本上你是通过应用程序连接的。