通过Facebook Javascript SDK获取电子邮件

时间:2013-07-20 05:25:02

标签: javascript facebook facebook-graph-api facebook-javascript-sdk facebook-login

我刚刚开始使用facebook API,但我似乎无法使用FB来使用FB.User对象提醒我的电子邮件。我可以打印出我的userID和我的名字,但如果我试图打印出我的电子邮件或我的教育,它会返回undefined。这是我的代码:

<html>
<head>
  <script src ="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

</head>
<body>

<div id="fb-root"></div>
<script>
    var appId = '1374555416100192';
    var siteRoot = '';

  window.fbAsyncInit = function() {
    FB.init({
      appId      : appId,
      channelURL : '///channel.html', // Channel File
      status     : true,
      cookie     : true,
      oauth      : true,
      xfbml      : true
    });
    $(document).trigger('fbload');
  };


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



   $(document).on(
    'fbload',  //  <---- HERE'S OUR CUSTOM EVENT BEING LISTENED FOR
    function(){
        //some code that requires the FB object
        //such as...
        FB.getLoginStatus(function(res){
            if( res.status == "connected" ){
                FB.api('/me', function(fbUser) {
                    alert(fbUser.name  + " "  + fbUser.email);
                });
            }
        });

    }
);
</script>


<div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1">Start w/ Facebook</div>

</body>
</html>

1 个答案:

答案 0 :(得分:4)

您需要电子邮件权限才能访问

更改此行

<div class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1">Start w/ Facebook</div>

如下所示并检查(添加data-scope="email"

<div class="fb-login-button" data-scope="email" data-show-faces="true" data-width="200" data-max-rows="1">Start w/ Facebook</div>