不推荐使用Plus.PeopleApi.load

Ioa*_*dze 6 android google-plus google-signin google-people

现在Plus.API已经弃用了Google Play Services 9.4,在Android应用程序上为经过身份验证的用户获取Google Plus圈子的正确方法是什么?

现在我们已经弃用了加载方法加上用户Plus.PeopleApi.load

新文件说:

如果您的应用需要社交信息和更广泛的个人资料数据,请查看Android联系人提供程序或跨平台的People API.

所以我应该使用Android Contacts Provider,这似乎是一个很难的选择(因为我必须使用游标过滤联系人并管理运行时权限).

以前弃用方法的任何简单替代方法只是为用户获取G +圈子列表?

Isa*_*hen 4

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 不同)。此外,如果您需要除名字/姓氏/显示名称、电子邮件和个人资料图片网址(登录 API 已提供)之外的登录用户的个人资料信息,您还应该使用相同的新 People 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'
Run Code Online (Sandbox Code Playgroud)

然后编写登录代码。

// 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
Run Code Online (Sandbox Code Playgroud)

然后您可以使用新的 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();
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)