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
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 myfbLoginManager.logInWithReadPermissions()
call. Formerly, I though gender info is included in"public_profile"
, but no.my full read permission code:
now my object have gender property,
object.toString()
result:documentation:
https://developers.facebook.com/docs/facebook-login/permissions/v3.0#permissions
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.