无法在Android的图形API中获取性别 [英] Not able to get the gender in graph api in android

查看:68
本文介绍了无法在Android的图形API中获取性别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我无法在OnCompleted函数中获得性别
  • 我能够获得其他参数,例如idnameemail
  • 当前应用处于开发模式
  • 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

昨天工作了.

代码:

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

推荐答案

在您的 https://developers.facebook中. com/帐户,在 App Review 部分下,单击开始提交,然后从登录权限中选择 user_gender >,然后执行提交步骤.

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.

facebook批准 user_gender 权限后,您可以使用它.

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

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

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

这篇关于无法在Android的图形API中获取性别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆