Facebook JS SDK's FB.api('/me')方法不'不返回我在Grap
Facebook JS SDK's FB.api('/me') method doesn't return the fields I expect in Graph API v2.4+
我试图使用Facebook api获得一些基本信息,但到目前为止,我只获得用户的名称和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>
您需要手动指定每个字段,因为Graph API v2.4:
- https://developers.facebook.com/docs/apps/changelog v2_4
- https://developers.facebook.com/docs/javascript/reference/FB.api
<声明字段/strong>
为了尝试提高移动网络上的性能,v2.4中的node和edge要求您显式地请求GET请求所需的字段。例如,GET/v2.4/me/feed默认不再包含喜欢和评论,但是GET/v2.4/me/feed?Fields =comments,likes将返回数据。要了解更多详细信息,请参阅如何请求特定字段的文档。
。
FB.api('/me', 'get', { access_token: token, fields: 'id,name,gender' }, function(response) {
console.log(response);
});
对于来自public_profile作用域的数据也可以使用这种语法(在Graph API v2.9中进行了测试):
FB.api('/me?fields=birthday,link,gender,age_range', function(response) {
console.log(response);
});
您可以在Graph API Explorer中在线测试可能的值,只需单击"Get Token"按钮:
https://developers.facebook.com/tools/explorer/?method=GET&路径=我% 3 ffields % 3 dbirthday % 2叮当声% 2 cgender % 2 cage_range& version = v2.9
完整的脚本如下:
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'));
谢谢。
请注意,电子邮件并不总是通过调用email
作为字段的me
api返回,即使范围email
被请求和授予,如果例如用户注册了一个电话号码:
https://developers.facebook.com/docs/facebook-login/permissions reference-email
- 从Javascript方法返回全局变量
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- AngularJS错误:提供程序必须从$get工厂方法返回值
- 修复AngularJS错误:提供程序必须从$get工厂方法返回值
- 从 javascript 中的对象方法返回一个对象
- 为什么我的Mongoose findAll方法返回500错误.
- 角度方法返回未绘制的代码
- 解析查询查找方法返回对象而不是数组
- Jquery Ajax调用没有't点击操作方法/返回整个HTML页面
- ajax方法返回错误时如何打印输出
- Meteor 方法返回客户端和服务器上的不同对象
- 如何将异步方法返回的值发送到javascript中的另一个异步方法
- Javascript getDay方法返回错误的数字
- Coffeescription类中的方法返回函数而不是字符串
- 当使用Q/promises/异步函数时,如何将一个值从一个类方法返回到另一个类
- find()方法返回具有不需要的属性的对象
- 如何在JSF中将toDataURL方法返回的画布内容作为图像存储在磁盘上
- 从请求.js请求方法返回结果
- jQuery的.children()方法返回'undefined'
- Angularjs 控制器在服务方法返回之前从服务中分配变量