简体   繁体   中英

Nonexisting field error while accessing user birthday in Facebook Android API

I want to access the user birthday & gender, but I am getting an error when I try to access the birthday.I am using the Facebook SDK version facebook-android-sdk:4.18.0.

It gives me this error:

{Response: responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 100, errorType: OAuthException, errorMessage: (#100) Tried accessing nonexisting field (user_birthday) on node type (User)}}

Here is my code:

public class MainActivity extends AppCompatActivity {
    private CallbackManager callbackManager;
    private TextView info;
    private LoginButton loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

        setContentView(R.layout.activity_main);
        info = (TextView)findViewById(R.id.info);
        loginButton = (LoginButton)findViewById(R.id.login_button);
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                Log.v("LoginActivity", response.toString());

                                // Application code
                                String email = object.optString("email");
                                String birthday = object.optString("birthday");
                                String name = object.optString("name"); // 01/31/1980 format
                                info.setText("email:  " +
                                        email + "\n" +
                                        "birthday " + birthday+
                                "\n name"+name);
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender,user_birthday");
                request.setParameters(parameters);
                request.executeAsync();

            }

            @Override
            public void onCancel() {
                info.setText("Login attempt cancelled.");
            }

            @Override
            public void onError(FacebookException e) {
                info.setText("Login attempt failed.");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

In the parameters you should use birthday instead of user-birthday:

parameters.putString("fields", "id, name, email, gender, birthday");

See the documentation for available fields of user bio: https://developers.facebook.com/docs/graph-api/reference/user/

Some Facebook integrations require approval before public usage.

Without this you will received an ERROR message like this:

Facebook error: “(#100) Tried accessing nonexisting field (user_birthday) on node type (User)”

Until reviewed and approved by Facebook, your app can use only the public user's profile and email.

See next useful recommendations:

Access to the date and month of birth of a person. Access to a person's year of birth depends on their privacy settings and the access token used to request this field.

Please note that most integrations require only the age_range field.

Check If an application requests this permission, Facebook specialists should check how it will be used.

When sending an application for review, you should specify exactly why the age_range permission is not enough in this case.

Next links will be helpful for it:

https://developers.facebook.com/docs/facebook-login/permissions#reference-user_birthday https://developers.facebook.com/docs/facebook-login/review/how-to-submit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM