Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

android - Get person details in new google sign in Play Services 8.3

I'm trying to get a user's profile using the new Google Sign In API introduced in play services 8.3. Other than Display Name, Email and Id, I also need user's gender.

Plus.PeopleApi.getCurrentPerson() 

is deprecated as per play services 8.3 and also returns null for me even though

mGoogleApiClient.hasConnectedApi(Plus.API) 

returns true.

GoogleSignInAccount.getGrantedScopes 

returns

https://www.googleapis.com/auth/plus.me
https://www.googleapis.com/auth/plus.login
profile
email
openid

Google Developer Console doesn't show any hits on the Google+ API. I have placed the correct google-services.json file in app/ folder of application. I even generated the SHA1 fingerprint programatically to verify if I was using the correct keystore.

How can I get the person google+ profile data (gender, family name, given name etc.) using the new sign in API?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Plus.API has been deprecated. See deprecation notes below: https://developers.google.com/+/mobile/android/api-deprecation

If you need profile information other than first / last / display name, email and profile picture url (which is already provided by the Sign-in API), please use the new People API.

On Android, you can do this:

// Add dependencies
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 profile & email
GoogleSignInOptions gso =
        new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
// Follow official doc to sign-in.
// https://developers.google.com/identity/sign-in/android/sign-in

Use People Api to retrieve detailed person info.

/** 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, Scopes.PROFILE);
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();
// All the person details
Person meProfile = service.people().get("people/me").execute();
// e.g. Gender
List<Gender> genders = meProfile.getGenders();
String gender = null;
if (genders != null && genders.size() > 0) {
    gender = genders.get(0).getValue();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...