google-people-api - 是否有任何工作示例/示例可用于使用适用于 Android 的 Google People API?
问题描述
我查看了google people API
他们的官方文档页面
https://developers.google.com/people/quickstart/java#step_4_run_the_sample
尽管遵循了所有必要的步骤API
都不起作用。
我在 GitHub 上遇到了以下示例,该示例效果不佳。https://github.com/Suleiman19/People-API-App
另外,我检查了谷歌示例
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);
}
}
推荐阅读
- flutter - 如何使用 ThemeData backgroundColor 获取 Colors.blue[100]?
- php - Symfony 4迁移实体表单验证使用validation.yaml
- ansible - 使用 Ansible 时格式化字符串事实列表
- angular - GraphQL 角度阿波罗客户端。从缓存中读取抛出错误,但对服务器的相同查询工作正常
- mysql - MySQL COUNT(Value) 在某些日期之间返回错误的计数值
- javascript - JSON parse/stringfy 添加额外数据
- c# - c# Nunit 获取所有忽略的测试计数和名称
- lua - 尝试在 lua 中索引一个 nil 值
- python - 如何在 tensorflow v2 中启用 cuda 统一内存
- php - 用php替换url中的句号