Question: Get user's location using facebook javascript sdk

Question

Get user's location using facebook javascript sdk

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

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?

UPDATE

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

UPDATE 2

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

Answers
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) {
    console.log(response);
    if (response.authResponse) {
        //user just authorized your app
        FB.api('/me?fields=picture,location', function(res) {
            console.log(res);
        });
    }
}, {scope: 'user_location', return_scopes: true});

Bigger example: http://www.devils-heaven.com/facebook-javascript-sdk-login/

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: https://developers.facebook.com/docs/facebook-login/review

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: https://developers.facebook.com/tools/debug/accesstoken/

Source Show
◀ Wstecz