我正在尝试使用Facebook获取一些基本信息,但到目前为止,我只获得用户的姓名和id。如在{ name: "Juan Fuentes", id: "123456" }
我需要得到电子信息,比如电子邮件,名字,姓氏和生日。
这是我的js代码
function facebookLogin() {
FB.login(function(response) {
var token = response.authResponse.accessToken;
var uid = response.authResponse.userID;
if (response.authResponse) {
FB.api('/me', 'get', { access_token: token }, function(response) {
console.log(response);
});
FB.api('/'+uid, 'get', { access_token: token }, function(response) {
console.log(response);
});
}
},
{ scope: 'public_profile' }
);
}这就是激活它的按钮
<a id="fb-login" href="#" onclick="facebookLogin()"></a>发布于 2015-09-15 11:56:05
您需要手动指定每个字段,因为Graph版本2.4:
声明域 为了提高移动网络上的性能,v2.4中的节点和边缘要求您显式地请求GET请求所需的字段。例如,GET /v2.4/me/feed在默认情况下不再包含“喜欢”和“注释”,但是GET /v2.4/me/feed?field=注释将返回数据。有关详细信息,请参阅有关如何请求特定字段的文档。
例如。
FB.api('/me', 'get', { access_token: token, fields: 'id,name,gender' }, function(response) {
console.log(response);
});发布于 2017-06-16 11:51:54
还可以对来自public_profile范围的数据使用这种语法(在GraphAPIv2.9中进行了测试):
FB.api('/me?fields=birthday,link,gender,age_range', function(response) {
console.log(response);
});您可以在图形API资源管理器中在线测试可能的值,只需单击“获取令牌”按钮:
发布于 2019-09-18 15:20:40
这里是完整的脚本:
jQuery(document).ready(function () {
openLoginPopup();
})
function openLoginPopup() {
FB.getLoginStatus(function (response) {
if (response.status == 'connected') {
getCurrentUserInfo(response);
} else {
FB.login(function (response) {
if (response.authResponse) {
getCurrentUserInfo(response);
} else {
console.log('Auth cancelled.');
}
}, {scope: 'email'});
}
});
}
function getCurrentUserInfo() {
FB.api('/me?fields=id,email,first_name,last_name,name', function (userInfo) {
console.log(userInfo.name + ': ' + userInfo.email);
});
}
window.fbAsyncInit = function () {
FB.init({
// appId: 'xxxxxxxxxxxxxxxxxxxxx', //livemode
appId: 'xxxxxxxxxxxx', //testmode
cookie: true, // Enable cookies to allow the server to access the session.
xfbml: true, // Parse social plugins on this webpage.
version: 'v4.0' // Use this Graph API version for this call.
});
};
(function (d, s, id) { // Load the SDK asynchronously
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id))
return;
js = d.createElement(s);
js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));谢谢。
https://stackoverflow.com/questions/32584850
复制相似问题