从 Playstore 下载应用程序时 Google 身份验证失败,但从 Android studio 安装应用程序时 Google 身份验证有效

发布于 2025-01-13 11:47:33 字数 2179 浏览 3 评论 0原文

我通过 Firebase 进行 Google 身份验证时遇到了一些奇怪的问题。

当我从 google play 商店安装它时,谷歌身份验证无法工作,但另一方面,当我从 Android studio(相同版本)本地安装它时,它确实可以工作。

以下是身份验证功能 -

private fun signInWithGoogle() {
    // Configure Google Sign In
    val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id))
        .requestEmail()
        .build()
    googleSignInClient = GoogleSignIn.getClient(requireContext(), gso)
    val signInIntent = googleSignInClient.signInIntent
    getResult.launch(signInIntent)
}

private val getResult =
    registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()) {
        if (it.resultCode == Activity.RESULT_OK) {

            val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
            if (task.isSuccessful) {
                try {
                    // Google Sign In was successful, authenticate with Firebase
                    val account = task.getResult(ApiException::class.java)!!

                    firebaseAuthWithGoogle(account.idToken!!)
                } catch (e: ApiException) {
                    // Google Sign In failed, update UI appropriately

                }

            } else {
                println(task.exception?.message)
            }


        }
    }

private fun firebaseAuthWithGoogle(idToken: String) {
    val credential = GoogleAuthProvider.getCredential(idToken, null)
    mAuth.signInWithCredential(credential)
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                println("signInWithCredential:success")

                startActivity(Intent(activity, MainActivityApplication::class.java))
                activity?.finish()


            } else {
                // If sign in fails, display a message to the user.
                println("signInWithCredential:failure ${task.exception}")

            }
        }
        .addOnFailureListener {
            println(it.message)
            binding.pbGoogleBtn.visibility= View.GONE
        }
}

I ran into some strange issue with Google-Authentication through Firebase.

When I install it from google play store, google-authentication fails to work, but in the other hand when I install it locally from Android studio (same version), it does work.

the following is the authentication function -

private fun signInWithGoogle() {
    // Configure Google Sign In
    val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id))
        .requestEmail()
        .build()
    googleSignInClient = GoogleSignIn.getClient(requireContext(), gso)
    val signInIntent = googleSignInClient.signInIntent
    getResult.launch(signInIntent)
}

private val getResult =
    registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()) {
        if (it.resultCode == Activity.RESULT_OK) {

            val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
            if (task.isSuccessful) {
                try {
                    // Google Sign In was successful, authenticate with Firebase
                    val account = task.getResult(ApiException::class.java)!!

                    firebaseAuthWithGoogle(account.idToken!!)
                } catch (e: ApiException) {
                    // Google Sign In failed, update UI appropriately

                }

            } else {
                println(task.exception?.message)
            }


        }
    }

private fun firebaseAuthWithGoogle(idToken: String) {
    val credential = GoogleAuthProvider.getCredential(idToken, null)
    mAuth.signInWithCredential(credential)
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                println("signInWithCredential:success")

                startActivity(Intent(activity, MainActivityApplication::class.java))
                activity?.finish()


            } else {
                // If sign in fails, display a message to the user.
                println("signInWithCredential:failure ${task.exception}")

            }
        }
        .addOnFailureListener {
            println(it.message)
            binding.pbGoogleBtn.visibility= View.GONE
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文