Google+ 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+ 圈子好友构建新功能,因为plus.login 范围内的新用户将无法获得任何数据。
如果您不想请求运行时权限,您仍然可以从People REST API获取已登录用户的联系人(请注意,这与 G+ People API 不同)。此外,如果您需要登录用户的个人资料信息,而不是名字/姓氏/显示名、电子邮件和个人资料图片 url(已由登录 API 提供),您还应该使用相同的新人员 API。
在 Android 上,当您需要联系人数据时(在上下文中,向用户解释您为什么要求他们的联系人信息。不要在前门登录活动中预先请求联系人范围)
// Add dependencies (SDKs will be downloaded from mavenCentral)
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 来检索联系人列表。
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
// On worker thread
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 /* whatever you like */)
.build();
ListConnectionsResponse response = service.people().connections()
.list("people/me")
// request 20 contacts
.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();
...
}
}