Search code examples
androidoauthtokengoogle-signin

Google Signin request for age not working


I am trying to login to google with Android, then I am sending the token to my server, and trying to access the data.

I would like to have access to the age range of the user, but seem to be unable.

Here is the Android code:

    // 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()
            .requestId()
            .requestIdToken(mView.getTarget().getString(R.string.google_server_client_id))
            .requestScopes(new Scope("https://www.googleapis.com/auth/profile.agerange.read"))
            .build();

    // Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
    mGoogleApiClient = new GoogleApiClient.Builder(mView.getActivity())
            .enableAutoManage(mView.getTarget().getActivity(), new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    Log.d(TAG, "onConnectionFailed: google signin");
                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

And here is the output (Server side) after using the access token received by the Android app:

[azp] => 99415613(...)s.googleusercontent.com
   [aud] => 994156(...)nss.apps.googleusercontent.com
   [sub] => 100452979093996347493
   [email] => b(...)gmail.com
   [email_verified] => 1
   [exp] => 1520621452
   [iss] => https://accounts.google.com
   [iat] => 1520617852
   [name] => M(...)e
   [picture] => https://lh3(...)hoto.jpg
   [given_name] => M(...)
   [family_name] => A(...)
   [locale] => (...)

Where the hell did age range go to?


Solution

  • According to the Getting Profile Information page, it looks like GoogleSignInOptions does not directly return information about a Person in the response but rather .requestProfile() just provides additional information Account object.

    To get the information you want, you need to use the People API which replaced the Plus API.

    After you sign in with your original code you need to create a credential and use it with the People API:

    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance(); //use some json factory of your choice
    
    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, Scopes.PROFILE);
    //NOTE: You'll use the email returned by your sign in (or an Account object from the same)
    credential.setSelectedAccount(new Account(email, "com.google"));
    
    People service = new People.Builder(httpTransport, jsonFactory, credential).setApplicationName("My Application").build();
    //'me' resolves of course, to the account you chose
    Person person = service.people().get("people/me").execute();
    person.getAgeRange();
    

    Note: A lot of credit for my understanding of this goes to this blog article and to Isabella Chens answer, so you might want to check those out too