我怎样才能获得简介像机器人从谷歌登入的性别? [英] How can i get profile like gender from google signin in android?
问题描述
我要整合谷歌登录到我的应用程序,当我第一次用户将符号创建一个帐户绑定到这一点,所以我需要一个像性别,语言环境等一些配置文件。
我试图为谷歌,登录doc和快速启动样本显示:
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);
登录成功,我可以得到的onActivityResult数据结构GoogleSignInResult,从GoogleSignInResult我可以得到一个GoogleSignInAccount,其中只包含显示名称,电子邮件地址和ID。
但是当 https://developers.google.com/apis-explorer/#p /我可以得到这样的性别概况,语言环境。是有什么我错过了吗?
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 ?
和我想谷歌加的API,似乎我能得到我想要的。但是却不知道如何使用,医生说创建客户端是这样的:
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();
但是当我用这个,点击登入按钮,将导致应用程序崩溃,任何人都可以给一些建议或样品code?对不起我的英文不好...
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...
推荐答案
首先,确保你已经创建Google+个人资料为您的谷歌帐户。然后,你可以参考以下code:
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();
和
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addApi(Plus.API)
.build();
然后
@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());
}
}
在的build.gradle
文件
// 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'
您可以看看我的GitHub的样本项目。希望这有助于!
You can take a look at My GitHub sample project. Hope this helps!
这篇关于我怎样才能获得简介像机器人从谷歌登入的性别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!