android - 如何在我的 Android 应用程序中集成 google+ 登录?

标签 android google-plus google-play-services google-api-client google-signin

大家好,实际上我需要通过我的应用程序让来自 google+ 的人登录,现在我阅读了 google 上的文档,其中指出

To allow users to sign in, integrate Google Sign-In into your app. When you initialize the GoogleApiClient object, request the PLUS_LOGIN and PLUS_ME scopes

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Scopes.PLUS_LOGIN)
.addScope(Scopes.PLUS_ME)
.build();

现在我很困惑,我是应该只使用上面的代码还是先实现 google 登录,然后在我的 onsuccess 方法中编写这段代码以从 google+ 获取个人资料。同样在上面的代码中,Scopes.Scopes.PLUS_LOGIN 没有工作,所以我使用了 PLUS.PLUS_LOGIN 谷歌的文档太旧了。

现在我已经使用了上面的代码,但它不是从 google+ 帐户登录,只是它显示了一个对话框,说明我需要从哪个帐户登录,但是当我点击该帐户时,它也没有执行任何操作目前我正在使用 google-sign 集成见下面的代码

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
    .build();

一切都很好,但我需要人的性别和他/她的出生日期,这就是为什么我考虑使用 google+,但问题是 google+ 集成对我不起作用我现在应该做什么 ??

如果有人知道如何使用这两个 google+ 或 google 登录 api 中的任何一个获取人的性别或出生日期,请告诉我我该怎么做,这对我来说真的很有意义。

最佳答案

因为您已经说过您当前的应用程序已经可以很好地支持 Google 登录,所以为了获取 Google+ 个人资料信息(例如性别、生日...),首先,请确保您已经创建了 Google+ 个人资料用于您的 Google 帐户。然后您可以使用以下代码更新您的应用程序。请注意,我在 GoogleSignInOptions 中使用了 .requestScopes(new Scope(Scopes.PLUS_LOGIN)),而不是在 mGoogleApiClient 中。

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);
            if (person != null){
                    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'

P/S:如果已创建 Google+ 个人资料,但在您将 Google 帐户添加到设备后,您需要从设备中删除现有的 Google 帐户,然后重新添加。当您再次运行您的应用程序时,将显示询问您允许/拒绝访问 Google+ 的消息。当然,您必须单击“允许”。

希望这对您有所帮助并且对您来说很清楚!

关于android - 如何在我的 Android 应用程序中集成 google+ 登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33934362/

相关文章:

android - 我什么时候应该断开 GoogleApiClient (Android)

Android Google plus,深度链接

java - 连接到 Google Play 游戏时出错

android - 当功能发生代码更改时,我们可以只发布动态功能模块而不是再次发布整个应用程序吗?

ios - Google+ 登录按钮自定义

android - 在 Google Play 游戏中查看应用的排行榜

java - 当用户不在线时,Google Analytics 是否离线存储数据?

android - Android:解密过程需要很多时间

android - 这是使用事件总线的正确方法吗?

javascript - 通过远程网站读取智能手机传感器数据