How to get user location and user hometown in facebook api?

siva picture siva · Nov 8, 2013 · Viewed 28.6k times · Source

I am already using this script but I didn't get hometown and location when I use API the hometown and location is undefined message appears please give example code and how to get user mobile number in Facebook API:

function login() {
FB.login(function(response) {
    if (response.authResponse) {
        // connected
    testAPI();
    } else {
        // cancelled
    }
}, { scope: 'email,user_birthday,user_location,user_hometown' });
}
function testAPI() {
console.log('Welcome!  Fetching your information.... ');
FB.api('/me', function(response) {
    console.log('Good to see you, ' + response.name + '.' + ' Email: ' +     response.email + ' Facebook ID: ' + response.id);
  //console.log('Good to see you, ' + response.name + '.');
  var userfirstName=response.first_name;
  var lastName=response.last_name;
  var useremail=response.email;
  var usersex=response.gender;
  var userbithday=response.birthday;
  var hometown= response.hometown.name;
  var location= response.location.name;

    alert(hometown);

});

}

 <fb:login-button show-faces="false" width="200" max-rows="1"     scope="email,user_birthday,user_location,user_hometown" onclick="testAPI();" onlogin="Log.info('onlogin callback')">
     Sign Up with Facebook
    </fb:login-button>

but can't get hometown and location, please help give any example.

Answer

Sahil Mittal picture Sahil Mittal · Nov 8, 2013

You are using wrong permissions, try this:

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

Another thing, to access the hometown use this:

var hometown= response.hometown.name;

Also, instead of /me; mention explicitly the fields you want using the fields parameter; just like: /me?fields=hometown

Graph API Explorer