Facebook登录按钮无法刷新以自动注销

时间:2017-03-29 15:23:56

标签: javascript facebook facebook-javascript-sdk

使用“使用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 个答案:

答案 0 :(得分:0)

好吧,我无法弄清楚为什么它不会自动刷新 - 也许是设计的。这是我的解决方法 - 基本上添加注销按钮并自己调用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>