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

耗尽温柔 提交于 2019-11-28 20:43:26
Heshan Sandeepa

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();                        
     }
    

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":"anehkumar@gmail.com"}, error: null}

Hope this will help you.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!