繁体   English   中英

当用户使用javascript通过Facebook登录时如何获取用户信息

[英]how to get info of user when they login via Facebook using javascript

我写这段代码。 如果用户之前已登录,则可以正常工作,但是如果用户尚未登录Facebook,则将其重定向到Facebook登录页面,成功登录后,他将重定向到我之前在开发人员中提供的页面。 facebook.com/app。 在这种情况下,我无法获取用户信息。 下面是我的代码:

<html>
<body>
    <script>
        var client_id = my_client_id;
        var url       = 'http://localhost/myitems.html';
        var scope     = 'email,public_profile';

        window.fbAsyncInit = function() {
            FB.init({
                appId      : client_id,
                xfbml      : true,
                status     : true,
                version    : 'v2.5'
            });
        };

        (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'));

        function loginFacebook(){
            FB.getLoginStatus(handleLoginFacebook);
        }

        function handleLoginFacebook(res){
            if(res.status==='connected'){
                var user={};
                user.access_token=res.authResponse.accessToken;

                FB.api('/me?fields=email,first_name,last_name,picture',function(res){
                    alert(JSON.stringify(res));
                    user.first_name   = res.first_name;
                    user.last_name    = res.last_name;
                    user.network      = 'facebook';
                    user.image        = res.picture;
                    user.network_id   = res.id;
                    user.deviceToken  = 0;
                    user.email        = res.email;
                    loginSocial(user, function(data){
                        var d=JSON.stringify(data);
                        alert(d);
                        d=JSON.parse(d);

                        if(d.success==true){
                            store(d);
                            window.location='myitems.html';
                        }else{
                            window.location='index.html';
                        }
                    });
                });
            }else{
                window.location='https://www.facebook.com/dialog/oauth/?client_id='+client_id+'&redirect_uri='+url+'&scope='+scope;
            }
        }
    </script>

    <button onclick='loginFacebook()'>Facebook Login</button>
</body>
</html>

你的其他应该是这样的:

FB.login(function (response) {
   //Get date here from the response.authResponse
   //In 'scope:' declare the scope of information you need
}, {scope: 'email, public_profile, user_friends', return_scopes: true});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM