如何从Android中的Google登录获取性别等个人资料? [英] How to get profile like gender from google signin in Android?

查看:237
本文介绍了如何从Android中的Google登录获取性别等个人资料?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将google登录集成到我的应用中,当用户首次登录时,我将为此创建一个帐户绑定,因此我需要一些个人资料,例如性别,语言环境等. 并且我尝试了Google登录文档和快速入门示例所示:

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,其中仅包含DisplayName,电子邮件和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?

我尝试了google plus api,看来我可以得到想要的东西.但不知道如何使用,文档说创建这样的客户端:

and I tried google plus api, it seems that I can get what I want. but don't know 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();

但是当我使用它时,单击登录按钮将导致应用程序崩溃.

but when I use this, click signin button will cause app crash.

更新:更新到新版本的Google登录时出现问题

Update: problems when update to new version of google sign in Missing api_key/current key with Google Services 3.0.0

推荐答案

更新:

Plus.PeopleApi 在Google Play服务9.4中已弃用,因为 Google的声明注释,请使用 Google People API 参考以下解决方案相反:

Since 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:

获取新的Google登录Play服务8.3中的人员详细信息 (伊莎贝拉·陈的回答);

Get person details in new google sign in Play Services 8.3 (Isabella Chen's answer);

无法从Google Plus获取私人生日帐户,尽管有明确的请求

更新结束

首先,请确保您已为自己的Google帐户创建了Google+个人资料.然后,您可以参考以下代码:

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!

这篇关于如何从Android中的Google登录获取性别等个人资料?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆