使用 Kotlin 使用 Firebase 登录 Google 中的 GetEmail() Null

问题描述

首先,对不起,如果已经有一个帖子解决了我的问题,但我发现的那些没有解决它。

我正在使用电子邮件创建登录并通过(工作正常)加上使用谷歌登录登录有效,但无论是在 Firebase 控制台还是在带有“account.email”的代码中,我都无法看到电子邮件

Firebase Console

binding.btGoogle.setonClickListener() {
            val googleConfig: GoogleSignInoptions =
                GoogleSignInoptions.Builder(GoogleSignInoptions.DEFAULT_SIGN_IN)
                    .requestIdToken(getString(R.string.default_web_client_id)).build()
            val googleClient: GoogleSignInClient = GoogleSignIn.getClient(this,googleConfig)
            googleClient.signOut()
            startActivityForResult(googleClient.signInIntent,GOOGLE_SIGN_IN)
        }
override fun onActivityResult(requestCode: Int,resultCode: Int,data: Intent?) {
        super.onActivityResult(requestCode,resultCode,data)

        if (requestCode == GOOGLE_SIGN_IN) {
            val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
            try {
                val account: GoogleSignInAccount? = task.getResult(ApiException::class.java)
//here 'account.email' is already null
                if (account != null) {
                    val credential: AuthCredential = GoogleAuthProvider.getCredential(
                        account.idToken,null
                    )

                    FirebaseAuth.getInstance().signInWithCredential(credential).addOnCompleteListener() {
                            if (it.isSuccessful) {
                                showHome(account.email ?: "",ProviderType.GOOGLE)
                            } else {
                                showAlert()
                            }
                        }
                }
            } catch (e: ApiException) {
                showAlert()
            }
        }
    }

调试中,我可以看到其他属性,如 displayName、givenname、idToken...但电子邮件只是 null。

我尝试过的解决方案:

  • 在 Firebase 中打开和关闭选项“避免使用相同的电子邮件创建多个帐户”

  • 使用“account.email”或“getEmail()”

困扰我的事情:

  • 寻找答案我看到有些人提到他们 接受了阅读权限,我没有看到任何要求我做的弹出窗口

这是我在 stackoverflow 中的第一个问题,加上我的英语不是最好的...对不起,如果我做错了什么,提前谢谢你。

解决方法

我真是太笨了,我漏了一个字:)

我有这个:

val googleConfig: GoogleSignInOptions =
                GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id))
        .build()

我需要这个:

val googleConfig: GoogleSignInOptions =
                GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id))
        .requestEmail()
        .build()