获取 Facebook 用户数据 Javascript API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24031418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Facebook User Data Javascript API
提问by Tsundoku
For some reason I can't get the user data even though I'm pretty sure my code is correct. The object isn't returning certain values (like: link, birthday, hometown, etc). Here's what I have:
出于某种原因,即使我很确定我的代码是正确的,我也无法获取用户数据。该对象没有返回某些值(例如:链接、生日、家乡等)。这是我所拥有的:
$(function () {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
basicAPIRequest();
}
});
});
function basicAPIRequest() {
FB.api('/me',
{fields: "id,about,age_range,picture,bio,birthday,context,email,first_name,gender,hometown,link,location,middle_name,name,timezone,website,work"},
function(response) {
console.log('API response', response);
$("#fb-profile-picture").append('<img src="' + response.picture.data.url + '"> ');
$("#name").append(response.name);
$("#user-id").append(response.id);
$("#work").append(response.gender);
$("#birthday").append(response.birthday);
$("#education").append(response.hometown);
}
);
}
回答by thewheat
You will need to request permissions to access these values
您将需要请求访问这些值的权限
Essentially your login buttonshould have the necessary data-scope attribute value to ask the necessary permissions from the user
本质上,您的登录按钮应该具有必要的数据范围属性值,以向用户询问必要的权限
<div class="fb-login-button" data-scope="email,user_birthday,user_hometown,user_location,user_website,user_work_history,user_about_me"
data-max-rows="1" data-size="medium" data-show-faces="false" data-auto-logout-link="false"></div>
Sample code below, just fill in your Facebook API ID
下面的示例代码,只需填写您的 Facebook API ID
<html>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : yourappid,
xfbml : true,
version : 'v2.0'
});
};
(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 basicAPIRequest() {
FB.api('/me',
{fields: "id,about,age_range,picture,bio,birthday,context,email,first_name,gender,hometown,link,location,middle_name,name,timezone,website,work"},
function(response) {
console.log('API response', response);
$("#fb-profile-picture").append('<img src="' + response.picture.data.url + '"> ');
$("#name").append(response.name);
$("#user-id").append(response.id);
$("#work").append(response.gender);
$("#birthday").append(response.birthday);
$("#education").append(response.hometown);
}
);
}
jQuery(document).ready(function(){
jQuery("#load").click(function(e){
if(typeof(FB) == "undefined") {
alert("Facebook SDK not yet loaded please wait.")
}
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('Logged in.');
basicAPIRequest();
}
else {
FB.login();
}
});
});
});
</script>
fb-profile-picture: <div id="fb-profile-picture"></div>
name: <div id="name"></div>
user-id: <div id="user-id"></div>
work: <div id="work"></div>
birthday: <div id="birthday"></div>
education: <div id="education"></div>
<p>1) Click login</p>
<div class="fb-login-button" data-scope="email,user_birthday,user_hometown,user_location,user_website,user_work_history,user_about_me
" data-max-rows="1" data-size="medium" data-show-faces="false" data-auto-logout-link="false"></div>
<p>2) Click load data</p>
<button id='load'>Load data</button>
</html>