0

I want to retrieve logged in user email. This is what I tied, but not retrieving the email. I could retrieve only id, first name, last name but not the email

LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("LoginActivity", response.toString());

                            // Application code
                            try {
                                String userEmail = object.getString("email");
                                String userId = object.getString("id");
                                emailEditText.setText(userEmail);
                                new SignupTask(SignupActivity.this, userId, userEmail).execute();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "id, first_name, last_name, email"); // Parámetros que pedimos a facebook
            request.setParameters(parameters);
            request.executeAsync();
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {

        }
    });

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

What do I have missed....

senps
  • 584
  • 2
  • 10
  • 30
  • Is it happening for all user or few?? Because most of the FB users are logged in via phone number but not email. So you will get null value for such users – Mohammed Atif Aug 09 '16 at 07:56
  • No, I'm jst testing with the emulator. I logged using email – senps Aug 09 '16 at 07:59
  • Try to use real device and check this http://stackoverflow.com/a/29379794/5515371 – Mujammil Ahamed Aug 09 '16 at 08:16
  • Try this solution, it may help you http://stackoverflow.com/questions/32196682/facebook-android-sdk-4-5-0-get-email-address –  Aug 09 '16 at 08:22

2 Answers2

1
ohk..try this code its working for me...    

LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setBackgroundResource(R.drawable.fb);
        loginButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        loginButton.setReadPermissions(Collections.singletonList("public_profile, email, user_birthday, user_friends"));

        callbackManager = CallbackManager.Factory.create();
        if (netWorkStatus) {
            LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    // App code
                    //Store Facebook data to webservice .
                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject object, GraphResponse response) {
                                    // Application code
                                    try {
                                        // String picture = object.getString("picture");
                                        //JSONObject jobj = new JSONObject(picture);
                                        //JSONObject dataObj = jobj.getJSONObject("data");

                                        name = object.getString("name");
                                        userEmail = object.getString("email");
                                        //String url = dataObj.getString("url").replace("\\", "");

                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }

                                }
                            });

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

                }

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

                }

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

                }

            });
        }
shahid17june
  • 1,441
  • 1
  • 10
  • 15
  • I tried it. But same issue was there. The reason for happen this was. I have used ReadPermission and PublishPermission at once – senps Aug 12 '16 at 05:25
0
CallbackManager callbackManager ;

//in OnCreate  initialize it
callbackManager = CallbackManager.Factory.create();


//after that overide onActivityResult
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

callbackManager.onActivityResult(requestCode, resultCode, data);
        }
shahid17june
  • 1,441
  • 1
  • 10
  • 15