2

我設法使用facebook js sdk開發登錄/註銷。如何檢查頁面刷新後驗證用戶的登錄狀態使用facebook javascript sdk

登錄後,我可以得到用戶的電子郵件。

我在刷新整個頁面後嘗試獲取已驗證用戶的信息時遇到了問題。

不知何故,FB對象不存在了。

我得到的錯誤:

Uncaught ReferenceError: FB is not defined

我怎麼能初始化的FB對象?

PS:我已閱讀和編碼解決方案:

FB is not defined javascript

,但它不工作。

在這個例子中,/我被替換/l214.animaux

我不知道該用戶個人資料ID。

我的代碼是在2個PHP文件分爲:

的index.php:

<div class="fb-login-button" onlogin="checkLoginState();" data-scope="public_profile,email" data-max-rows="1" data-size="xlarge" data-show-faces="false" data-auto-logout-link="true"></div> 

和facebook_login.php:

<!DOCTYPE html> 

<html lang="en"> 

    <head> 
     <?php 
     $title = Yii::app()->name; 
     ?> 
     <title><?php echo $title; ?></title> 
     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> 
    </head> 

    <body class="inner-page page-produse"> 

     <div id="fb-root"></div> 
     <script> 

      (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/sdk.js#xfbml=1&version=v2.8&appId=1813020315643394"; 
       fjs.parentNode.insertBefore(js, fjs); 
      }(document, 'script', 'facebook-jssdk')); 

      function api_call_get_info_about_the_logged_in_user() { 

       console.log('Welcome! Fetching your information.... '); 

       FB.api('/me', {fields: 'email'}, function (response) { 

        console.log('Successful login for: ' + response.email); 

       }); 

      } 

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

       api_call_get_info_about_the_logged_in_user(); 
      }; 

      function checkLoginState() { 
       FB.getLoginStatus(
         function (response) { 
          console.log('facebook get login status'); 
          console.log(response); 
          if (response.status === 'connected') { 

// Logged into your app and Facebook. 

           api_call_get_info_about_the_logged_in_user(); 

          } else if (response.status === 'not_authorized') { 

// The person is logged into Facebook, but not your app. 

          } else { 

// The person is not logged into Facebook, so we're not sure if 

// they are logged into this app or not. 

          } 
         } 
       ); 
      } 

     </script> 

     <div class="wrapper"> 

      <div class="site-wrapper"> 

       <?php 
       echo $content; 
       ?> 

       <div class="site-wrapper-overlay"></div> 

      </div> <!-- .site-wrapper --> 

     </div> <!-- .wrapper --> 

    </body> 

</html> 

回答

1

多次嘗試後,我試着撥打checkLoginState功能,它的工作原理。

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

       //api_call_get_info_about_the_logged_in_user(); 
       checkLoginState(); 
      }; 
相關問題