drxm72811 2016-12-05 11:18
浏览 83
已采纳

如何使用facebook javascript sdk检查页面刷新后验证用户的登录状态

I managed to develop the login/logout using the facebook js sdk.

After login, I am able to get the email of the user.

I am having trouble when I try to get the information for a authentified user, after I refresh the entire page.

Somehow, the FB object is not there anymore.

I get the error:

Uncaught ReferenceError: FB is not defined

How can I init the FB object ?

ps: I have already read and coded the solution from:

FB is not defined javascript

but it does not work.

In that example, /me is replaced with /l214.animaux

I don't know the user profile id.

My code is divided in 2 php files:

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>

and 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条回答 默认 最新

  • dongwu9063 2016-12-05 11:32
    关注

    After many attempts, I tried to call the checkLoginState function and it works.

    window.fbAsyncInit = function () {
                    FB.init({
                        appId: '1813020315643394',
                        xfbml: true,
                        version: 'v2.8'
                    });
    
                    //api_call_get_info_about_the_logged_in_user();
                    checkLoginState();
                };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体