2017-08-30 72 views
2

我设法通过Facebook API登录(我认为)。这是我的代码:通过Facebook登录后检索名和姓

<script> 
    window.fbAsyncInit = function() { 
    FB.init({ 
     appId   : '134824563795810', 
     autoLogAppEvents : true, 
     xfbml   : true, 
     version   : 'v2.10' 
    }); 
    FB.AppEvents.logPageView(); 
    }; 

    (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 myFacebookLogin() { 
    FB.login(function(){}, {scope: 'publish_actions'}); 
    } 

</script> 

而简单的按钮只是为了测试,如果这个工程:

<button onclick="myFacebookLogin()">Kontynuuj przez facebook</button> 

点击它后,我收到此弹出:

enter image description here

我点击好吧,我想我已经登录了。另一次点击会生成小的弹出窗口,很快就会消失。我说我认为我已登录,因为我没有在我的网站上使用任何授权。我不需要它。

我的问题是:如果我登录了,如何获取用户的名和姓,并将这些值输入到括号内?

enter image description here

我想使脚本只把值代入输入。像:

  • Imię - >first name
  • Nazwisko - >last name

有什么建议?

编辑:

我终于想出了这个代码:

<script> 
function getUserData() { 
     FB.api('/me?fields=first_name,last_name,email,location', function(response) { 
       var imie = document.getElementById('basketInputName'); 
       if(!imie || !imie.value) 
        imie.value = response.first_name; 
       var nazwisko = document.getElementById('basketInputLastName'); 
        if(!nazwisko || !nazwisko.value) 
         nazwisko.value = response.last_name; 
       var email = document.getElementById('basketInputMail'); 
        if(!email || !email.value) 
         email.value = response.email; 
       var location = document.getElementById('cityToHidden'); 
        if(!location || !location.value) 
         location.value = response.location.name; 
     }); 
} 
    window.fbAsyncInit = function() { 


FB.init({ 
    appId   : '134824563795810', 
    autoLogAppEvents : true, 
    xfbml   : true, 
    version   : 'v2.10' 
}); 
FB.getLoginStatus(function(response) { 
     if (response.status === 'connected') { 
       //user is authorized 
       document.getElementById('loginBtn').style.display = 'none'; 
       getUserData(); 
     } else { 
       //user is not authorized 
     } 
}); 
    }; 

    (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 myFacebookLogin() { 
FB.login(function(){ 
    getUserData(); 
}, {scope: 'user_location'}); 
} 

但它不工作,我怎么想。当我点击loggin按钮没有任何反应,但当我刷新页面,并检查桶我看到firstName,lastNameemail在输入。我不知道如何通过点击login按钮进行动态填充输入而无需刷新页面。 有人可以帮忙吗?

+0

您需要使用用户的访问令牌对'/ me'进行API调用,然后询问您想要的字段。如果你想使用JS SDK来做这个客户端 - https://developers.facebook.com/docs/javascript/reference/FB.api/ – CBroe

+0

你需要什么publish_actions?如果您只想获取名称,则不需要额外的权限,特别是不需要额外的权限。关于登录过程,看看这篇文章:http://www.devils-heaven.com/facebook-javascript-sdk-login/ – luschn

+0

你有权利luschn,不需要publish_actions。我将它改为:'scope:'email,public_profile'' – speedvees

回答

0

使用user_about_me & public_profile范围

function myFacebookLogin() { 
    FB.login(function(){}, {scope: 'publish_actions,user_about_me,public_profile'}); 
} 
3

使用public_profile范围

function myFacebookLogin() { 
FB.login(function(){}, {scope: 'publish_actions,public_profile'}); 

}

在公众形象你FIRST_NAME和LAST_NAME

看到这里details