无法在 android 图表 api 中获取性别

Not able to get the gender in graph api in android


这是昨天的工作。


代码:

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();

您的访问令牌需要 user_gender 权限: https://developers.facebook.com/docs/facebook-login/permissions/v3.0#reference-user_gender

在您的 https://developers.facebook.com/ 帐户中,在 App Review 部分下,单击 Start a Submission,select user_gender 来自 LOGIN PERMISSIONS 并完成提交步骤。

facebook批准user_gender权限后,即可使用

再次在您的开发者帐户中点击设置,然后点击高级,然后在升级API版本 将您的应用程序调用的 API 版本更改为 v3.1 并使用以下代码,您将获得用户的性别。

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();
    }