dx3904 dx3904 - 3 months ago 140
Android Question

How can i get profile like gender from google signin in android?

I want to integrate google sign in to my app ,when user first sign in i will create an account bind to this,so i need some profiles like gender ,locale ,etc .
and i tried as the google-sign-in doc and quick-start sample shows:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();


when click to sign in i will call :

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);


sign in successful ,i can get a data structure GoogleSignInResult in onActivityResult,from GoogleSignInResult i can get a GoogleSignInAccount,which only contains DisplayName,email and id.
but when in https://developers.google.com/apis-explorer/#p/,i can get profiles like gender,locale .Is there anything i missed ?

and i tried google plus api,it seems that i can get what i want .but don't konw how to use, the doc says create client like this :

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PLUS_LOGIN))
.addScope(new Scope(Scopes.PLUS_ME))
.build();


but when i use this ,click signin button will cause app crash ,can anyone give some advice or sample code ?sorry for my poor English...

Update: problems when update to new version of google sign in Missing api_key/current key with Google Services 3.0.0

BNK BNK
Answer

UPDATE:

Since Plus.PeopleApi has been deprecated in Google Play services 9.4 as Google's declaration notes, please refer to the following solutions using Google People API instead:

Get person details in new google sign in Play Services 8.3 (Isabella Chen's answer);

Cannot get private birthday from Google Plus account although explicit request

END OF UPDATE


First of all, make sure you have created Google+ profile for your Google account. Then you can refer to the following code:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)             
                .requestScopes(new Scope(Scopes.PLUS_LOGIN))
                .requestEmail()
                .build();

and

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .addApi(Plus.API)
                .build();

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);

            // G+
            Person person  = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
            Log.i(TAG, "--------------------------------");
            Log.i(TAG, "Display Name: " + person.getDisplayName());
            Log.i(TAG, "Gender: " + person.getGender());
            Log.i(TAG, "AboutMe: " + person.getAboutMe());
            Log.i(TAG, "Birthday: " + person.getBirthday());
            Log.i(TAG, "Current Location: " + person.getCurrentLocation());
            Log.i(TAG, "Language: " + person.getLanguage());
        }
    }

Inside build.gradle file

// Dependency for Google Sign-In
compile 'com.google.android.gms:play-services-auth:8.3.0'
compile 'com.google.android.gms:play-services-plus:8.3.0'

You can take a look at My GitHub sample project. Hope this helps!