skip to Main Content

In Android Studio, using facebook SDK GraphRequest and token, I can get account information, except for gender.

This is my script which I’ve tried:

GraphRequest request = GraphRequest.newMeRequest(token, (object, response) -> {
    Log.e("fbLoginActivity", object.toString());

    String fbId;
    String fbEmail;
    String fbFirst_name;
    String fbLast_name;
    String fbBirthday;
    String fbGender;
    String fbPhoto;

    try {
        String fbId = object.getString("id");
        String fbEmail = object.getString("email");
        String fbFirst_name = object.getString("first_name");
        String fbLast_name = object.getString("last_name");
        String fbBirthday = object.getString("birthday");
        String fbGender = object.getString("gender"); // <-- ERROR, but when I comment this line, it works (without gender data)
        String fbPhoto = "http://graph.facebook.com/" + fbId + "/picture?type=large";
    } catch (JSONException e) {
        Toast.makeText(mContext, "fb graph JSONException error", Toast.LENGTH_SHORT).show();
    }
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,last_name,email,gender,birthday,picture.type(large)");
request.setParameters(parameters);
request.executeAsync();

And this is my read permission on login with faceook:

fbLoginManager.logInWithReadPermissions(
    LoginActivity.this,
    Arrays.asList("email", "public_profile", "user_birthday", "user_photos")
);

The problem is in this line:

String fbGender = object.getString("gender");

So I checked with object.toString() and actually there’s no gender property being outputted in the API:

Log.e("fbLoginActivity", object.toString());

Do you know any working way to obtain facebook user gender?

2

Answers


  1. Chosen as BEST ANSWER

    It seems there's no helpful answer here. Good thing I've solved it myself!

    The problem is on my read permission, I just have to add "user_gender" on my fbLoginManager.logInWithReadPermissions() call. Formerly, I though gender info is included in "public_profile", but no.

    my full read permission code:

    fbLoginManager.logInWithReadPermissions(
            LoginActivity.this,
            Arrays.asList("email", "public_profile", "user_birthday", "user_gender", "user_link", "user_photos")
    );
    

    now my object have gender property, object.toString() result:

    {
        "id": // ...
        "first_name":"Taufik",
        "last_name":"Nur Rahmanda",
        "email":"[email protected]",
        "gender":"male", // <-- YEAH!
        "birthday":"07/26/1993",
        // ... etc
    }
    

    documentation:

    https://developers.facebook.com/docs/facebook-login/permissions/v3.0#permissions


  2. Facebook doesn’t support to get the gender and real Facebook user id by using Graph API.

    You need to find other way around to get the gender, may be you need a library to find the gender based on the first name or something.

    You can take a look at this answer here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search