0

I used the example from the facebook documentation but after granting facebook permission to connect I cannot get any user info from the user object. The user is defined but all its attributes are null. Here is my code:

<html>
    <head>
      <title>My Facebook Login Page</title>
    </head>
    <body>

      <div id="fb-root"></div>
      <script>
        window.fbAsyncInit = function() {
          FB.init({
            appId      : '161718123880158', // App ID
            channelUrl : '', // Channel File
            status     : true, // check login status
            cookie     : true, // enable cookies to allow the server to access the session
            xfbml      : true,  // parse XFBML
            logging     : true
          });
          FB.api('/me', function(user) {
            if (user) {
              var image = document.getElementById('image');
              image.src = 'http://graph.facebook.com/' + user.id + '/picture';
              var name = document.getElementById('name');
              name.innerHTML = user.name
            }
          });
        };
        // Load the SDK Asynchronously
        (function(d){
           var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
           if (d.getElementById(id)) {return;}
           js = d.createElement('script'); js.id = id; js.async = true;
           js.src = "//connect.facebook.net/en_US/all.js";
           ref.parentNode.insertBefore(js, ref);
         }(document));
      </script>
<div class="fb-login-button" scope="email,user_checkins">Login with Facebook</div>
      <div align="center">
        <img id="image"/>
        <div id="name"></div>
      </div>

    </body>
 </html>

The output is undef for name and empty image

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

It's because you haven't got an active access token. If you do a put a console.log(user) and examine the response it shows the error.

  1. What is the 'Website with Facebook login' field set to in your app settings? It needs to match the URL that you are developing on.
  2. Try wrapping the FB.api call in a FB.getLoginStatus callback function, like this:

    FB.getLoginStatus(function() {
       FB.api('/me', function(user) {
          //your code here
       });
     })
    
Joe T
  • 794
  • 4
  • 6
  • Thank you that did the trick. I'm not 100% sure which one fixed the issue. I updated the "Website with Facebook Login" to the exact url I was testing with but without adding FB.getLoginStatus in #2 it still does not work. Will the login work for all urls under the main domain? For example if we indicate "example.com" in the settings, will "example.com/mypage" work as well? – user1532118 Jul 17 '12 at 16:08