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

0

I'm developing a native Android application and the form of authentication is by Google profile. I would like to get the gender of each user, at signup time, to verify whether it is male or female. Is it possible to do this?

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

    
asked by anonymous 12.07.2017 / 15:13

1 answer

2

The very question you mentioned is exemplified by%% required to catch the user's gender using the Google People API

You basically send the account that logged in through AsyncTask to get a AsyncTask object and then give GoogleSignInAccount that returns the list of user genres to that Person .

No% of% 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();
        }
    }
}
}
    
12.07.2017 / 16:47