Facebook登录按钮不刷新自动注销

Posted

技术标签:

【中文标题】Facebook登录按钮不刷新自动注销【英文标题】:Facebook login button not refreshing to logout automatically 【发布时间】:2017-03-29 15:23:56 【问题描述】:

使用 'Facebook Login for the Web with the javascript SDK',我有一个登录按钮可以正常工作,即用户登录正常。(https://developers.facebook.com/docs/facebook-login/web#loginbutton)。

但是,在我刷新页面之前,该按钮不会更改为“注销”按钮。刷新后会显示注销按钮,单击后会按预期恢复为登录按钮。以下是可以在 IIS 中运行的完整代码。我把它放在我的默认网站上并调用http://localhost/aspnet_client/system_web/4_0_30319/test.html

注意:在上面写的地方添加您的应用 ID,在此处添加您的应用 ID。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
</head>

<body>
    <div class="row margin-bottom-small">
        <div id="login-button" class="fb-login-button" data-max-rows="1" data-size="xlarge" data-show-faces="false" data-auto-logout-link="true" onlogin="checkLoginState();"></div>
        <div id="status"></div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script>
        function statusChangeCallback(response) 
            console.log('statusChangeCallback');
            console.log(response);
            if (response.status === 'connected') 
                testAPI();
            
        
        function checkLoginState() 
            FB.getLoginStatus(function(response) 
                statusChangeCallback(response);
            );
        
        window.fbAsyncInit = function() 
                                FB.init(
                                appId      : 'ADD YOUR APP ID HERE',
                                cookie     : true,  // enable cookies to allow the server to access the session
                                xfbml      : true,  // parse social plugins on this page
                                version    : 'v2.8' // use graph api version 2.8
                            );
            FB.getLoginStatus(function(response) 
                statusChangeCallback(response);
            );
        ;
        // 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/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        (document, 'script', 'facebook-jssdk'));

        // Here we run a very simple test of the Graph API after login is
        // successful.  See statusChangeCallback() for when this call is made.
        function testAPI() 
            console.log('Welcome!  Fetching your information.... ');
            FB.api('/me', function(response) 
                console.log('Successful login for: ' + response.name);
                document.getElementById('status').innerHTML =
                'Thanks for logging in, ' + response.name + '!';
            );
        
    </script>
</body>
</html>

FB代码来自https://developers.facebook.com/docs/facebook-login/web#example

我不想以编程方式刷新页面。这是一个单页应用程序,刷新页面将带我开始。

谢谢。

【问题讨论】:

【参考方案1】:

好吧,我不明白为什么它不会自动刷新——也许是设计使然。这是我的解决方法 - 基本上添加注销按钮并自己调用 FB.logout。如果 Facebook 开发团队的某个人可以提供更优雅的解决方案,我很想知道。谢谢。

注意:在上面写的地方添加您的应用 ID,在此处添加您的应用 ID。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
</head>

<body>
    <div class="row margin-bottom-small">
        <div id="login-button" class="fb-login-button" data-max-rows="1" data-size="small" data-show-faces="false" data-auto-logout-link="false" onlogin="checkLoginState();"></div>
        <div hidden id="logout-button" class="fb-login-button" data-max-rows="1" data-size="small" data-show-faces="false" data-auto-logout-link="false" onlogin="logout();">Log Out</div>
        <div id="status"></div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script>
        function statusChangeCallback(response) 
            console.log('statusChangeCallback');
            console.log(response);
            if (response.status === 'connected') 
                testAPI();
             else 
                // The person is not logged into your app or we are unable to tell.
                //document.getElementById('status').innerHTML = 'Please log into this app.';
                document.getElementById('login-button').style.display = 'block';
                document.getElementById('logout-button').style.display = 'none';
            
        
        function checkLoginState() 
            FB.getLoginStatus(function(response) 
                statusChangeCallback(response);
            );
        
        function logout() 
            FB.logout(function (response) 
                document.getElementById('logout-button').style.display = 'none';
                document.getElementById('login-button').style.display = '';
                document.getElementById('status').innerHTML = 'You have been logged out!';
            );
        
        window.fbAsyncInit = function() 
                                FB.init(
                                appId      : 'ADD YOUR APP ID HERE',
                                cookie     : true,  // enable cookies to allow the server to access the session
                                xfbml      : true,  // parse social plugins on this page
                                version    : 'v2.8' // use graph api version 2.8
                            );
            FB.getLoginStatus(function(response) 
                statusChangeCallback(response);
            );
        ;
        // 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/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        (document, 'script', 'facebook-jssdk'));

        // Here we run a very simple test of the Graph API after login is
        // successful.  See statusChangeCallback() for when this call is made.
        function testAPI() 
            console.log('Welcome!  Fetching your information.... ');
            FB.api('/me', function(response) 
                console.log('Successful login for: ' + response.name);
                 document.getElementById('login-button').style.display = 'none';
                document.getElementById('logout-button').style.display = 'block';
                document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.name + '!';
            );
        
    </script>
</body>
</html>

【讨论】:

以上是关于Facebook登录按钮不刷新自动注销的主要内容,如果未能解决你的问题,请参考以下文章

Facebook注销按钮并在注销后重定向

ios Facebook 登录按钮 - 不会更改为注销

react-native如何在facebook登录和注销后刷新页面

Facebook SDK 4.4.+ |登录后如何隐藏注销按钮?

注销按钮操作不起作用

我们可以以编程方式注销 facebook