-->

Deprecated Plus.PeopleApi.load

2020-07-11 07:29发布

问题:

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?

回答1:

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