Skip to content Skip to sidebar Skip to footer

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.PeopleA

Solution 1:

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 & emailGoogleSignInOptionsgso=newGoogleSignInOptions.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. */privatestaticHttpTransportHTTP_TRANSPORT= AndroidHttp.newCompatibleTransport();
/** Global instance of the JSON factory. */privatestaticfinalJsonFactoryJSON_FACTORY= JacksonFactory.getDefaultInstance();

// On worker threadGoogleAccountCredentialcredential=
         GoogleAccountCredential.usingOAuth2(MainActivity.this, Scopes.PROFILE);
credential.setSelectedAccount(
        newAccount(googleSignInAccount.getEmail(), "com.google"));
Peopleservice=newPeople.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME /* whatever you like */) 
                .build();
// All the person detailsPersonmeProfile= service.people().get("people/me").execute();
// e.g. Gender
List<Gender> genders = meProfile.getGenders();
Stringgender=null;
if (genders != null && genders.size() > 0) {
    gender = genders.get(0).getValue();
}

Solution 2:

UPDATE: based on @Isabella Chen's comments below, for who does not want to use getCurrentPerson which is marked deprecated, you can start using Google People API instead, you can also see my another answer at the following S.O question:

Cannot get private birthday from Google Plus account although explicit request


IMO, you can refer to the following code:

// [START configure_signin]// Configure sign-in to request the user's ID, email address, and basic// profile. ID and basic profile are included in DEFAULT_SIGN_IN.// FOR PROFILE PICTURE:// Ref: https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInAccount.html#getPhotoUrl%28%29// getPhotoUrl(): Gets the photo url of the signed in user.// Only non-null if requestProfile() is configured and user does have a Google+ profile picture.GoogleSignInOptionsgso=newGoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestScopes(newScope(Scopes.PROFILE))
                .requestScopes(newScope(Scopes.PLUS_LOGIN))
                .requestProfile()
                .requestEmail()
                .build();
        // [END configure_signin]// [START build_client]// Build a GoogleApiClient with access to the Google Sign-In API and the// options specified by gso.
        mGoogleApiClient = newGoogleApiClient.Builder(this)
                .enableAutoManage(this/* FragmentActivity */, this/* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .addApi(Plus.API)
                .build();
        // [END build_client]

Then:

@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);if (requestCode == RC_SIGN_IN) {
            GoogleSignInResultresult= Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);

            Personperson= Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);            
            Log.i(TAG, "Gender: " + person.getGender());
        }
    }

Logcat info:

11-20 09:06:35.431 31289-31289/com.example.googlesignindemo I/GoogleSignIn: Gender: 0

Hope this helps!

Solution 3:

For getting profile information google introduced the easiest way so far I think!

GoogleSignInAccountacct= GoogleSignIn.getLastSignedInAccount(getActivity());
if (acct != null) {
  StringpersonName= acct.getDisplayName();
  StringpersonGivenName= acct.getGivenName();
  StringpersonFamilyName= acct.getFamilyName();
  StringpersonEmail= acct.getEmail();
  StringpersonId= acct.getId();
  UripersonPhoto= acct.getPhotoUrl();
}

Post a Comment for "Get Person Details In New Google Sign In Play Services 8.3"