使用Firebase身份验证Google登录失败

时间:2019-07-12 03:55:17

标签: android kotlin firebase-authentication google-signin

这不是重复的问题。在前面的文章中,我找不到正确的解决方案。尝试使用上一个问题中提到的所有可能的东西后,这里的问题就会问到。

因此,我已经阅读了StackOverflow中提供的所有答案。对我来说没有任何工作,我已经按照firebase文档中的说明实施了此身份验证。另外,我输入了正确的SHA1,并且google-service.json已正确放置在我的项目中。因此,我从2天开始就陷入了问题,并且检查了从云控制台到Firebase的所有内容。 StackOverflow到GitHub但是我没有找到任何解决方案。

错误代码为: com.google.android.gms.common.api.ApiException:12500:

这是我的gradle依赖项

implementation 'com.google.firebase:firebase-auth:18.0.0'

implementation 'com.google.android.gms:play-services-auth:17.0.0'

这是我的Kotlin代码

package com.medlance.team.medlance.medlance.user

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.GoogleAuthProvider
import com.medlance.team.medlance.medlance.R
import com.medlance.team.medlance.medlance.home.HomeActivity
import kotlinx.android.synthetic.main.activity_user_login.*
import org.jetbrains.anko.longToast
import org.jetbrains.anko.toast

class UserLoginActivity : AppCompatActivity() {

    private lateinit var auth: FirebaseAuth
    private lateinit var googleSignInClient: GoogleSignInClient
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user_login)
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build()

        googleSignInClient = GoogleSignIn.getClient(applicationContext, gso)
        auth = FirebaseAuth.getInstance()
        signInButtonGoogle.setOnClickListener {
            loginProgress.visibility = View.VISIBLE
            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) {
                loginProgress.visibility = View.GONE
                longToast("Something went wrong")
                Log.w(TAG, "Google sign in failed"+resultCode, e)

            }
        }
    }

    private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {


        val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
        auth.signInWithCredential(credential)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success, update UI with the signed-in user's information
                    Log.d(TAG, "signInWithCredential:success")
                    val user = auth.currentUser
                    updateUI(user)
                } else {

                    Log.w(TAG, "signInWithCredential:failure", task.exception)
                    updateUI(null)
                }

            }
    }

    private fun updateUI(user: FirebaseUser?) {
        val intent = Intent(applicationContext,HomeActivity::class.java)
        if (user != null) {
            intent.putExtra("user",auth.currentUser.toString())
            startActivity(intent)
            finish()
        }
    }

    override fun onStart() {
        super.onStart()
        val currentUser = auth.currentUser
        updateUI(currentUser)
    }

    companion object {
        private const val TAG = "GoogleActivity"
        private const val RC_SIGN_IN = 9001
    }
}

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools" package="com.debashis.projectISRO">


    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme" tools:ignore="GoogleAppIndexingWarning">
        <activity
                android:name=".home.HomeActivity"
                android:label="@string/title_activity_home">
        </activity>
        <activity android:name=".user.UserLoginActivity">
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

我正在等待这个可怕的开发人员社区的帮助。

0 个答案:

没有答案