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
.
Using People api's you can retrieve birthdate and gender details.
Use dependency 'com.google.apis:google-api-services-people:v1-rev4-1.22.0' in gradle to include people's api.
Below is my answer hope it helps to you.
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:
and
Then
Inside build.gradle file
// Dependency for Google Sign-In
You can take a look at belo GitHub sample project. Hope this helps and solved your problem.
https://github.com/ngocchung/GoogleSignInDemo
If you want latest integration please follow below link which have a nice documentation as well as brief about code and it's all parameter.
https://developers.google.com/identity/sign-in/android/start-integrating
Here is the complete working example, hope it help for future reader. What the app do is sign in(Sign In API included name and email) first, then request birthday & gender(People API) authentication, and save it to
SharedPreferences
for reuse on next launch. Finally it will print basic info and advanced (gender & birthday) info.Reminder:
<uses-permission android:name="android.permission.INTERNET" />
in AndroidManifest.xml.implementation 'com.google.android.gms:play-services-auth:12.0.1'
,implementation 'com.google.apis:google-api-services-people:v1-rev255-1.23.0'
, andimplementation 'com.google.api-client:google-api-client-android:1.23.0'
independencies {}
ofbuild.gradle
.compileSdkVersion
,targetSdkVersion
, andappcompat-v7
from 27 to 26 since I got warning after #2 dependencies added.signingConfigs { debug { storeFile file('<path to jks file>') keyAlias '<your key alias>' keyPassword '<your key password>' storePassword '<your store password>' } }
inbuild.gradle
, which jks file generated fromBuild
->Generated Signed APK...
->Create new...
keytool -exportcert -keystore <path to jks file> -list -v
to get SHA1 hex key, then visits play console, fill in project name, app package name, SHA1 hex key.Scopes.BIRTHDAY
in library, so I have to hard-coded the birthday endpoint URL"https://www.googleapis.com/auth/user.birthday.read"
, which the link can get from https://developers.google.com/people/v1/how-tos/authorizing#profile-scopes OR "Show Scopes" in "Try it API" panel at https://developers.google.com/people/api/rest/v1/people/getaccount.getGivenName()
andaccount.getFamilyName()
.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.
Gradle
Authentication
PeopleApi
I hope it helps :)
add this in your
build.gradle
dependenciestry this