在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 belowGoogleSignInOptions 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 threadGoogleAccountCredential 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.loadNew documentation says: 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-deprecationREST endpoint announcement:https://developers.google.com/+/web/people/#retrieve-a-collection-of-peopleSo 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 belowGoogleSignInOptions 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-inThen 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 threadGoogleAccountCredential 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-21 01:48