Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 971
siva Avatar asked Nov 08 '13 06:11

siva


1 Answers

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

like image 154
Sahil Mittal Avatar answered Oct 14 '22 15:10

Sahil Mittal