不推荐使用Plus.PeopleApi.load [英] Deprecated Plus.PeopleApi.load

查看:127
本文介绍了不推荐使用Plus.PeopleApi.load的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在在 Google Play Services 9.4 中不推荐使用Plus.API在Android应用程序上为经过身份验证的用户获取 Google Plus圈子?

现在,我们已经弃用了加载方法以及用户解决方案

Google+ People API最终将于2017年第一季度全面弃用,有关详细信息,请参见以下弃用说明:

Android公告: https://developers.google.com/+/mobile/android/api-deprecation

REST端点公告: https://developers.google.com/+/web/people/#retrieve-a-collection-people

因此,您应该考虑建议的替代方法,而不是基于G +圈子好友构建新功能,因为在plus.login范围内,新用户无法获得任何数据.

如果您不想请求运行时权限,仍然可以从人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'

然后输入登录代码.

// 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();
        ...
    }
}

Now that Plus.API is deprecated in Google Play Services 9.4, what is correct way to get Google Plus circles for authenticated user on Android Application?

Now We have deprecated method of loading plus users Plus.PeopleApi.load

New documentation says:

If your app needs social information and more extensive profile data, check out the Android Contacts Provider or the cross-platform People API.

So I should go with Android Contacts Provider that seems to be a hard alternative (Because I have to filter contacts with cursors and also manage Runtime Permissions).

Any easy alternatives of previous deprecated method to just get List of G+ circles for user?

解决方案

Google+ People API will eventually be fully deprecated 2017 Q1, see below deprecation notes for details:

Android announcement: https://developers.google.com/+/mobile/android/api-deprecation

REST endpoint announcement: https://developers.google.com/+/web/people/#retrieve-a-collection-of-people

So you should consider alternatives suggested and not build new features based on G+ Circle friends, as no data will be available for new users with the plus.login scope.

If you don't want to request runtime permissions, you can still get signed-in user's contacts from People REST API (Note that this is something different from G+ People API). Also, if you need signed-in user's profile information other than first / last / display name, email and profile picture url (which is already provided by the Sign-in API), you should also use the same new People API.

On Android, when you need Contacts data (In context, explaining to the user why you are asking for their contacts information. DO NOT request contacts scope upfront in your front-door Sign-In Activity)

// 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'

Then write sign-in code.

// 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

Then you can use new People Api to retrieve contacts list.

/** 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();
        ...
    }
}

这篇关于不推荐使用Plus.PeopleApi.load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆