How to get gender from a Google user in an app using Firebase?

Asked

Viewed 85 times

0

I am developing a native Android app and the way of authentication is through the Google profile. I would like to get the gender of each user, at the time of signup, to check whether it is male or female. It is possible to do this?

In this question I learned that Firebase itself does not support access to gender, so I believe it should be direct on Android.

  • Check out this answer: https://stackoverflow.com/a/33906880/7762411

1 answer

2


In the very question you mentioned is exemplified to AsyncTask necessary to catch the user’s gender using the Google People API

Basically you send for this AsyncTask the account you logged in to GoogleSignInAccount to obtain an object of Person and then give a profile.getGenders() which returns the user’s gender list.

In the onPostExecute you iterate the list to get the real value of the genre.

class GetGendersTask extends AsyncTask<GoogleSignInAccount, Void, List<Gender>> {
    @Override
    protected List<Gender> doInBackground(GoogleSignInAccount... googleSignInAccounts) {
        List<Gender> genderList = new ArrayList<>();
        try {
            HttpTransport httpTransport = new NetHttpTransport();
            JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();

            //Redireciona a URL para aplicações web.
            // Pode ser deixada em branco.
            String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";

            // Troca o código de autorização pelo token de acesso
            GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
                    httpTransport,
                    jsonFactory,
                    getApplicationContext().getString(R.string.server_client_id),
                    getApplicationContext().getString(R.string.server_client_secret),
                    googleSignInAccounts[0].getServerAuthCode(),
                    redirectUrl
            ).execute();

            GoogleCredential credential = new GoogleCredential.Builder()
                    .setClientSecrets(
                        getApplicationContext().getString(R.string.server_client_id), 
                        getApplicationContext().getString(R.string.server_client_secret)
                    )
                    .setTransport(httpTransport)
                    .setJsonFactory(jsonFactory)
                    .build();

            credential.setFromTokenResponse(tokenResponse);

            People peopleService = new People.Builder(httpTransport, jsonFactory, credential)
                    .setApplicationName("My Application Name")
                    .build();

            // Obtem o perfil do usuário
            Person profile = peopleService.people().get("people/me").execute();
            genderList.addAll(profile.getGenders());
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return genderList;
    }

    @Override
    protected void onPostExecute(List<Gender> genders) {
        super.onPostExecute(genders);
        // Itera entre a lista de Gêneros para
        // obter o valor do gênero (masculino, feminino, outro)
        for (Gender gender : genders) {
            String genderValue = gender.getValue();
        }
    }
}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.