Deprecated Plus.PeopleApi.load

 ̄綄美尐妖づ 提交于 2019-12-04 05:51:37

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();
        ...
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!