Javascript Facebook JS SDK 的 FB.api('/me') 方法不会返回我在 Graph API v2.4+ 中期望的字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32584850/
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
Facebook JS SDK's FB.api('/me') method doesn't return the fields i expect in Graph API v2.4+
提问by Juan Fuentes
I'm trying to get some basic information using Facebook api, but so far I only get the user's name and id. As in { name: "Juan Fuentes", id: "123456" }
我正在尝试使用 Facebook api 获取一些基本信息,但到目前为止我只获取了用户的姓名和 ID。如{ name: "Juan Fuentes", id: "123456" }
I need to get mor einformation, like email, first name, last name and birthday
我需要获取更多电子信息,例如电子邮件、名字、姓氏和生日
This is my js code
这是我的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' }
);
}
And this is the button that activates it
这是激活它的按钮
<a id="fb-login" href="#" onclick="facebookLogin()"></a>
回答by Tobi
You need to manually specify each field since Graph API v2.4:
从 Graph API v2.4 开始,您需要手动指定每个字段:
- https://developers.facebook.com/docs/apps/changelog#v2_4
- https://developers.facebook.com/docs/javascript/reference/FB.api
- https://developers.facebook.com/docs/apps/changelog#v2_4
- https://developers.facebook.com/docs/javascript/reference/FB.api
Declarative Fields
To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data. For more details see the docs on how to request specific fields.
声明性字段
为了尝试提高移动网络的性能,v2.4 中的节点和边缘要求您明确请求 GET 请求所需的字段。比如GET /v2.4/me/feed默认不再包含赞和评论,但是GET /v2.4/me/feed?fields=comments,likes会返回数据。有关更多详细信息,请参阅有关如何请求特定字段的文档。
E.g.
例如
FB.api('/me', 'get', { access_token: token, fields: 'id,name,gender' }, function(response) {
console.log(response);
});
回答by NXT
It's also possible to use this syntax for data from public_profilescope (tested in Graph API v2.9):
也可以将此语法用于来自public_profile范围的数据(在 Graph API v2.9 中测试):
FB.api('/me?fields=birthday,link,gender,age_range', function(response) {
console.log(response);
});
You can test the possible values online in Graph API Explorer, just click "Get Token" button:
您可以在Graph API Explorer 中在线测试可能的值,只需单击“获取令牌”按钮:
回答by Mukesh
Here is the complete Script:
这是完整的脚本:
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'));
Thanks.
谢谢。
回答by stephan f
Note that email is not always returnedby a call to the me
api with email
as field, even if scope email
was requested and granted, if e.g. the user signed up with a phone number:
请注意,电子邮件并不总是通过调用me
带有email
as 字段的api返回,即使范围email
被请求并授予,如果例如用户使用电话号码注册:
https://developers.facebook.com/docs/facebook-login/permissions#reference-email
https://developers.facebook.com/docs/facebook-login/permissions#reference-email