Search code examples
androidfacebook-loginfacebook-android-sdk

'user_birthday' not found in GraphUser - android facebook sdk


Permission was set in Facebook application dashboard to access user_birthday. In application user_birthday permission is listed in permission request.

 request.setPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_work_history"));

In the JSON response after connecting with facebook, the tag firstname, middlename, lastname, email,and id was output but the json has no birthday tag. What have i missed

  Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser graphUser, Response response) {

                    Log.i(TAG, "LoggedInUser:" + graphUser);
                    Log.i(TAG, "LoggedInUser_Id:" + graphUser.getId());
                    Log.i(TAG, "LoggedInUser_First:" + graphUser.getFirstName());
                    Log.i(TAG, "LoggedInUser_Middle:" + graphUser.getMiddleName());
                    Log.i(TAG, "LoggedInUser_Last:" + graphUser.getLastName());
                    Log.i(TAG, "LoggedInUser_Email:" + graphUser.asMap().get("email"));
                    Log.e(TAG, "LoggedInUser_BIRTHDAY:" +  graphUser.asMap().get("user_birthday"));

                    String email = (String) graphUser.asMap().get("email");
                    if(email==null || email.isEmpty())
                        Toast.makeText(LogIn.this, "Failed to get facebook credentials",
                                Toast.LENGTH_LONG).show();
                    else
                        editEmail.setText(email);

                    firstName = graphUser.getFirstName();
                    lastName  = graphUser.getLastName();

                }
            });

Solution

  • use this code

    GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject object,
                                            GraphResponse response) {
                                        String email = object
                                                .optString("email");
                                        String dob = object
                                                .optString("birthday");
                                        // user_id = object.optString("id");
                                        String name = object.optString("name");
                                        Log.d("fb detail", "" + email + " "
                                                + name + " " + dob);
                                        new LoginAsynTask(context).execute(
                                                "social", name, email, dob,
                                                object.optString("first_name"),
                                                object.optString("last_name"));
                                        Toast.makeText(context, "" + email,
                                                Toast.LENGTH_LONG).show();
                                    }
                                });
    
                        Bundle parameters = new Bundle();
                        parameters
                                .putString("fields",
                                        "id,name, email, birthday,gender,first_name,last_name");
                        request.setParameters(parameters);
                        request.executeAsync();