Friday, August 10, 2018

Retrieving Users Gender While Google SignIn using Firebase but not getting all users Gender?

Leave a Comment

Scenario:

I am trying to login with GOOGLE SIGN IN using Firebase Google Login in my android app, my basic need is that to retrieve USER gender while USER sign in my app.

Problem:

Even if I am getting gender of USERS but NOT All USERS, so where is the problem because getting only some users gender is quite strange, so my question is that why i am not getting all users gender while login?

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 !");                     }                 }             }         }     } 

1 Answers

Answers 1

The thing is that gender should be explicitly shared by the user on his Google + account. I suggest you to experiment by yourself.

  1. Go to this page https://developers.google.com/people/api/rest/v1/people/get
  2. Type resourceName = people/me and personFields = genders
  3. Do the request, most likely you'll get the response with only resourceName and etag fields.
  4. Now go to https://plus.google.com/u/0/me page, which will open your G+ page and open Account Settings. Or you can open https://aboutme.google.com/u/0/ page directly.
  5. Click on the icon next to your Gender. This is the visibility setting, so user can choose if he wants to make his gender public, private or share only with circles. I suggest you to try different visibility options and check how it changes response at the step #3.

TL;DR: There is Gender visibility setting on G+, user may have chosen not to share his gender and there is no dedicated profile scope to request during auth for it. This setting is competely private.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment