谷歌+ People API 将在 2017 年第一季度彻底停用,请参阅下面的弃用说明以获取详细信息:
Android 通告:
https://developers.google.com/+/mobile/android/api-deprecation
REST 端点通告:
https://developers.google.com/+/web/people/#retrieve-a-collection-of-people
因此,您应考虑建议的替代方案,并不基于 G+ Circle 好友构建新功能,因为 对于使用 plus.login 范围的新用户将无数据可用。
如果您不想请求运行时权限,仍然可以从 People REST API 获取已登录用户的联系人(请注意,这与 G+ People API 不同)。另外,如果您需要已登录用户的个人资料信息而不是名字、姓氏、显示名称、电子邮件和个人资料图片 URL(这已由登录 API 提供),您还应使用相同的新 People API。
在 Android 上,当您需要联系人数据时 (在上下文中向用户解释为什么要请求他们的联系人信息。不要在您的前门登录活动中预先请求联系人范围)
compile 'com.google.api-client:google-api-client:1.22.0'
compile 'com.google.api-client:google-api-client-android:1.22.0'
compile 'com.google.apis:google-api-services-people:v1-rev4-1.22.0'
然后编写登录代码。
// Make sure your GoogleSignInOptions request email & contacts scopes as shown below
GoogleSignInOptions gso =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope(PeopleScopes.CONTACTS_READONLY)))
.build();
// Follow official doc to sign-in.
// https://developers.google.com/identity/sign-in/android/sign-in
然后,您可以使用新的People Api来检索联系人列表。
private static HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
GoogleAccountCredential credential =
GoogleAccountCredential.usingOAuth2(MainActivity.this, PeopleScopes.CONTACTS_READONLY);
credential.setSelectedAccount(
new Account(googleSignInAccount.getEmail(), "com.google"));
People service = new People.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME )
.build();
ListConnectionsResponse response = service.people().connections()
.list("people/me")
.setPageSize(20)
.execute();
List<Person> connections = response.getConnections();
if (connections != null && connections.size() > 0) {
for (Person person : connections) {
List<Name> names = person.getNames();
if (names != null && names.size() > 0) {
Log.i(TAG, "Name: " + person.getNames().get(0).getDisplayName());
} else {
Log.i(TAG, "No names available for connection.");
}
List<Gender> genders = person.getGenders();
String ageRange = person.getAgeRange();
List<Birthday> birthdays = person.getBirthdays();
...
}
}