Get the location of the user (City, State, Country) with facebook login?

-1

How do I get the city, state and country of the user with a facebook login?

I have the following code:

facebook.setReadPermissions("email", "public_profile", "user_birthday","user_location");

private void facebookLogin() {
    mAuth = FirebaseAuth.getInstance();
    mCallbackManager = CallbackManager.Factory.create();
    facebook.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d(TAG, "facebook:onSuccess:" + loginResult);
            GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    Log.d("JSON", "" + response.getJSONObject().toString());
                    try {
                        nome = object.optString("first_name");
                        sobrenome = object.optString("last_name");
                        email = object.optString("email");
                        aniversario = object.optString("user_birthday");
                        idFB = object.optString("id");
                        sexo = object.getString("gender");
                        paisLogin = object.getJSONObject("location").getString("country"); //como fazer a query?
                        cidade = object.getJSONObject("location").getString("city"); //como fazer a query?
                        SaveSharedPreferences.setIdFacebook(getContext(),idFB);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            Bundle parameters = new Bundle();
            parameters.putString("fields","id,first_name,last_name,email,location,gender");
            graphRequest.setParameters(parameters);
            graphRequest.executeAsync();
            AuthCredential credential = FacebookAuthProvider.getCredential(loginResult.getAccessToken().getToken());
            handleFacebookAccessToken(credential);
            //handleFacebookAccessToken(loginResult.getAccessToken());
        }
    
asked by anonymous 01.08.2017 / 02:30

2 answers

-2

This worked for me, adding the getLocationUser method, it looks for the location based on the id, coming from the "location" node in the previous Json:

private void facebookLogin() {
    mAuth = FirebaseAuth.getInstance();
    mCallbackManager = CallbackManager.Factory.create();
    //Login com facebook arrumar um lugar melhor e mais organizado..
    facebook.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d(TAG, "facebook:onSuccess:" + loginResult);
            GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    Log.d("JSON", "" + response.getJSONObject().toString());
                    try {
                        nome = object.optString("first_name");
                        sobrenome = object.optString("last_name");
                        email = object.optString("email");
                        aniversario = object.optString("user_birthday");
                        idFB = object.optString("id");
                        sexo = object.getString("gender");
                        locationID = object.getJSONObject("location").getString("id");
                        getLocationUser(locationID); <<<<-----
                        SaveSharedPreferences.setIdFacebook(getContext(),idFB);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            Bundle parameters = new Bundle();
            parameters.putString("fields","id,first_name,last_name,email,location,gender");
            graphRequest.setParameters(parameters);
            graphRequest.executeAsync();
            AuthCredential credential = FacebookAuthProvider.getCredential(loginResult.getAccessToken().getToken());
            handleFacebookAccessToken(credential);
            //handleFacebookAccessToken(loginResult.getAccessToken());
        }

        @Override
        public void onCancel() {
            Log.d(TAG, "facebook:onCancel");
            // ...
        }

        @Override
        public void onError(FacebookException error) {
            Log.d(TAG, "facebook:onError", error);
            // ...
        }
    });
}

private void getLocationUser(String id) {
    Bundle params = new Bundle();
    params.putString("location", "id");
    new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            id+"/?fields=location",
            params,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    Log.e("Response 2", response + "");
                    try {
                        paisLogin = (String) response.getJSONObject().getJSONObject("location").get("country");
                        cidade = (String) response.getJSONObject().getJSONObject("location").get("city");
                        UF = (String) response.getJSONObject().getJSONObject("location").get("state");
                        Log.e("Location", paisLogin);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
    ).executeAsync();
}
    
01.08.2017 / 20:33
0

There is more than one way to do this. At Location documentation, show some examples and parameters you need use to redeem the location. On Android, you can make an asynchronous request using the GraphRequest class using GET, which would be HttpMethod.GET . See below:

/* make the API call */
new GraphRequest(
    AccessToken.getCurrentAccessToken(),
    "...?fields=location",
    null,
    HttpMethod.GET,
    new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
            /* aqui será exibida o resultado */

            // para resgatar o nome da cidade, basta resgatar o objeto 
            // JSON passando como parâmetro o nome do campo
            String cidade = (String) response.getJSONObject()
                .getJSONObject("location").get("city");
        }
    }
).executeAsync();

See below the list of parameters that can be passed to receive the specific values:

  • city : City
  • city_id : City ID
  • country : Country
  • country_code : Country code
  • latitude : Latitude
  • located_in : Primary location, if you are elsewhere
  • longitude : Longitude
  • name : Name
  • region : Region
  • region_id : Identifying the region
  • state : State
  • street : Street
  • zip : Cep

See more details in the documentation on Location . p>     

01.08.2017 / 21:22