Android studio - 从 Play 商店下载应用程序时,Firebase Google 身份验证不起作用

问题描述 投票:0回答:1

我正在开发 Mission Control - 一个时间管理应用程序,最近我使用 Firebase 添加了 Google 身份验证(电子邮件和密码是第一个身份验证方法)。

直接从 Android Studio 测试“通过 Google 注册/登录”时,一切正常,但当我将应用程序上传到商店并从那里下载时,却出现了问题。

按 Google btn 将打开您现有的电子邮件列表以供选择,仅此而已 - 无需注册/登录,没有任何错误..

如果有人对这个问题有任何经验,我希望得到一些帮助:)

这是该应用程序: https://play.google.com/store/apps/details?id=com.missioncontrol.timeplanning

代码:

private fun initSignInWithGoogle() //Called from onCreate
    {
        //Actions for signing in w google:

        //Sign w Google btn
        val btnSignWGoogle = binding?.btnSignWGoogle

        // Initialize sign in options the client-id is copied form Firebase auth
        val googleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("/*Here is the id*/")
            .requestEmail()
            .build()

        // Initialize sign in client
        googleSignInClient = GoogleSignIn.getClient(this@IntroActivity, googleSignInOptions)

        btnSignWGoogle?.setOnClickListener { // Initialize sign in intent
            val intent: Intent = googleSignInClient.signInIntent
            // Start activity for result
            startActivityForResult(intent, Constants.GOOGLE_SIGN_REQUEST_CODE) //Show emails
        }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        // Check whether we returned from email selection and an email was selected
        if (requestCode == Constants.GOOGLE_SIGN_REQUEST_CODE&&resultCode == RESULT_OK) {
            // When request code is equal to GOOGLE_SIGN_REQUEST_CODE
             signInAccountTask =
                GoogleSignIn.getSignedInAccountFromIntent(data)

            //Get the user data
             googleSignInAccount = signInAccountTask.result
             displayName = googleSignInAccount?.displayName!!
             email = googleSignInAccount?.email!!
             photoUrl = googleSignInAccount?.photoUrl?.toString()!!


            Log.d("SignInGoogle", "Display Name: $displayName")
            Log.d("SignInGoogle", "Email: $email")
            Log.d("SignInGoogle", "Photo URL: $photoUrl")


            //Checks if the email is in the USERS collection
            FireStoreClass().checkEmailUniqueness(email!!, this) { isUnique ->
            }
        }
        else{
            Log.d("SignInGoogle", "Error + " + requestCode.toString())
        }
    }

    fun isEmailUnique(isUnique: Boolean)
    {
        //Called from firebase
        if (isUnique) {
            Log.d("SignInGoogle", "Email unique")
            //Email is not used - create new user and go to trailer
            try {
                try {              
                    if (googleSignInAccount != null) {
                        // When sign in account is not equal to null initialize auth credential
                        val authCredential: AuthCredential = GoogleAuthProvider.getCredential(
                            googleSignInAccount.idToken, null
                        )
                        // Check credential and sign the user up
                        auth.signInWithCredential(authCredential)
                            .addOnCompleteListener(this) { task ->
                                // Check sign up
                                if (task.isSuccessful) {
                                    // When task is successful redirect
                                    Log.d("SignInGoogle", "Trying to add user to collection")
                                    val user = com.missioncontrol.timeplanning.models.User("", displayName!!,email!!,photoUrl?:"",0,"",)
                                    FireStoreClass().registerUser(this, user)
                                    // Display Toast
                                    displayToast("Thank you for registering")
                                } else {
                                    // When task is unsuccessful display Toast
                                    displayToast(
                                        "Authentication Failed :" + task.exception?.message
                                    )
                                }
                            }
                    }
                } catch (e: ApiException) {
                    e.printStackTrace()
                    Log.d("SignInGoogle", e.toString())
                }

            }
            catch (e: Exception)
            {
                Toast.makeText(this@IntroActivity, "Error please try again or contact support", Toast.LENGTH_SHORT).show()
            }

        }
        else
        {//Email exists - sign in
            Log.d("SignInGoogle", "Email not unique")

            if (signInAccountTask.isSuccessful) {
                Log.d("SignInGoogle", "Success sign in")
                // When google sign in successful initialize string
                val s = "Google sign in successful"
                // Display Toast
                displayToast(s)
                // Initialize sign in account
                try {
                    // Initialize sign in account
                    if (googleSignInAccount != null) {
                        // When sign in account is not equal to null initialize auth credential
                        val authCredential: AuthCredential = GoogleAuthProvider.getCredential(
                            googleSignInAccount.idToken, null
                        )
                        // Check credential
                        auth.signInWithCredential(authCredential)
                            .addOnCompleteListener(this) { task ->
                                // Check condition
                                if (task.isSuccessful) {
                                    // When task is successful redirect to Main activity
                                    startActivity(
                                        Intent(
                                            this@IntroActivity,
                                            MainActivity::class.java
                                        ).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                    )
                                } else {
                                    // When task is unsuccessful display Toast
                                    displayToast(
                                        "Authentication Failed :" + task.exception?.message
                                    )
                                }
                            }
                    }
                } catch (e: ApiException) {
                    e.printStackTrace()
                    Log.d("SignInGoogle", e.toString())
                }
            }
            else
            {
                Log.d("SignInGoogle", signInAccountTask.toString())
            }
        }
    }

    fun userRegisteredSuccess() //Gets called automatically from FireStoreClass upon registration
    {
        //User was added to collection successfully - go to trailer
        Log.d("SignInGoogle", "added user to collection")

        Toast.makeText(this, "Thanks for registering" +
                "." , Toast.LENGTH_LONG).show()
        hideProgressDialog()

                    startActivity(Intent(this, TrailerActivity::class.java))
                    finish()
    }

在多部手机上运行它 - 所有手机上的问题都是相同的

android firebase firebase-authentication google-oauth google-signin
1个回答
0
投票

我将 Google 控制台中的所有 sha-1 和 sha-256 代码以及 Android studio 中的代码添加到 Firebase 中,并将新的 json 添加到项目中,它解决了问题

© www.soinside.com 2019 - 2024. All rights reserved.