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();
}
}
}
}