获取用户的出生日期&使用Google登录Android的性别 [英] Get User's Birthdate & Gender using Google Sign-In Android
问题描述
我在应用程序中整合了 Google登录
。我可以获取用户的电子邮件
和 DisplayName
。现在,我想获取用户出生日期
和性别
。
我添加了所有必需的请求
& 范围
转换为 GoogleApiClient
,这些都是通过API授予的。这里是代码。
// [START configure_signin]
//配置登录以请求用户ID,电子邮件地址和基本
//个人资料。 ID和基本配置文件包含在DEFAULT_SIGN_IN中。
GoogleSignInOptions GSO =新GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestProfile()< - 此
.requestScopes(
新范围( Scopes.PLUS_ME),新范围(Scopes.PROFILE)< - 此
)
.build();
// [END configure_signin]
// [START build_client]
//创建一个可以访问Google Sign-In API和
//的GoogleApiClient由gso指定的选项。
mGoogleApiClient =新GoogleApiClient.Builder(本)
.enableAutoManage(这个/ * FragmentActivity * /,新GoogleApiClient.OnConnectionFailedListener(){
@覆盖
公共无效onConnectionFailed(@NonNull ConnectionResult connectionResult){
//发生无法解析的错误,并且Google API(包括Sign-In)不会
//可用。
Log.d(TAG,onConnectionFailed:+ connectionResult);
}
} / * OnConnectionFailedListener * /)
.addApi(Auth.GOOGLE_SIGN_IN_API,GSO)
.addScope(新范围(Scopes.PLUS_ME))≤; - 这个
.addScope(新的范围(Scopes.PROFILE))< - 这
.build();
// [END build_client]
以下是 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(); < - 对于(作用域范围:范围){
Log.d(TAG,setupUserData:+ scope.toString())的
; < - 此
}
}
}
这是授予的范围的日志
D / SplashActivity:setupUserData:GrantedScopes大小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
这是我的Google移动服务的依赖项
compile'com.google.android.gms:play-services-auth:10.2.0 '
compile'com.google.android.gms:play-services-plus:10.2.0'
现在,我不知道如何访问用户的个人资料信息
。
感谢高级版。
正如获取人物和个人资料信息,以获取更多个人资料信息和用户的联系人,请使用 People API 。您必须获得用户的同意才能访问此信息,方法是请求其他范围当用户登录时。
您可以拨打 people.get
,传递一个资源名称,以获取每个人的私人联系人和公共个人资料数据。如果您的请求成功,则回复中包含人包括生日和 gender 。
您可能想要访问我提供了更多信息的链接。
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
.
Thanks in Advanced.
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.
这篇关于获取用户的出生日期&使用Google登录Android的性别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!