Search code examples
androidgoogle-play-servicesgoogle-api-clientgoogle-signingoogle-plus-signin

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?


Solution

  • 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.
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestScopes(new Scope(Scopes.PROFILE))
                    .requestScopes(new Scope(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 = new GoogleApiClient.Builder(this)
                    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .addApi(Plus.API)
                    .build();
            // [END build_client]
    

    Then:

    @Override
        public void onActivityResult(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) {
                GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                handleSignInResult(result);
    
                Person person  = 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!