Я заглянул в google people API
на их официальной странице документации.
https://developers.google.com/people/quickstart/java#step_4_run_the_sample
Несмотря на выполнение всех необходимых шагов, API
не работает.
Я наткнулся на пример ниже на GitHub, который тоже не работает. https://github.com/Suleiman19/People-API-App
также я проверяю пример Google
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/Desktop;
Ниже код работает. может получить доступ к API людей
private static class GetUserInfoTask extends AsyncTask<String, Void, Void> {
private final Context context;
private ISignIn iSignIn;
private HttpTransport httpTransport = new NetHttpTransport();
private JacksonFactory jsonFactory = new JacksonFactory();
private String personEmail;
private String personLastName;
private String personFirstName;
private String image;
private String genderString;
private String aboutMeLocal;
private String phoneLocal;
private long birthdayLocal;
public GetUserInfoTask(Context context, ISignIn iSignIn) {
this.context = context;
this.iSignIn = iSignIn;
}
@Override
protected Void doInBackground(String... params) {
personEmail = params[0];
personLastName = params[1];
personFirstName = params[2];
image = params[3];
Person userProfile = null;
Collection<String> scopes = new ArrayList<>();
scopes.add(Scopes.PROFILE);
GoogleAccountCredential mCredential =
GoogleAccountCredential.usingOAuth2(context, scopes);
//mCredential.setSelectedAccount(new Account(personEmail, context.getString(R.string.account_type)));
mCredential.setSelectedAccountName(personEmail);
People service = new People.Builder(httpTransport, jsonFactory, mCredential)
.setApplicationName(context.getString(R.string.app_name)) // your app name
.build();
// Get info. on user
try {
userProfile = service.people().get("people/me").setRequestMaskIncludeField("person.biographies,person.birthdays,person.genders,person.phone_numbers").execute();
} catch (IOException e) {
FirebaseApp.initializeApp(context);
FirebaseCrash.report(e);
LogUtils.e(TAG, e.getMessage());
}
// Get whatever you want
if (userProfile != null) {
// Gender
List<Gender> genders = userProfile.getGenders();
if (genders != null && genders.size() > 0) {
Gender gender = genders.get(0);
if (gender != null) {
// save mGender
genderString = gender.getValue();
LogUtils.d(TAG, "mGender : " + gender.getValue());
}
}
// BirthDay
List<Birthday> birthdays = userProfile.getBirthdays();
if (birthdays != null && birthdays.size() > 0) {
Birthday birthday = birthdays.get(0);
if (birthday != null && birthday.getDate() != null && birthday.getDate().getYear() != null && birthday.getDate().getMonth() != null
&& birthday.getDate().getDay() != null) {
// save mBirthday
Calendar calendar = Calendar.getInstance();
calendar.set(birthday.getDate().getYear(), birthday.getDate().getMonth(), birthday.getDate().getDay());
birthdayLocal = calendar.getTime().getTime();
LogUtils.d(TAG, "mBirthday : " + birthday.toString());
}
}
// Phone Number
List<PhoneNumber> phoneNumbers = userProfile.getPhoneNumbers();
if (phoneNumbers != null && phoneNumbers.size() > 0) {
PhoneNumber phoneNumber = phoneNumbers.get(0);
if (phoneNumber != null) {
// save mPhoneNumber
phoneLocal = phoneNumber.getValue();
LogUtils.d(TAG, "mPhoneNumber : " + phoneNumber.getValue());
}
}
// biography (About me)
List<Biography> biographies = userProfile.getBiographies();
if (biographies != null && biographies.size() > 0) {
Biography biography = biographies.get(0);
if (biography != null) {
// save biography
aboutMeLocal = biography.getValue();
LogUtils.d(TAG, "biography : " + biography.getValue());
}
}
}
try{
ListConnectionsResponse response = service.people().connections()
.list("people/me")
// This line's really important! Here's why:
// http://stackoverflow.com/questions/35604406/retrieving-information-about-a-contact-with-google-people-api-java
.setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers,person.biographies")
.execute();
List<Person> connections = response.getConnections();
if (connections!=null && connections.size()>0){
for (Person person : connections) {
getPersonInfo(person);
}}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
iSignIn.onSaveUserInformation(personEmail, personFirstName, personLastName, image, genderString, birthdayLocal, phoneLocal, aboutMeLocal);
}
}
Спасибо, вы буквально потратили два дня, пытаясь это сделать, документы Google многое оставляют для воображения.
Как использовать эту асинтаксическую задачу? Я не могу найти класс GoogleAccountManager в v26 gradle "com.google.apis: google-api-services-people: v1-rev20180918-1 .26.0". Мне нужно прочитать пол пользователя, номер телефона пользователя и DoB пользователя. Я не могу читать через People API. Пожалуйста, дайте мне знать.