JTM JTM - 19 days ago 5
Android Question

Why can't I get a user's first name from a GraphRequest?

I want users to be able to register for my Android app using the Facebook

SDK
. To get their information, I am performing a
GraphRequest
with the permissions "
email
" and "
public_profile
." According to this page, the user's first and last name should be accessible in the "
first_name
" and "
last_name
" fields. However, when I click the button, I get an error saying that there is no value for first_name. The
JSON
array that is returned is along the lines of:

{"name":"First M. Last","id":"1234567890123456"}


I have not submitted my app for review, because it is not yet complete. Could this have something to do with this problem?

Answer

As per official documents your login for accessing public_profile permission should look like :

 LoginManager.getInstance().logInWithReadPermissions(Login.this, Arrays.asList("public_profile", "email"));

To get Profile Info :

 private void getFbDetails(final AccessToken accessToken) {
        GraphRequest request = GraphRequest.newMeRequest(accessToken,
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {

                     //   Toast.makeText(Login.this, object.toString(), Toast.LENGTH_LONG).show();
                        Log.v("FB Details", object.toString());
                        if (object != null) {
                            name_fb = object.optString("name");
                            email_fb = object.optString("email");
                        }
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,first_name, last_name, email,link");
        request.setParameters(parameters);
        request.executeAsync();
    }