简体   繁体   English

Javascript-使用Facebook登录,成功登录后显示用户的个人资料图片

[英]Javascript - Login with Facebook, display user's profile picture after logged in successfully

I am making a Facebook Quiz app. 我正在制作一个Facebook测验应用程序。 Which lets the user log in with Facebook. 允许用户使用Facebook登录。 I'm trying to display User's profile picture automatically without having to click on "Get Info" but really it kept crashing the code and it the login button stops working. 我试图自动显示用户的个人资料图片,而无需单击“获取信息”,但实际上它一直使代码崩溃,并且登录按钮停止工作。 Currently, it works. 目前,它可以工作。 The user logs in then to display the profile picture, the user has to click "Get Info" button and it will display the user's profile picture. 用户登录然后显示个人资料图片,用户必须单击“获取信息”按钮,它将显示用户的个人资料图片。 I don't want the "get info" button , I want the profile picture to be automatically displayed. 我不希望“获取信息”按钮 ,我希望自动显示个人资料图片。

HTML: HTML:

<div id="status"></div>
   <div id="lgnbtn">
      <button onclick="login()" id="login">Login with Facebook</button>
   </div>
<button onclick="getInfo()" id="getInfo">Get Info</button>

Javascript: Javascript:

// initialize and setup facebook js sdk
        window.fbAsyncInit = function() {
            FB.init({
              appId      : '1036296558220017',
              xfbml      : true,
              version    : 'v2.5'
            });

            FB.getLoginStatus(function(response) {
                if (response.status === 'connected') {
                    document.getElementById('status').innerHTML = 'We are connected.';
                    document.getElementById('login').style.visibility = 'hidden';
                } else if (response.status === 'not_authorized') {
                    document.getElementById('status').innerHTML = 'We are not logged in.'
                } else {
                    document.getElementById('status').innerHTML = 'You are not logged into Facebook.';
                }
            });
        };
        (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'));

        // login with facebook with extra permissions
        function login() {
            FB.login(function(response) {
                if (response.status === 'connected') {
                    document.getElementById('status').innerHTML = 'We are connected.';
                    document.getElementById('login').style.visibility = 'hidden';
                } else if (response.status === 'not_authorized') {
                    document.getElementById('status').innerHTML = 'We are not logged in.'
                } else {
                    document.getElementById('status').innerHTML = 'You are not logged into Facebook.';
                }
            }, {scope: 'email'});
        }

    // getting basic user info
        function getInfo() {
                FB.api('/me', 'GET', {fields: 'first_name,last_name,name,id,picture.width(150).height(150)'}, function(response) {
                document.getElementById('status').innerHTML = "<img src='" + response.picture.data.url + "'>";
            });

        }

Invoke getInfo(); 调用getInfo(); in FB.login success handler. FB.login成功处理程序中。

 window.fbAsyncInit = function() { FB.init({ appId: '1034762986580727', xfbml: true, version: 'v2.5' }); FB.getLoginStatus(function(response) { if (response.status === 'connected') { document.getElementById('status').innerHTML = 'We are connected.'; document.getElementById('login').style.visibility = 'hidden'; } else if (response.status === 'not_authorized') { document.getElementById('status').innerHTML = 'We are not logged in.' } else { document.getElementById('status').innerHTML = 'You are not logged into Facebook.'; } }); }; (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')); // login with facebook with extra permissions function login() { FB.login(function(response) { if (response.status === 'connected') { document.getElementById('status').innerHTML = 'We are connected.'; document.getElementById('login').style.visibility = 'hidden'; getInfo(); // Invoke it here } else if (response.status === 'not_authorized') { document.getElementById('status').innerHTML = 'We are not logged in.' } else { document.getElementById('status').innerHTML = 'You are not logged into Facebook.'; } }, { scope: 'email' }); } // getting basic user info function getInfo() { FB.api('/me', 'GET', { fields: 'first_name,last_name,name,id,picture.width(150).height(150)' }, function(response) { console.log(response);//Check the response in console document.getElementById('status').innerHTML = "<img src='" + response.picture.data.url + "'>"; }); } 
 <div id="status"></div> <div id="lgnbtn"> <button onclick="login()" id="login">Login with Facebook</button> </div> 

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

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