从 Facebook JS API 获取电子邮件,问题还是功能?
Getting email from Facebook JS API, issue or feature?
我正在使用以下内容作为参考:
https://developers.facebook.com/docs/facebook-login/web
但是,我似乎无法提取电子邮件。我不确定这是因为用户设置不允许电子邮件,还是我正在拨打的电话有问题。
这是我正在使用的代码:
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
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.';
}
}
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
var result = JSON.stringify(response);
alert(response.email)
}, {scope: 'public_profile,email,first_name,last_name'});
<fb:login-button data-scope="email" scope="public_profile, email" onlogin="checkLoginState();">
</fb:login-button>
我可以正常登录,但我没有收到电子邮件。不知道是用户设置问题还是代码问题
FB.api 没有范围参数,您已经在登录按钮中定义了范围。也没有名为 "first_name" 或 "last_name" 的范围,它们只是用户 table.
中的字段
无论如何,从 v2.4 开始,您必须在 API 端点中指定字段:https://developers.facebook.com/docs/apps/changelog#v2_4
例如:
FB.api('/me', {fields: 'email,first_name,last_name'}, function(response) {
...
});
您可以在文档中阅读有关 FB.api 的更多信息:https://developers.facebook.com/docs/javascript/reference/FB.api
顺便说一句,有一篇关于使用 JS SDK 登录的文章,如果您需要更多信息:http://www.devils-heaven.com/facebook-javascript-sdk-login/
我正在使用以下内容作为参考:
https://developers.facebook.com/docs/facebook-login/web
但是,我似乎无法提取电子邮件。我不确定这是因为用户设置不允许电子邮件,还是我正在拨打的电话有问题。
这是我正在使用的代码:
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
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.';
}
}
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
var result = JSON.stringify(response);
alert(response.email)
}, {scope: 'public_profile,email,first_name,last_name'});
<fb:login-button data-scope="email" scope="public_profile, email" onlogin="checkLoginState();">
</fb:login-button>
我可以正常登录,但我没有收到电子邮件。不知道是用户设置问题还是代码问题
FB.api 没有范围参数,您已经在登录按钮中定义了范围。也没有名为 "first_name" 或 "last_name" 的范围,它们只是用户 table.
中的字段无论如何,从 v2.4 开始,您必须在 API 端点中指定字段:https://developers.facebook.com/docs/apps/changelog#v2_4
例如:
FB.api('/me', {fields: 'email,first_name,last_name'}, function(response) {
...
});
您可以在文档中阅读有关 FB.api 的更多信息:https://developers.facebook.com/docs/javascript/reference/FB.api
顺便说一句,有一篇关于使用 JS SDK 登录的文章,如果您需要更多信息:http://www.devils-heaven.com/facebook-javascript-sdk-login/