Not able to get the gender in graph api in android

2019-02-25 20:21发布

问题:

  • I am not able to get the gender here in OnCompleted function
  • I am able to get other params like id,name,email
  • Currently app is in Dev mode

This was working yesterday.


Code:

GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                Log.v("LoginActivity", response.toString());
                                try {

                                    AccessToken token = AccessToken.getCurrentAccessToken();
                                    String accessToken = token.getToken().toString();
                                    String expiresAt = token.getExpires().toString();
                                    String id = object.getString("id");
                                    String gender = object.getString("gender");

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                    System.out.println(e.toString());
                                }

                            }
                        });

                Bundle parameters = new Bundle();
                parameters.putString("fields", "id, name, email, gender, location, picture.type(large)");
                request.setParameters(parameters);
                request.executeAsync();

回答1:

In your https://developers.facebook.com/ account, under App Review section, click Start a Submission, select user_gender from LOGIN PERMISSIONS and go through submission steps.

After facebook will approve user_gender permission, you can use it.

Again in your developer account click Settings then Advanced and in Upgrade API Version change the API version your app calls to v3.1 and using below code, you will get the gender of the user.

private void attemptToLogIn() {
   LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_gender"));
   LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        makeGraphRequest(loginResult.getAccessToken());
                    }

                    @Override
                    public void onCancel() {
                    }

                    @Override
                    public void onError(FacebookException error) {

                    }
                });
    }

    public void makeGraphRequest(AccessToken token) {
        GraphRequest request = GraphRequest.newMeRequest(
                token, (object, response) -> {
                    try {
                        String gender = object.getString("gender");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "name,gender");
        request.setParameters(parameters);
        request.executeAsync();
    }


回答2:

Your access token need the user_gender permission: https://developers.facebook.com/docs/facebook-login/permissions/v3.0#reference-user_gender