我尝试在我的网站上使用FB登录,需要获得用户信息(电子邮件,public_profile,出生日期和位置)。但登陆后,我只找到了用户的名字和facebook账号。代码:
<img src="" alt="" onclick="fblogin();"/>
Javascript部分:
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
testAPI();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
// This is called with the results from from FB.getLoginStatus().
function checkLoginState() {
FB.getLoginStatus(function (response) {
statusChangeCallback(response);
});
}
function fblogin()
{
checkLoginState();
FB.login(function (response) {
if (response.authResponse) {
console.log('Bilgiler Alınıyor');
FB.api('/me', function (response) {
console.log(JSON.stringify(response));
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, { scope: 'user_about_me,email,public_profile' });
}
window.fbAsyncInit = function () {
FB.init({
appId: '{app-id}',
xfbml: true,
version: 'v2.4'
});
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
console.log(response.authResponse.accessToken);
}
});
};
(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'));
JSON result:
{"name":"Name Surname","id":"xxxxxxxxxxxxxxxxx"}
没有电子邮件或任何其他信息。
这是一个众所周知的问题,在变更日志中搜索"声明字段":https://developers.facebook.com/docs/apps/changelog#v2_4
例如:FB.api('/me?fields=id,name,email', ...
我相信你也可以这样做:
FB.api('/me', {fields: 'id,name,email'}, ...
Btw,确保你知道异步JavaScript是如何工作的,你在FB之前调用checkLoginState()。登录,但FB。getLoginStatus不立即返回一些东西,你总是调用FB.login。这里有一篇关于如何使用FB的文章。登录和FB。getLoginStatus: http://www.devils-heaven.com/facebook-javascript-sdk-login/