How to get the logged user's email address with Graph Request in Facebook Android sdk 4.0

splinter123 picture splinter123 · Apr 12, 2015 · Viewed 20.9k times · Source

I'm trying to get the user's email address once he's logged into my Android app with Facebook (sdk 4.0). I've read many posts asking the same thing but I still couldn't make it work. I simply log the user in with the code

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

Then I make the Graph API request with

LoginManager.getInstance().registerCallback(fbCallbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    System.out.println(loginResult.getAccessToken().toString());

                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(
                                        JSONObject object,
                                        GraphResponse response) {
                                    System.out.println(object.toString());

                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "email");
                    request.setParameters(parameters);
                    request.executeAsync();
                }

                @Override
                public void onCancel() {
                    // App code
                }

                @Override
                public void onError(FacebookException exception) {
                    // App code
                }
            });

The output is just

{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[public_profile, contact_email, user_friends, email, basic_info]}
{"id":"xxxxxxxxxxxxxxx"}

Even if I remove the "fields" part, I get a JSON with a bunch of the user's public info, but never the email field. I'm testing this on my own Facebook account, and I do have an email address associated to it.

Answer

Heshan Sandeepa picture Heshan Sandeepa · Apr 12, 2015

You can get the logged user email as follows , But note that ,

  1. They do not guaranteed you will get an email address read here .

  2. In some cases, though user has provided an email, it will not come through request, if the email is not valid.

    @Override
    public void onSuccess(LoginResult loginResult) {
    
    
    GraphRequest request = GraphRequest.newMeRequest( AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object,GraphResponse response) {
                try {
                    String  email=object.getString("email");
                    Log.d(TAG + "user email ", email);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
                }
    
                });
    
                request.executeAsync();                        
     }