fb登录中电子邮件的未定义值

时间:2015-11-16 07:42:17

标签: javascript php facebook email

我想从facebook i用户java脚本SDK获取用户的电子邮件ID以获取电子邮件ID

  <script>

(function(d){
    var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "https://connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));

     $(document).ready(function() {
     $('#fb_button').click(function() {
     FB._https = (window.location.protocol == "https:"); // Required because FB Javascript SDK tries to submit https to http
    FB.init({appId:"{app_id}", cookie:true, status:true, xfbml:true, oauth:true , version:'v2.5'});
    FB.login(function(response) {

    if (response.authResponse) {
    console.log('Welcome!  Fetching your information.... ');
    console.log(response); // dump complete info
    access_token = response.authResponse.accessToken; //get access token
    user_id = response.authResponse.userID; //get FB UID


    FB.api('/me', function(response) {
        user_email = response.email; //get user email
        alert(user_email);
  });  
  }
  else{
  alert('no response');
  }   
}, {
    scope: 'email'

    });

     });
});


(function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
  //  document.getElementById('fb-root').appendChild(e);
    }());
     </script>

它返回响应但未定义警报值 我复制粘贴相同的代码在另一个站点与不同的app_id它返回相同用户的电子邮件ID。我也试过PHP SDK但结果相同。可能这个问题是重复的。但我几乎尝试了所有这些问题的答案。

任何人都知道出了什么问题吗?

提前致谢

1 个答案:

答案 0 :(得分:3)

“声明字段”是您所需要的,请参阅更改日志:https://developers.facebook.com/docs/apps/changelog#v2_4

例如:

FB.api('/me', {fields: 'name,email'}, function(response) {
    user_email = response.email; //get user email
    alert(user_email);
});