Collect data from Facebook SDK User

Asked

Viewed 597 times

1

I have the following code to log into Facebook:

 final FacebookCallback facebookCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Toast.makeText(getApplicationContext(),  "Complete!!!", Toast.LENGTH_SHORT).show();
            executeGraphRequest(loginResult.getAccessToken().getUserId());
        }

        @Override
        public void onCancel() {
            Toast.makeText(getApplicationContext(),  "Facebook Cancel!", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(FacebookException error) {
            Toast.makeText(getApplicationContext(),  "Facebook error: "+error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
        }
    };



    login_button = LoginButton.class.cast(findViewById(R.id.login_button));
    login_button.setReadPermissions("public_profile");
    login_button.registerCallback(callbackManager, facebookCallback);

When callback is returned, through the user ID I try to get the information such as name, email and photo using the GraphRequest:

private void executeGraphRequest(final String userId){
    GraphRequest request =new GraphRequest(AccessToken.getCurrentAccessToken(), userId, null, HttpMethod.GET, new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse response) {
            Log.i("FACEBOOK", response.getJSONObject().toString());
            Log.i("FACEBOOK", Profile.getCurrentProfile().toString());
        }
    });

    Bundle parameters = new Bundle();
    parameters.putString("fields", "id, name, email, gender, birthday");
    request.setParameters(parameters);
    request.executeAsync();

}

But the only information he returns is the name, id and gender:

I/FACEBOOK: {"id":"0000000000","name":"Thiago Domacoski","gender":"Le"}

How do I get the user’s email?

1 answer

1


I think you need to put email here:

login_button.setReadPermissions("public_profile");

Thus:

login_button.setReadPermissions("public_profile", "email);

It is also good to check on the facebook site if the email permission is released(should be automated but better to see).

  • 1

    So, Facebook only releases the email if the user confirmed the same!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.