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();
}
}
}
}
Check out this answer: https://stackoverflow.com/a/33906880/7762411
– Grupo CDS Informática