I am trying to get the user's information, including gender and age, from its Google Plus account. Since these fields may be private, I thought that requesting them explicitly would solve the problem. However, although the sign in dialog states explicitly that the app requests to View your complete date of birth
, I fail to get the birthday.
These are my scopes (tried many variations):
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
//.requestScopes(new Scope(Scopes.PROFILE))
//.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestScopes(new Scope("https://www.googleapis.com/auth/user.birthday.read"),
new Scope("https://www.googleapis.com/auth/userinfo.profile"))
//.requestProfile()
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PROFILE))
.build();
When using the deprecated getCurrentPerson
in onActivityResult
, I get null value:
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (mGoogleApiClient.hasConnectedApi(Plus.API)) {
Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
if (person != null) {
Log.i(TAG, person.getDisplayName()); //returns full name successfully
Log.i(TAG, person.getGender()); //0
Log.i(TAG, person.getBirthday()); //null
}
}
}
I also tried to get it from the account (GoogleSignInAccount account = result.getSignInAccount()
) but as far as I've searched, this variable doesn't own the requested data at all.
Am I missing something? Or maybe it's impossible to get private data although explicit request?
Thanks.
BTW, I haven't tried yet a scenario with a private gender.
Try this initialization
else requested user needs to have public visibility of the birthday.
I think this Google People API documentation will be helpful for your issue.
Please pay attention to:
and
If your app has not got an access token, you can read Using OAuth 2.0 to Access Google APIs or try my answer at the following question:
UPDATE: Supposing that your app can get the access token by using the sample code in my answer above, then inside
onResponse
, add more snippets as below:Logcat info (I truncated since it's long)
GoogleSignInOptions
andGoogleApiClient
:Please note that the OkHttp version I use here is v2.6.0, if your app uses the newest (3.3.1), then the syntax/classes will be different. Of course, you can also use the others such as Volley, Retrofit...
Moreover, this Google's People API - Method people.get provides Try It as screenshot below
Other useful links:
Google Developers Blog - Announcing the People API
Google APIs related to Google People API
Authorizing with Google for REST APIs (another way to get access token)