Reputation: 61
I looked into google people API
on their official documentation page
https://developers.google.com/people/quickstart/java#step_4_run_the_sample
in spite of following all necessary steps API
does not work.
I came across below example on GitHub which is not working as well. https://github.com/Suleiman19/People-API-App
also, I check google example
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/Desktop;
Upvotes: 3
Views: 1912
Reputation: 61
Below code is working. able to access people 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);
}
}
Upvotes: 3