Question: Get user's location using facebook javascript sdk


Get user's location using facebook javascript sdk

Answers 1
Added at 2016-12-31 01:12

I am using below piece of code to get user's location however, it does not return location

FB.api('/me?fields=picture,location', function(res) {
    console.log(res); // no location

Any idea what am I doing wrong and how can I get user location?


With Graph API Explorer, it does give user location but with above code it does not.


Any idea how can I get location of user using Facebook JavaScript SDK?

Answers to

Get user's location using facebook javascript sdk

nr: #1 dodano: 2017-01-03 09:01

If it works in the API Explorer, but not in your App, there can only be one reason: You did not authorize with the user_location permission. In the API Explorer, you can just select it in a popup. In your code, you have to specifically ask for it with FB.login:

FB.login(function(response) {
    if (response.authResponse) {
        //user just authorized your app
        FB.api('/me?fields=picture,location', function(res) {
}, {scope: 'user_location', return_scopes: true});

Bigger example:

Make sure you try this as user with a role in the App (Admin, Developer, Tester), or it will not even ask for the permission. If you want to go public, you need to go through the review process first:

I have added the return_scopes parameter and a console.log at the beginning of the first callback, debug the response in the chrome dev tools and make sure it includes the user_location permission.

Btw, you get an Access Token in the response too, use the Debugger to check for the authorized permissions:

Source Show
◀ Wstecz