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();
                };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮