如何使用 javascript sdk 获取 Facebook 用户的电子邮件 ID
我正在使用 JavaScript API 为 Facebook 创建应用程序。问题是,它正在返回电子邮件=未定义
。
我不知道为什么?如果我在应用程序上使用 Facebook 登录/注销按钮,则警报会显示用户的正确电子邮件 ID,但我不想这样做。 我缺少什么?
这是我的代码:
<p><fb:login-button autologoutlink="true" perms="user_about_me,email"></fb:login-button></p>
<script>
window.fbAsyncInit = function () {
FB.init({ appId: '250180631699888', status: true, cookie: true,
xfbml: true
});
FB.getLoginStatus(function (response) {
if (response.session) {
greet();
}
});
};
(function () {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
} ());
function greet() {
FB.api('/me', function (response) {
alert('Welcome, ' + response.name + "!");
alert('Your email id is : '+ response.email);
});
}
</script>
I am using JavaScript API to create my app for Facebook. The problem is, it's returningemail = undefined
.
I don't know why? And if I use Facebook login/logout button on my app then the alert shows correct email id of the user but I don't want to do that.
What am I missing?
Here is my code:
<p><fb:login-button autologoutlink="true" perms="user_about_me,email"></fb:login-button></p>
<script>
window.fbAsyncInit = function () {
FB.init({ appId: '250180631699888', status: true, cookie: true,
xfbml: true
});
FB.getLoginStatus(function (response) {
if (response.session) {
greet();
}
});
};
(function () {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
} ());
function greet() {
FB.api('/me', function (response) {
alert('Welcome, ' + response.name + "!");
alert('Your email id is : '+ response.email);
});
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据 Facebook 页面上的最新信息,您应该使用“范围”而不是权限。
https://developers.facebook.com/docs/reference/javascript/FB.login/
如果您访问
https://developers.facebook.com/tools/console/
并使用 fb-api - >以 user-info 示例作为起点,然后注销并再次登录,它应该要求您提供电子邮件权限,您可以看到您的电子邮件正在打印。正如您在帖子中提到的那样,这是使用response.email 完成的。
According to the latest info on the facebook page you should use 'scope' instead of perms.
https://developers.facebook.com/docs/reference/javascript/FB.login/
If you visit
https://developers.facebook.com/tools/console/
and use the fb-api -> user-info example as a starting point, then logout and back in again, it should ask you for email perms and you can see your email being printed. It is done using response.email as you mention in your post.
使用它来获得额外许可,
了解更多详细信息,请访问:
https://www.fbrell.com/examples/
Use this to for extra permission
for more details visit :
https://www.fbrell.com/examples/
在此代码中,我从 facebook 获取用户数据并使用 ajax 存储到我的数据库中
In this code i have get user data form facebook and store into my database using ajax
您的解决方案有一些问题。首先,您使用的是旧的身份验证方案。您应该使用此处描述的新版本:
https://developers.facebook.com/docs/reference/javascript/
您需要将 oauth:true 添加到您的 init 函数中,并确保您的 getLoginStatus 查找新类型的响应。
说到这里,您需要确保您拥有查看用户电子邮件的正确权限。您可以在此处查看所需的权限:
http://developers.facebook.com/docs/reference/api/user/
您可以通过使用 FB.login 函数来获取这些信息,如 TommyBs 在另一个答案中所述。
一旦您有了这些选项,您就可以使用 FB.api 函数来获取电子邮件。
There are a couple of things wrong with your solution. First of all you are using the old authentication scheme. You should use the new one described here :
https://developers.facebook.com/docs/reference/javascript/
You need to add the oauth:true to your init function, and make sure that your getLoginStatus looks for the new type of response.
When that is said you need to make sure you have the right permissions to see the users e-mail. You can see the required permissions here:
http://developers.facebook.com/docs/reference/api/user/
You get those by using the FB.login function as described by TommyBs in another answer.
Once you have those options you can use the FB.api function to get the e-mail.
这是我如何检索用户名和电子邮件的示例:
here is example how i retrieve user name and e-mail: