Facebook返回未定义的电子邮件和生日 [英] Facebook returns undefined for email and birthday

查看:104
本文介绍了Facebook返回未定义的电子邮件和生日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用提供 first_name last_name 性别的Facebook API 电子邮件生日在我的网站上注册。我使用Graph AP并生成具有必要范围的访问令牌。
我尝试了我创建应用程序的个人帐户,并且正确地返回了字段,但是对于FB测试用户和朋友的帐户,它将以未定义的形式返回电子邮件和生日。在graph.facebook.com中,显示朋友帐户的以下错误。





Facebook在doulingo上返回我朋友同一个帐户的电子邮件地址。


解决方案

我的代码的问题是因为不使用access_token。
我使用access_token检索所有, first_name last_name gender 生日电子邮件

 函数authUser(){
FB.login(checkLoginStatus,{scope:'email,user_likes'});
function checkLoginStatus(response){
if(!response || response.status!=='connected'){
alert('User is not authorized');
document.getElementById('loginButton')。style.display ='block';
} else {
alert('用户授权');
document.getElementById('loginButton')。style.display ='none';
console.log('Access Token:'+ response.authResponse.accessToken);

//这必须在你的回调中!
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
//访问令牌非常重要,以获取一些字段,如电子邮件,生日

getData();
}
}
}


函数getData(){
console.log('欢迎!获取您的信息.... );
FB.api('/ me',function(response){
console.log('Good to see you,'+ response.name +'。'+ response.id);
});
};


I am trying to use the Facebook API that provides first_name, last_name, gender, email, birthday for signup on my website. I used the Graph AP and generated an access token with necessary scope. I tried on my personal account in which I created the app and it returned the fields properly, but for FB test user and friend's account, it returns email and birthday as "undefined". In graph.facebook.com it shows the following error for a friend's account.

Facebook returns email id for my friend's same account on doulingo.

Am I setting the permissions incorrectly or is there any other problem ?

解决方案

The problem with my code was all because of not using access_token. I used access_token to retrieve all, first_name, last_name, gender, birthday, email.

function authUser() {
            FB.login(checkLoginStatus, {scope:'email, user_likes'});
            function checkLoginStatus(response) {
                if(!response || response.status !== 'connected') {
                    alert('User is not authorized');
                    document.getElementById('loginButton').style.display = 'block';
                } else {
                    alert('User is authorized');
                    document.getElementById('loginButton').style.display = 'none';
                    console.log('Access Token: ' + response.authResponse.accessToken);

                    //This has to be in your callback!
                    var uid = response.authResponse.userID;
                    var accessToken = response.authResponse.accessToken;
                // access token is very important to get some fields like email, birthday

                    getData();
                }
            }
        }


    function getData() {
        console.log('Welcome!  Fetching your information.... ');
        FB.api('/me', function(response) {
            console.log('Good to see you, ' + response.name + '.' + response.id);
        });
    };

这篇关于Facebook返回未定义的电子邮件和生日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆