Aloke Desai
Aloke Desai

Reputation: 1057

Getting Email through Facebook Javascript SDK

I'm just starting to play around with the facebook API but I can't seem to get the API to alert my email using the FB.User object. I can get it to print out my userID and my name, but it returns as undefined if I try to print out my email or my education. Here's my code:

<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>

Upvotes: 0

Views: 6397

Answers (1)

Manoj Yadav
Manoj Yadav

Reputation: 6612

You need email permission to access it

Change this line

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

As below and check (Added 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>

Upvotes: 4

Related Questions