0

I want to retrieve facebook user email, user name and last name. I retrieved name and last name. But I don't know how to retrieve email.

I have this code:

fbloginButton.registerCallback(callbackManager, new  
FacebookCallback<LoginResult>()
    {
        @Override
        public void onSuccess(LoginResult loginResult)
        {
          Intent intent = new  
          Intent(LoginTienda.this,Comprador_registrado.class);
          startActivity(intent);
        }

        @Override
        public void onCancel()
        {

        }

        @Override
        public void onError(FacebookException error)
        {
            String msgerror = error.getMessage();
            Toast.makeText(LoginTienda.this, msgerror, 
            Toast.LENGTH_SHORT).show();
        }
    });

    profileTracker = new ProfileTracker()
    {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile 
        currentProfile)
        {

            if(currentProfile == null)
            {

                Intent i = new 
                Intent(LoginTienda.this,MainActivity.class);
                startActivity(i);
            }
            else
            {
                //get the username and last name
                nombrefb = currentProfile.getFirstName();
                apellidofb = currentProfile.getLastName();

                Toast.makeText(getApplicationContext(),
                apellidofb,Toast.LENGTH_LONG).show();
            }
        }
    };  

How can I retrieve the user mail? I can't find a method in Profile class as getEmail. I find getName(), getFirstName() or getLastName() in order to get the first name and the last name.

Thanks.

I also have this code:

public void onSuccess(LoginResult loginResult)
        {
                GraphRequest graphRequest = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
                    {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response)
                        {
                            String rsp = response.toString();
                            Toast.makeText(getApplicationContext(),rsp,Toast.LENGTH_LONG).show();
                            try
                            {
                                correofb = object.getString("email").toString();
                            }
                            catch (JSONException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    });

            Bundle parametros = new Bundle();
            parametros.putString("campos","email");
            graphRequest.setParameters(parametros);
            graphRequest.executeAsync();
        }

And after running it what I watch is id and name but no email. What's wrong?

axmug
  • 476
  • 2
  • 10
  • 26

1 Answers1

0

Call below method from registerCallback onSuccess

private void makeGraphRequest(LoginResult loginResult) {
    GraphRequest request = GraphRequest.newMeRequest(
            loginResult.getAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(
                        JSONObject object,
                        GraphResponse response) {

                    Log.d("response: ", response + "");
                    try {

                        String id =object.getString("id").toString();
                        String email = object.getString("email").toString();
                        String name = object.getString("name").toString();
                        //String imageurl = "https://graph.facebook.com/" + id + "/picture?type=large";
                        //object.get("gender");
                    } catch (Exception e) {
                        e.printStackTrace();

                    }

                }

            });

    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,email,gender, birthday");
    request.setParameters(parameters);
    request.executeAsync();
}
Munir
  • 2,548
  • 1
  • 11
  • 20
  • Thanks for answering. I added more details on explanation of the problem in the post above. I don't know what's wrong. – axmug Oct 27 '17 at 19:38
  • I show your code why you change bundle parameters key "fields" to "campos"? just change that key with "fields" and it's work. – Munir Oct 28 '17 at 13:30