检索用户性别虽然Google使用Firebase登录但未获得所有用户性别?

kdb*_*lue 5 android google-signin

场景:

我想在我的Android应用程序中GOOGLE SIGN IN使用登录Firebase Google Login,我的基本需求是在我的应用程序中登录时检索USER性别USER.

问题:

即使我得到的性别USERS,但不是全部 USERS,那么,是因为越来越问题只是一些用户的性别是很奇怪的,所以我的问题是,为什么我没有得到所有用户的性别,同时登录?

public void performFirebaseLogin(GoogleSignInAccount acct, final Context context, final LoginSPrefRepositoryImpl loginSPrefRepositoryImpl) {
        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        final GoogleAccountCredential googleCredential = GoogleAccountCredential.usingOAuth2(
                    context, Collections.singleton(Scopes.PROFILE));
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener((Activity) context, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {

                            FirebaseUser user = mAuth.getCurrentUser();
                            if (user != null) {
                            //here i am calling profile detail of user
                                new GetProfileDetails(user, googleCredential,loginSPrefRepositoryImpl).execute();
                            } else {
                                    onLoginListener.onFailure("Login Failed");
                            }
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                        }
                    }
                });
    }


//here i am retrieving user's information  

class GetProfileDetails extends AsyncTask<Void, Void, Person> {

        private PeopleService ps;
        private int authError = -1;
        private FirebaseUser user;
        private LoginSPrefRepositoryImpl loginSPrefRepositoryImpl;

        GetProfileDetails(FirebaseUser user, GoogleAccountCredential credential, LoginSPrefRepositoryImpl loginSPrefRepositoryImpl) {

            this.loginSPrefRepositoryImpl = loginSPrefRepositoryImpl;
            this.user = user;
            credential.setSelectedAccount(
                    new Account(user.getEmail(), "com.google"));

            HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
            JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

            ps = new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                    .setApplicationName("Google Sign In Quickstart")
                    .build();
        }

        @Override
        protected Person doInBackground(Void... params) {
            Person meProfile = null;
            try {
                meProfile = ps
                        .people()
                        .get("people/me")
                        .setPersonFields("genders")
                        .execute();
            } catch (UserRecoverableAuthIOException e) {
                e.printStackTrace();
                authError = 0;
            } catch (GoogleJsonResponseException e) {
                e.printStackTrace();
                authError = 1;
            } catch (IOException e) {
                e.printStackTrace();
                authError = 2;
            }
            return meProfile;
        }

        @Override
        protected void onPostExecute(Person meProfile) {
            //mainAct.printBasic();
            if (authError == 0) { //app has been revoke, re-authenticated required.
                //mainAct.reqPerm();
            } else if (authError == 1) {
                Log.w(TAG, "People API might not enable at" +
                        " https://console.developers.google.com/apis/library/people.googleapis.com/?project=<project name>");
            } else if (authError == 2) {
                Log.w(TAG, "API io error");
            } else {
                if (meProfile != null) {
                    // Log.d("kkkk", "" + meProfile.getGenders());
                    JSONArray jsonArray = new JSONArray(meProfile.getGenders());
                    try {
                        JSONObject jsonObject = jsonArray.getJSONObject(0);
                        String gender = jsonObject.getString("formattedValue");
                        loginSPrefRepositoryImpl.isLoggedIn();
                        loginSPrefRepositoryImpl.setGender(gender);
                        onLoginListener.onSuccess(user);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        onLoginListener.onFailure("Something went wrong !");
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

nik*_*kis 2

问题是gender用户应该在他的Google +帐户上明确共享。我建议你自己尝试一下。

  1. 转到此页面https://developers.google.com/people/api/rest/v1/people/get
  2. 类型resourceName=people/mepersonFields=genders
  3. 执行请求,很可能您会得到仅包含resourceNameetag字段的响应。
  4. 现在转到https://plus.google.com/u/0/me页面,这将打开您的 G+ 页面并打开帐户设置。或者您可以直接打开https://aboutme.google.com/u/0/页面。
  5. 单击您的性别旁边的图标。这是可见性设置,因此用户可以选择是否要将自己的性别公开、私有或仅与圈子分享。我建议您尝试不同的可见性选项,并在第 3 步检查它如何改变响应。

TL;DR:G+ 上有Gender可见性设置,用户可能选择不分享他的性别,并且在身份验证期间没有专门的配置文件范围可供请求。此设置完全私密。