0

我有一个使用旧版本 FB API 的注册表单。

我正在尝试将其更新到当前版本,并且在处理来自 FB 的响应时遇到问题。

单击 FB 登录按钮并授予权限(电子邮件、publish_stream)后,我找不到知道用户已授予访问权限(而不仅仅是作为已注册用户登录)并需要继续进行注册的下一页的方法形式。

权限请求成功后的响应与登录成功相同:

{
    authResponse:
    {
        accessToken:    AAAFEIew...URYo,
        userID:         100003908595992,
        expiresIn:      6014,
        signedRequest:  uhTqZodDn0QUoWAUBuYEKmlaM8zViO0r_fnKONaC4v8.eyJhbGdvcml...k5MiJ9,
    },
    status: connected
}

如何判断登录是已注册用户的结果还是新注册的结果?

我正在使用 JavaScript SDK。

代码:

<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>


                <div id="fb-root" style="padding: 0px;"></div>
                <script>
                    window.fbAsyncInit = function() {
                        FB.init({
                            appId      : 'API_KEY',
                            status     : true,
                            channelUrl : '//'+window.location.hostname+'/channel.php',
                            cookie     : true,
                            xfbml      : true,
                            oauth      : true
                        });

                        // listen for and handle auth.statusChange events
                        FB.Event.subscribe('auth.statusChange', function(response) {
                            if (response.authResponse) {
                                // user has auth'd your app and is logged into Facebook
                                alert('logged in!');
                            }
                            else
                            {
                                // user has not auth'd your app, or is not logged into Facebook
                                alert('not logged in!');
                            }
                        });

                    };
                    (function(d){
                        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
                        js = d.createElement('script'); js.id = id; js.async = true;
                        js.src = "//connect.facebook.net/en_US/all.js";
                        d.getElementsByTagName('head')[0].appendChild(js);
                    }(document));
4

2 回答 2

0

这是我在尝试做你正在做的事情时使用的资源。它工作得非常好。

window.fbAsyncInit = function() {
    FB.init({ appId: 'Your_APP_ID',
        status: true,
        cookie: true,
        xfbml: true,
        oauth: true});

    showLoader(true);

    function updateButton(response) {
        button       =   document.getElementById('fb-auth');
        userInfo     =   document.getElementById('user-info');

        if (response.authResponse) {
            //user is already logged in and connected
            FB.api('/me', function(info) {
                login(response, info);
            });

            button.onclick = function() {
                FB.logout(function(response) {
                    logout(response);
                });
            };
        } else {
            //user is not connected to your app or logged out
            button.innerHTML = 'Login';
            button.onclick = function() {
                showLoader(true);
                FB.login(function(response) {
                    if (response.authResponse) {
                        FB.api('/me', function(info) {
                            login(response, info);
                        });
                    } else {
                        //user cancelled login or did not grant authorization
                        showLoader(false);
                    }
                }, {scope:'email,user_birthday,status_update,publish_stream,user_about_me'});
            }
        }
    }

    // run once with current status and whenever the status changes
    FB.getLoginStatus(updateButton);
    FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol
        + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
}());

参考:http ://thinkdiff.net/facebook/new-javascript-sdk-oauth-2-0-based-fbconnect-tutorial/

信仰斯隆

于 2012-05-31T21:50:11.003 回答
0

我意识到我需要使用 FB SDK 中的 getLoginStatus 函数。

我的功能代码是:

<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId       : '<?php echo HSWI_FB_KEY; ?>',
            status      : true,
            logging     : true,
            channelUrl  : '//'+window.location.hostname+'/channel.php',
            cookie      : true,
            xfbml       : true,
            oauth       : true
        });

        FB.Event.subscribe('auth.login', function(response)
        {
            window.location.href    = '/login_script.php';
        });

        FB.Event.subscribe('auth.logout', function(response) {
            window.location.reload();
        });

        checkLoggedInFB();

    };
    (function(d){
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
    }(document));

    function fbLogin()
    {
        FB.login(
            function(response)
            {
                if(response.authResponse)
                {
                    window.location.href    = '/login_script.php';
                }
            }
        );
    }

    function checkLoggedInFB()
    {
        try
        {
            FB.getLoginStatus(
                function(response)
                {
                    if(response.status == 'connected')
                    {
                        window.location.href    = '/login_script.php';
                    }
                }
            );
        }
        catch(e)
        {
            alert('Error checking if logged into FB: '+ e.message);
            return false;
        }
        return true;
    }

</script>
<style type="text/css">
    .fb-login-button
    {
        margin-top: -15px;
    }

    .fb-login-button.fb_iframe_widget.fb_hide_iframes
    {
        display: none;
    }
</style>
<div class="fb-login-button" scope="email,publish_stream,user_birthday,user_location,user_about_me">Login</div>
于 2012-08-22T18:15:57.813 回答