Rishi
Rishi

Reputation: 151

How to get user Gender and birthday from facebook in android

I am Trying to get gender and birthday of current login user from Facebook, but I always getting (id, email, name) I have search everything related to this but I didn't get exactly that for I am looking. below code, I have Trying that's not working.

facebookLoginButton  = (LoginButton)view .findViewById(R.id.fragment_login_facebook);
        facebookLoginButton.setFragment(this);
callbackManager = CallbackManager.Factory.create();

facebookLoginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_friends"));
        callbackManager = CallbackManager.Factory.create();

    facebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        private ProfileTracker mProfileTracker;
        @Override
        public void onSuccess(LoginResult loginResult) {


            String accessTocken = loginResult.getAccessToken().getToken().toString();

            Log.v("TAG", "Access Token " + loginResult.getAccessToken().getToken());
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {

                            try {
                                String email = object.getString("birthday");
                                String name = object.getString("gender");
                                Toast.makeText(getActivity().getApplicationContext(),email + " " + name ,Toast.LENGTH_SHORT).show();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });

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



            if (com.facebook.Profile.getCurrentProfile() == null){
                mProfileTracker = new ProfileTracker() {
                    @Override
                    protected void onCurrentProfileChanged(com.facebook.Profile oldProfile, com.facebook.Profile currentProfile) {
                        mProfileTracker.stopTracking();
                    }
                };
                mProfileTracker.startTracking();
            } else {
                com.facebook.Profile profile = com.facebook.Profile.getCurrentProfile();
            }

            try {
                URL image_value = new URL("http://graph.facebook.com/" + loginResult.getAccessToken().getUserId() + "/picture?type=large");
                Log.v(Constants.TAG, image_value + "");
            } catch (Exception e) {
            }
        }

        @Override
        public void onCancel() {
            Log.e("TAG", "wrong");
        }

        @Override
        public void onError(FacebookException error) {
            Log.e("TAG", "wrong");
        }
    });

Where facebookLoginButton is LoginButton and callbackManager is CallbackManager Please help me thanks in advance.

Upvotes: 5

Views: 4412

Answers (1)

Khaled Lela
Khaled Lela

Reputation: 8139

  • Facebook gender & birthday needs app review.

Apps that Require App Review

Your app requires review if it uses the following functionality:

Facebook Login and also asks for a person's birthday, location, hometown, gender, age range, or link to profile

  • Check the link for more details.

Upvotes: 8

Related Questions