ApiException尝试使用Google帐户在Firebase上对用户进行身份验证

时间:2019-08-03 01:39:19

标签: android firebase kotlin firebase-authentication

我正在尝试使用google帐户对我的Firebase应用进行身份验证,但每次com.google.android.gms.common.api.ApiException:12500:它都会返回一个异常:

我在firebase的提供程序配置中添加了Web客户端凭据(为Google登录自动创建),但是没有用。

class LoginActivity : AppCompatActivity() {

    private lateinit var auth: FirebaseAuth
    private lateinit var googleSignInClient: GoogleSignInClient

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login_activity)
//        if (savedInstanceState == null) {
//            supportFragmentManager.beginTransaction()
//                .replace(R.id.container, LoginFragment.newInstance())
//                .commitNow()
//        }
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build()

        googleSignInClient = GoogleSignIn.getClient(this, gso)

        button_google.setOnClickListener { signIn() }
    }

    private fun signIn() {
        val signInIntent = googleSignInClient.signInIntent
        startActivityForResult(signInIntent, RC_SIGN_IN)
    }

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

        if (requestCode == RC_SIGN_IN) {
            val task = GoogleSignIn.getSignedInAccountFromIntent(data)
            try {
                val account = task.getResult(ApiException::class.java)
                firebaseAuthWithGoogle(account!!)
            } catch (e: ApiException) {
                Log.w(com.ramattec.salaodavania.ui.login.TAG, "Google sign in failed", e)
            }
        }
    }

    private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
        Log.d(com.ramattec.salaodavania.ui.login.TAG, "firebaseAuthWithGoogle:" + acct.id!!)

        val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
        auth.signInWithCredential(credential)
            .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    Log.d(com.ramattec.salaodavania.ui.login.TAG, "signInWithCredential:success")
                    val user = auth.currentUser

                } else {
                    Log.w(com.ramattec.salaodavania.ui.login.TAG, "signInWithCredential:failure", task.exception)

                }
            }
    }
}

我的代码与Firebase文档相同,并且我的活动只有Google登录按钮

val任务= GoogleSignIn.getSignedInAccountFromIntent(数据)

0 个答案:

没有答案