Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 613
splinter123 Avatar asked Apr 12 '15 14:04

splinter123


People also ask

How do I find the email of a Facebook login?

Click the arrow in the upper-right corner of your news feed and select "Account Settings." Your email address is listed under General Account Settings.

How do I get my Facebook API user ID?

The simplest way to get a copy of the User Profile object is to access the /me endpoint: FB. api('/me', function(response) { }); This will this will return the users name and an ID by default.

How do I find Facebook user data?

You can get user data by using the Facebook Graph API if the user has given their permission to share their data and your app has the appropriate permissions to receive this data. This topic shows you how to make a single request for data and how to have a batch of requests in a single request.


3 Answers

Here is the complete code through which i get all the data we need from facebook

 login_facebook_button = (LoginButton) findViewById(R.id.login_facebook_button);
    login_facebook_button.setReadPermissions(Arrays.asList("public_profile", "user_friends", "email", "user_birthday"));
    //LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
    // Callback registration
    login_facebook_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // App code
            Log.e("onSuccess", "--------" + loginResult.getAccessToken());
            Log.e("Token", "--------" + loginResult.getAccessToken().getToken());
            Log.e("Permision", "--------" + loginResult.getRecentlyGrantedPermissions());
            Profile profile = Profile.getCurrentProfile();
            Log.e("ProfileDataNameF", "--" + profile.getFirstName());
            Log.e("ProfileDataNameL", "--" + profile.getLastName());

            Log.e("Image URI", "--" + profile.getLinkUri());

            Log.e("OnGraph", "------------------------");
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            // Application code
                            Log.e("GraphResponse", "-------------" + response.toString());
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,link,gender,birthday,email");
            request.setParameters(parameters);
            request.executeAsync();

        }

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

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

And the response of the call in log is:

{Response:  responseCode: 200, graphObject: {"id":"896337040431723","name":"Aneh Thakur","link":"https:\/\/www.facebook.com\/app_scoped_user_id\/896337040431723\/","gender":"male","birthday":"08\/05\/1992","email":"[email protected]"}, error: null}

Hope this will help you.

  • TrinityTuts
like image 163
Aneh Thakur Avatar answered Sep 21 '22 09:09

Aneh Thakur


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();                        
     }
    
like image 21
Heshan Sandeepa Avatar answered Sep 19 '22 09:09

Heshan Sandeepa


fbLoginButton.setReadPermissions("public_profile", "user_friends", "user_photos", "email", "user_birthday", "public_profile", "contact_email");

"contact_email" is the email permission, i was missing that when i added it to i got the things working.

like image 45
Dhiraj Singh Avatar answered Sep 21 '22 09:09

Dhiraj Singh