使用Graph Request android的Facebook用户生日?

时间:2017-02-27 07:47:49

标签: android facebook-graph-api date-of-birth

我正在做的是获取用户详细信息。我能够获得FbId,用户名和使用者,我也希望得到用户生日。请检查以下代码提前感谢: -

GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject user, GraphResponse response) {
                            // Application code
                            if (AccessToken.getCurrentAccessToken() != null) {
                                if (user != null) {
                                    Log4Android.e(FaceBookLogin.this, user.toString());
    //                                Log.e("onCompleted ", "  1= > " + new Gson().toJson(user));
                                    facebookUser = new Gson().fromJson(user.toString(), FacebookUser.class);
                                    try {
    //                                    Log.e("onCompleted ", "  2= > " + new Gson().toJson(user));
                                        Log.e("onCompleted ", "  22= > " + new Gson().toJson(facebookUser));
                                        if (facebookUser.getEmail() != null || !facebookUser.getEmail().equals("")) {
                                            checkUserStatus(context, facebookUser.getId());
                                        } else {
                                            UtilitySingleton.getInstance(context).ShowToast(R.string.email_error);
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        facebookUser = getUsableData(response);
                                        UtilitySingleton.getInstance(context).ShowToast(R.string.email_error);
                                    }
                                }
                            }
                        }
                    });

            Bundle parameters = new Bundle();
    //        parameters.putString("fields", "id,name,email,first_name,last_name");
            parameters.putString("fields", "id,name,email,gender,birthday,first_name,last_name");
            request.setParameters(parameters);
            request.executeAsync();

3 个答案:

答案 0 :(得分:1)

要获得许可,请转到您的Facebook信息中心>应用评论>开始提交>并选择user_birthday

你可以沿着这些代码行做一些事情 -

if (AccessToken.getCurrentAccessToken() != null) {
     GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject user, GraphResponse response) {
                        if (response != null && response.getJSONObject() != null) {

                            try {
                                String birthDate = response.getJSONObject().getString("birthday");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });

        Bundle parameters = new Bundle();

        parameters.putString("fields","id,name,email,gender,birthday,first_name,last_name");
        request.setParameters(parameters);
        request.executeAsync();
}

在发送GraphRequest之前,您应该始终检查访问令牌。

答案 1 :(得分:1)

您要求的权限是什么? 获取用户生日需要权限:“user_birthday”。

答案 2 :(得分:1)

这解决了我的问题: -

String BIRTHDATE = "user_birthday";

     public static final String[] FACEBOOK_PERMISSIONS = {ValueKeys.PUBLIC_PROFILE, ValueKeys.EMAIL, BIRTHDATE};

LoginManager.getInstance().logInWithReadPermissions((Activity) context, Arrays.asList(FACEBOOK_PERMISSIONS));