-1

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..!!

Sidh
  • 7
  • 1
  • 7
  • 1
    Will you please add more information about `Where you stuck ?` – AndiGeeky Oct 15 '15 at 04:33
  • @AndiGeeky Hii I am using Facebook sdk 4.6.0 and API version 2.5 I am in the above code i am getting the name, id but not getting the email id using json object.please any suugestion?. – Sidh Oct 15 '15 at 04:42
  • Debug your app and check what is in `GraphResponse response`..!! – AndiGeeky Oct 15 '15 at 04:44
  • i am getting this in Graphresponse response {Response: responseCode: 200, graphObject: {"id":"971469282899927","name":"Siddharth Choudhary"}, error: null} The last one that is email is null and showing error!! – Sidh Oct 15 '15 at 05:12
  • Are you logging in using same account which has developer account ?? – AndiGeeky Oct 15 '15 at 05:13
  • Yes I am logging to the same account is there any issue ..??? – Sidh Oct 15 '15 at 06:59
  • @ siddharth : For same account no issue.. Different one needs `publish_actions` .!! – AndiGeeky Oct 15 '15 at 07:07
  • @AndiGeeky That's not correct! – Tobi Oct 15 '15 at 07:16
  • @Tobi : Oh.. I am sorry. `publish_actions` need for only publish content on behalf of user..!! `Login` does not require that permission.>!! – AndiGeeky Oct 15 '15 at 07:19
  • @AndiGeeky He needs the `email` permission. – Tobi Oct 15 '15 at 07:21
  • @AndiGeeky I have given email permission too in with this.. LoginManager.getInstance().logInWithReadPermissions(this,Arrays.asList("user_photos", "email", "user_location")); and logging to my account.but its returning null.!! – Sidh Oct 15 '15 at 07:52

3 Answers3

1
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   
            }
});
Tobi
  • 31,405
  • 8
  • 58
  • 90
Rotomac17
  • 239
  • 3
  • 16
  • Use the above code, to retrieve email id from Facebook account – Rotomac17 Oct 15 '15 at 08:50
  • Your code works for me .Thanks for the post..!! but how ?? Bundle parameters = new Bundle(); parameters.putString("fields", "id,name,link,birthday,picture,email,gender"); request.setParameters(parameters); request.executeAsync(); How it execute because here i need to pass it on the other variable ? – Sidh Oct 15 '15 at 10:23
  • Can I knw what values you need to pass on to the other variable? – Rotomac17 Oct 16 '15 at 10:51
  • I need to pass id,fname,lname,email and also mobile number but i have read that mobile number is not allowed in the API version ? – Sidh Oct 19 '15 at 05:57
1

this way to get all part of Facebook login...

    locationMangaer = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    loginButton = (LoginButton)findViewById(R.id.login_button);
    loginButton.setReadPermissions(Arrays.asList("email", "user_photos", "public_profile", "user_friends"));

    callbackManager= CallbackManager.Factory.create();
    loginButton.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) {
                            // Application code
                            Log.e("LoginActivity", response.toString() + "    " + object.toString());

                            try {
                                String name=object.getString("name");
                                String email=object.getString("email");
                                String id=object.getString("id");
                                String gender=object.getString("gender");




                                Log.e("LoginActivity", name + "    " + email + "   " + id + "   " + gender);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });



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

        }

        @Override
        public void onCancel() {
            // App code
            Log.e("LOGIN Cancle", "LOGIN Cancle");
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
            Log.e("LOGIN Cancle", exception.getMessage());
        }
    });
Abhinav singh
  • 1,448
  • 1
  • 14
  • 31
0

Starting with Graph API v2.4, you need to specifiy each field which you want to have returned in your query.

new GraphRequest(AccessToken.getCurrentAccessToken(),
  "/me", null , HttpMethod.GET,
  new GraphRequest.Callback() {
    ...

needs to become

new GraphRequest(AccessToken.getCurrentAccessToken(),
  "/me?fields=id,name,email", null , HttpMethod.GET,
  new GraphRequest.Callback() {
    ...

This is very well documented, and there are dozens of similar questions here on SO. Please refer to the docs first before posting a question.

Keep in mind that you need the appropriate permissions to be able to request user details. In this case it's email

See:

Tobi
  • 31,405
  • 8
  • 58
  • 90
  • .I replace the code with yours one but its still same output is coming no change is there is something other i need to change..?? – Sidh Oct 15 '15 at 09:13
  • Have you got the `email` permission or not? – Tobi Oct 15 '15 at 09:37
  • I have given permission above in Login Manager .!! Yes i got the Email permission ..!!!! email return null .. LoginManager.getInstance().logInWithReadPermissions(facebooklogin.this,Arrays.asList("email")); – Sidh Oct 15 '15 at 10:00