【问题标题】:How to get Facebook account's gender and age range in Graph 3.0 API android如何在 Graph 3.0 API android 中获取 Facebook 帐户的性别和年龄范围
【发布时间】:2023-03-03 12:46:01
【问题描述】:

自从 Facebook 在提供用户数据方面变得更加安全以来,情况发生了翻天覆地的变化。在使用 Bundles 和 Graph Request 之前,我可以使用 Graph 2.12 在我的管理员帐户中获取性别和年龄范围。

  //Log in Facebook
private void signInFacebook() {

    //Request a read permission of user's info from Facebook
    //Data provided by Facebook will be used for Firebase FireStore
    LoginManager.getInstance().logInWithReadPermissions(LogIn.this, Arrays.asList("email", "public_profile"));

    LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(final LoginResult loginResult) {

            mStateOfSuccess = false;
            //Dismiss any snackbar first before showing a new one
            mSnackBar.dismiss();
            mSnackBar.show();

            Log.d(TAG, "facebook:onSuccess:" + loginResult);

            //Bundle is use for passing data as K/V pair like a Map
            Bundle bundle=new Bundle();
            //Fields is the key of bundle with values that matched the proper Permissions Reference provided by Facebook
            bundle.putString("fields","id, email, first_name, last_name, gender, age_range");

            //Graph API to access the data of user's facebook account
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("Login Success", response.toString());

                            //For safety measure enclose the request with try and catch
                            try {

                                //The get() or getString() key should be included in Bundle otherwise it won't work properly
                                //If not then error dialog will be called

                                //First re-initialize jSON object to a new Contructor with parameter that is equal to a jSON format age range
                                JSONObject ageRange  = new JSONObject(object.getString("age_range"));

                                        //Log in using Facebook with Firebase
                                        loginToFirebaseUsingFacebook(loginResult.getAccessToken()
                                                ,object.getString("first_name")
                                                ,object.getString("last_name")
                                                //Then get again get a string from object itself for the minimum age range
                                                //The idea is that we need to get minimum age only written im string format
                                                //not the whole age range data that is written in jSOM format
                                                ,ageRange.getString("min")
                                                ,object.getString("gender")
                                                ,object.getString("email")
                                        );

                            }
                            //If no data has been retrieve throw some error
                            catch (JSONException e) {
                               ErrorDialog(e.getMessage(),"facebookAuth");
                            }

                        }
                    });


            //Set the bundle's data as Graph's object data
            request.setParameters(bundle);

            //Execute this Graph request asynchronously
            request.executeAsync();

        }

        @Override
        public void onCancel() {
            Log.d(TAG, "facebook:onCancel");
            ErrorDialog("Request has canceled.","facebookAuth");
        }

        @Override
        public void onError(FacebookException error) {
            Log.d(TAG, "facebook:onError", error);
            ErrorDialog(String.valueOf(error),"facebookAuth");
        }
    });


}

但现在一切都发生了变化,即使访问我自己的帐户(管理员)也无法提供我需要的数据。根据他们的文档enter image description here

【问题讨论】:

  • 专门为这两个字段添加了两个新权限,您需要先询问用户,然后您的应用才能立即读取此数据。

标签: android facebook-graph-api


【解决方案1】:

我想出了如何在 Graph 3.0 中做到这一点,您需要做的就是将这个 user_age_rangeuser_gender 包含到权限中,当 使用管理员帐户登录时,它现在将按预期工作 如果应用尚未通过 Facebook 验证。

  //Log in Facebook
private void signInFacebook() {

    //Request a read permission of user's info from Facebook
    //Data provided by Facebook will be used for Firebase FireStore
    //For more updates about Read Permissions - User Attributes: ''https://developers.facebook.com/docs/facebook-login/permissions/''
    LoginManager.getInstance().logInWithReadPermissions(LogIn.this, Arrays.asList("email","public_profile","user_gender","user_age_range"));

    LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(final LoginResult loginResult) {

            mStateOfSuccess = false;
            //Dismiss any snackbar first before showing a new one
            mSnackBar.dismiss();
            mSnackBar.show();

            Log.d(TAG, "facebook:onSuccess:" + loginResult);

            //Bundle is use for passing data as K/V pair like a Map
            Bundle bundle=new Bundle();
            //Fields is the key of bundle with values that matched the proper Permissions Reference provided by Facebook
            bundle.putString("fields","id,email,first_name,last_name,gender,age_range");

            //Graph API to access the data of user's facebook account
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("Login Success", response.toString());

                            //For safety measure enclose the request with a try and catch
                            try {

                                //The get() or getString() key should be included in Bundle otherwise it won't work properly
                                //If not then error dialog will be called

                                //First re-initialize jSON object to a new Contructor with parameter that is equal to a jSON format age range
                                JSONObject ageRange  = new JSONObject(object.getString(getResources().getString(R.string.age_range)));

                                        //Log in using Facebook with Firebase
                                        loginToFirebaseUsingFacebook(loginResult.getAccessToken()
                                                ,object.getString(getResources().getString(R.string.fname))
                                                ,object.getString(getResources().getString(R.string.lname))
                                                //Then get again get a string from object itself for the minimum age range
                                                //The idea is that we need to get minimum age only written im string format
                                                //not the whole age range data that is written in jSON format
                                                ,ageRange.getString(getResources().getString(R.string.minimum))
                                                ,object.getString(getResources().getString(R.string.gender).toLowerCase())
                                                ,object.getString(getResources().getString(R.string.mail))
                                        );

                            }
                            //If no data has been retrieve throw some error
                            catch (JSONException e) {

                                //Log in using Facebook with Firebase
                                try {
                                    loginToFirebaseUsingFacebook(loginResult.getAccessToken()
                                            ,object.getString(getResources().getString(R.string.fname))
                                            ,object.getString(getResources().getString(R.string.lname))
                                            //Exception occurs if age and gender has no value
                                            ,null
                                            ,null
                                            ,object.getString(getResources().getString(R.string.mail))
                                    );
                                } catch (JSONException e1) {
                                    ErrorDialog(e.getMessage(),"facebookAuth");
                                }

                            }

                        }
                    });


            //Set the bundle's data as Graph's object data
            request.setParameters(bundle);

            //Execute this Graph request asynchronously
            request.executeAsync();

        }

        @Override
        public void onCancel() {
            Log.d(TAG, "facebook:onCancel");
            ErrorDialog("Request has canceled.","facebookAuth");
        }

        @Override
        public void onError(FacebookException error) {
            Log.d(TAG, "facebook:onError", error);
            ErrorDialog(String.valueOf(error),"facebookAuth");
        }
    });


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多