Login with facebook in Facebook

Asked

Viewed 3,047 times

2

I am logging in fb only with js using facebook sdk. The login part is doing right, but for some reason only returns me name and id. It is not returning e-mail and other attributes I need. My code

<html>
  <body>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'XXXXXXXX', // Set YOUR APP ID
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true,  // parse XFBML
      version    : 'v2.3'
    });
 
    FB.Event.subscribe('auth.authResponseChange', function(response) {
      if (response.status === 'connected') {
        document.getElementById("message").innerHTML +=  "<br>Connected to Facebook";
        //SUCCESS
      }
      else if (response.status === 'not_authorized') {
        document.getElementById("message").innerHTML +=  "<br>Failed to Connect";
        //FAILED
      }
      else {
        document.getElementById("message").innerHTML +=  "<br>Logged Out";
        //UNKNOWN ERROR
      }
    });
  };

  function Login()
  {
    FB.login(function(response) {
      if (response.authResponse) {
        getUserInfo();
      }
      else {
        console.log('User cancelled login or did not fully authorize.');
      }
    },{ scope: 'email, user_about_me, user_birthday, user_hometown' });
  }

  function getUserInfo() {
	     FB.api('/me?fields=name,email,gender,birthday,link', function(response) {
	        console.log(response);
	        var str = "<b>Name</b> : " + response.name + "<br>";
	        str += "<b>Link: </b>" + response.link + "<br>";
	        str += "<b>Username:</b> " + response.username + "<br>";
	        str += "<b>id: </b>" + response.id + "<br>";
	        str += "<b>Email:</b> " + response.email + "<br>";
	        str += "<input type='button' value='Get Photo' onclick='getPhoto();'/>";
	        str += "<input type='button' value='Logout' onclick='Logout();'/>";
	        document.getElementById("status").innerHTML = str;
	     }, { scope: 'email, user_about_me, user_birthday, user_hometown' });
	}
  
  
  function getPhoto() {
    FB.api('/me/picture?type=normal', function(response) {
      var str="<br/><b>Pic</b> : <img src='"+response.data.url+"'/>";
      document.getElementById("status").innerHTML+=str;
    });
  }

  function Logout() {
    FB.logout(function(){document.location.reload();});
  }

  // 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  = "http://connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);
  }(document));
</script>

<div id="fb-root"></div>
<div align="center">
  <h2 style="font-family:VERDANA">SmartPromos Login FB</h2>
  <div id="status">
     <br/>
    <img src="http://donatetab.firstgiving.com/images/fb-login-button.png" style="cursor:pointer;" onclick="Login()"/>
  </div>

  <br/><br/><br/><br/><br/>
  <div id="message">
  
  </div>
</div>
  </body>
</html>

  • Sometimes the email is not public and this is impossible to show. Just like city and state.

  • Hmmm understood, I think it was not that case, because I’m doing on android tbm e la on mobile it returns a json with email and other attributes.

1 answer

1

I get it.

At the end of } of FB.loginwho is in function Login(), place:

,{ scope: 'email, user_about_me, user_birthday, user_hometown' }

Place user_about_me in the scope.

Look at the way I did a while back, it worked:

Exemplo

It’s a bureaucracy to make use of Facebook Login now. I’m warning you.

You have to have SSL in the hosting.

That is to say, https.

  • put, but not worked only brings name and id

  • I confused function, edited my code. Read again.

  • I changed the way you did in the getUserInfo method but nothing tbm

  • It is the method Login().

  • I made the change in login method and edited my question, but it does not bring.

  • Well, you’re using an old version of the Facebook API, the 2.3, without oAuth. That may be it. But you said that on your android returns the JSON with the data... and can take in the Ministry getUserInfo() the Scope.

  • Yes I think it is the old version anyway, make your code available this way with Canvas I will implement the changes in mine. On android I did different but tbm need to do in the web version and only find things with this old api

  • This canvas there is my freshness, is that I made a bagaça effect on the page, so the canvas.

  • Got it, well I’m going to check in on the login with oAuth, thanks for the help

  • I found the bug, I will edit my question and put the change. What I added was FB.api('/me? Fields=name,email,gender,Birthday,link'

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.