繁体   English   中英

Google 登录:使用播放服务获取用户信息

[英]Google Login: Getting User information with play services

我正在开发一款 android 游戏,我必须在其中获取后端的用户 gmail 地址。 我正在使用 google 提供的游戏助手类,并且正在使用 google play API 和 plus api。 直到最近我才使用

Plus.AccountApi.getAccountName(_gameHelper.getApiClient());

为了获取地址,但此代码已贬值,因此我无法在某些设备上使用它。

我尝试从登录的onActivityResult信息中获取信息,但 Intent 返回为空。 不仅如此,为了从 Intent 访问该信息,我需要谷歌登录 API,该 API 无法与播放 API 一起使用。 我将不胜感激有关此事的任何帮助或建议。 非常感谢您。

将 Google 登录添加到您的 Android 应用程序

配置 Google 登录:

// 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()
    .build();

// Build a GoogleApiClient with access to GoogleSignIn.API and the options above.
mGoogleApiClient = new GoogleApiClient.Builder(this)
    .enableAutoManage(this, this)
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
    .build();

然后,当登录按钮被点击时,启动登录意图:

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);

系统会提示用户选择要登录的 Google 帐户。 如果您请求的范围超出了个人资料、电子邮件和 openid,还会提示用户授予对所请求资源的访问权限。

最后,处理活动结果:

@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);
        if (result.isSuccess()) {
            GoogleSignInAccount acct = result.getSignInAccount();
            // Get account information
            mFullName = acct.getDisplayName();
            mEmail = acct.getEmail();
        }
    }
}

请参阅: https : //developers.google.com/identity/sign-in/android/

这是您在当前版本的 Kotlin 中执行此操作的方式:

Games.getPlayersClient(requireContext(), GoogleSignIn.getLastSignedInAccount(requireContext())!!)
            .currentPlayer
            .addOnSuccessListener { player ->
                
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM