androidfacebookfacebook-android-sdk

How to get email id From Android Facebook SDK 4.6.0?


Here is my code for getting user information after facebook login. I am trying to get emailid from user I am getting Name , id , but not getting the emailid .I have tried with the Login Button and Login Manager class both giving the same results . Now how to get email id from response :

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(final LoginResult loginResult) {
                    new GraphRequest(AccessToken.getCurrentAccessToken(),
                            "/me", null , HttpMethod.GET,
                            new GraphRequest.Callback() {
                                public void onCompleted(
                                        GraphResponse response) {
                                         handle the result 

                                    if (response !=null  )
                                    {
                                        //GET USER INFORMATION
                                        Toast.makeText(getApplicationContext(),"Hello in JSON",Toast.LENGTH_LONG).show();
                                        JSONObject json = response.getJSONObject();
                                        //JSONArray jsona = response.getJSONArray();
                                        Log.i(TAG,"" +json);
                                       // Log.i(TAG,"" +json);
                                        String email = json.optString("email");
                                       // String email = json.optString("email");
                                        //Log.i("email", "" + email);
                                        Log.i(TAG,"" +email);
                                        String fullName = json.optString("name");
                                        String location = json.optString("location");
                                        String accessToken = loginResult.getAccessToken().getToken();
                                        String user_id = json.optString("id");
                                        Log.i(TAG,"" +json);
                                        Toast.makeText(getApplicationContext(),fullName,Toast.LENGTH_LONG).show();
                                        Toast.makeText(getApplicationContext(),email,Toast.LENGTH_LONG).show();
                                        Toast.makeText(getApplicationContext(),user_id,Toast.LENGTH_LONG).show();
                                        //int type = 1;
                                       // String lastUpdate = json.getString("updated_time");

                                      //  Log.i("email", "" + email);
                                      //  Log.i("Message", "HELLO");
                                        Log.i("Name", "" + fullName);
                                        Log.i("ID", "" + user_id);


                                    }
                                }

                            }).executeAsync();

Thanks..!!


Solution

  • LoginManager.getInstance().logInWithReadPermissions(WelcomeActivity1.this, (Arrays.asList("public_profile", "user_friends","user_birthday","user_about_me","email")));
    
    String email;
    
    
    LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
     @Override
      public void onSuccess(LoginResult loginResult) {
      Log.d("tag","FF fb onSuccess");
      GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject object,GraphResponse response) {
         try {
               String[] splited ;
               JSONObject obj =  object.getJSONObject("picture").getJSONObject("data");
    
                                  if (object.has("email"))
                                  {
                                     email =  object.getString("email");
                                  }
                                  else
                                  {
                                      email = "";
                                  }
    
    
    
                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
    
    
                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,link,birthday,picture,email,gender");
                    request.setParameters(parameters);
                    request.executeAsync();
    
    
                }
    
                @Override
                public void onCancel() {
                    Log.d("tag","fb onCancel");
                     // App code
                }
    
    
    
    @Override
                public void onError(FacebookException exception) {
                    Log.d("tag","fb onError");
                     // App code   
                }
    });