Get User's Birthdate & Gender using Google Sign-In Android

Kasim Rangwala picture Kasim Rangwala · Apr 5, 2017 · Viewed 20.5k times · Source

I've integrated Google Sign-In in my application. I can get user's Email and DisplayName. Now, I want to get user's Birthdate and Gender.

I've added all required requests & Scopes into GoogleApiClient which all are granted by API. here's 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.
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestProfile() <- This
            .requestScopes(
                    new Scope(Scopes.PLUS_ME), new Scope(Scopes.PROFILE) <- This
            )
            .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 */, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    // An unresolvable error has occurred and Google APIs (including Sign-In) will not
                    // be available.
                    Log.d(TAG, "onConnectionFailed:" + connectionResult);
                }
            } /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .addScope(new Scope(Scopes.PLUS_ME)) <- This
            .addScope(new Scope(Scopes.PROFILE)) <- This
            .build();
    // [END build_client]

Here's the granted Scopes in GoogleSignInAccount

private void setupUserData(GoogleSignInAccount acct) {
    if (acct != null) {
        mPrefs.setName(acct.getDisplayName());
        mPrefs.setEmail(acct.getEmail());
        if (acct.getPhotoUrl() != null) {
            mPrefs.setPicURL(acct.getPhotoUrl().toString());
        }
        Set<Scope> scopes = acct.getGrantedScopes(); <- This
        for (Scope scope : scopes) {
            Log.d(TAG, "setupUserData: " + scope.toString()); <- This
        }
    }
}

Here's the log of granted scopes

D/SplashActivity: setupUserData: GrantedScopes size 6
D/SplashActivity: setupUserData: https://www.googleapis.com/auth/plus.me
D/SplashActivity: setupUserData: https://www.googleapis.com/auth/userinfo.email
D/SplashActivity: setupUserData: https://www.googleapis.com/auth/userinfo.profile
D/SplashActivity: setupUserData: email
D/SplashActivity: setupUserData: profile
D/SplashActivity: setupUserData: openid

Here's my Google Mobile Service's dependency

compile 'com.google.android.gms:play-services-auth:10.2.0'
compile 'com.google.android.gms:play-services-plus:10.2.0'

Now, I don't know how to get access to user's profile information.

Answer

Teyam picture Teyam · Apr 6, 2017

As mentioned in Getting people and profile information, to get additional profile information and a user's contacts, use the People API. You must get consent from the user to access this information by requesting additional scopes when the user signs in.

You can call people.get, passing in a resource name, to get private contact and public profile data for each person. If your request is successful, the response contains an instance of a Person including birthday and gender.

You may want to visit the links I've provided for more information.