我設法通過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>
點擊它後,我收到此彈出:
我點擊好吧,我想我已經登錄了。另一次點擊會生成小的彈出窗口,很快就會消失。我說我認爲我已登錄,因爲我沒有在我的網站上使用任何授權。我不需要它。
我的問題是:如果我登錄了,如何獲取用戶的名和姓,並將這些值輸入到括號內?
我想使腳本只把值代入輸入。像:
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
,lastName
和email
在輸入。我不知道如何通過點擊login
按鈕進行動態填充輸入而無需刷新頁面。 有人可以幫忙嗎?
您需要使用用戶的訪問令牌對'/ me'進行API調用,然後詢問您想要的字段。如果你想使用JS SDK來做這個客戶端 - https://developers.facebook.com/docs/javascript/reference/FB.api/ – CBroe
你需要什麼publish_actions?如果您只想獲取名稱,則不需要額外的權限,特別是不需要額外的權限。關於登錄過程,看看這篇文章:http://www.devils-heaven.com/facebook-javascript-sdk-login/ – luschn
你有權利luschn,不需要publish_actions。我將它改爲:'scope:'email,public_profile'' – speedvees