Я пытаюсь реализовать метод Google Auth с использованием Firebase, но вход не выполняется.Logcat не показывает мне красный текст, когда он терпит неудачу.Я настроил SHA 1, но он все еще не работает.Я использую Kotlin.
LoginActivity.kt
package com.example.madistrezsieu
import android.content.Intent
import android.os.Bundle
import android.widget.Toast.LENGTH_SHORT
import android.widget.Toast.makeText
import androidx.appcompat.app.AppCompatActivity
import com.example.madistrezsieu.MainActivity
import com.example.madistrezsieu.R
import com.example.madistrezsieu.view.User
import com.google.android.gms.auth.api.Auth
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.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.common.api.GoogleApiClient
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.GoogleAuthProvider
import com.google.firebase.firestore.FirebaseFirestore
import kotlinx.android.synthetic.main.activity_login.*
class LoginActivity : AppCompatActivity() {
private var firebaseAuth: FirebaseAuth? = null
private var authStateListener: FirebaseAuth.AuthStateListener? = null
private var googleApiClient: GoogleApiClient? = null
private var rootRef: FirebaseFirestore? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
rootRef = FirebaseFirestore.getInstance()
google_sign_in_button.setOnClickListener { signIn() }
firebaseAuth = FirebaseAuth.getInstance()
authStateListener = FirebaseAuth.AuthStateListener { auth ->
val firebaseUser = auth.currentUser
if (firebaseUser != null) {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
}
val googleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
googleApiClient = GoogleApiClient.Builder(applicationContext)
.enableAutoManage(this) { makeText(this, "You got a GoogleApiClient Error!", LENGTH_SHORT).show() }
.addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
.build()
}
private fun signIn() {
val signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient)
startActivityForResult(signInIntent, RC_SIGN_IN)
}
public 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 googleSignInAccount = task.getResult(ApiException::class.java)
if (googleSignInAccount != null) {
firebaseSignInWithGoogle(googleSignInAccount)
}
} catch (e: ApiException) {
makeText(this, "Google sign in failed!", LENGTH_SHORT).show()
}
}
}
private fun firebaseSignInWithGoogle(googleSignInAccount: GoogleSignInAccount) {
val authCredential = GoogleAuthProvider.getCredential(googleSignInAccount.idToken, null)
firebaseAuth!!.signInWithCredential(authCredential).addOnCompleteListener(this) {
val firebaseUser = firebaseAuth!!.currentUser
if (firebaseUser != null) {
createUserIfNotExists(firebaseUser)
}
}
}
private fun createUserIfNotExists(firebaseUser: FirebaseUser) {
val uid = firebaseUser.uid
val userName = firebaseUser.displayName
val user = User(uid, userName!!)
val uidRef = rootRef!!.collection("users").document(uid)
uidRef.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
val document = task.result
if (!document!!.exists()) {
uidRef.set(user)
}
}
}
}
public override fun onStart() {
super.onStart()
firebaseAuth!!.addAuthStateListener(authStateListener!!)
}
public override fun onStop() {
super.onStop()
if (firebaseAuth != null) {
firebaseAuth!!.removeAuthStateListener(authStateListener!!)
}
}
companion object {
//private val TAG = "LoginActivity"
private const val RC_SIGN_IN = 123
}
}
Я не знаю, в чем может быть проблема, если я сделаю другой проект Android Studio и скопирую, вставлю код, а затем добавлю еще одинприложение в проекте Firebase?
LOGCAT
06-12 15:08:23.264 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{c73e49c u0 com.google.android.gms/com.google.android.gms.auth.api.signin.ui.SignInActivity}
06-12 15:08:23.334 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{c73e49c u0 com.google.android.gms/com.google.android.gms.auth.api.signin.ui.SignInActivity}
06-12 15:08:23.374 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{15692e6 u0 com.example.madistrezsieu/com.google.android.gms.auth.api.signin.internal.SignInHubActivity}
06-12 15:08:23.544 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{1ccc6a0 u0 com.google.android.gms/com.google.android.gms.signin.activity.SignInActivity}
06-12 15:08:25.174 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{de116d2 u0 com.google.android.gms/com.google.android.gms.common.api.GoogleApiActivity}
06-12 15:08:26.354 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{1187be7 u0 com.google.android.gms/com.google.android.gms.common.account.OriginalAccountChipAccountPickerActivity}
06-12 15:08:28.044 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{6a14a0b u0 com.google.android.gms/com.google.android.gms.common.account.AccountChipAccountPickerActivity}
06-12 15:08:28.074 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{1ccc6a0 u0 com.google.android.gms/com.google.android.gms.signin.activity.SignInActivity}
06-12 15:08:28.744 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{de116d2 u0 com.google.android.gms/com.google.android.gms.common.api.GoogleApiActivity}
06-12 15:08:28.764 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{c73e49c u0 com.google.android.gms/com.google.android.gms.auth.api.signin.ui.SignInActivity}
06-12 15:08:28.824 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{15692e6 u0 com.example.madistrezsieu/com.google.android.gms.auth.api.signin.internal.SignInHubActivity}
06-12 15:08:28.834 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{d14245b u0 com.example.madistrezsieu/com.example.madistrezsieu.LoginActivity}
06-12 15:08:28.834 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{d14245b u0 com.example.madistrezsieu/com.example.madistrezsieu.LoginActivity}
06-12 15:08:28.894 2897-2897/com.example.madistrezsieu D/WHATTHEFUCK: EROARE
06-12 15:08:33.884 1383-2275/? D/AlarmManager: sending alarm PendingIntent{bcc15b6: PendingIntentRecord{49e31a2 com.whatsapp broadcastIntent}}, i=com.whatsapp.MessageHandler.CLIENT_PINGER_ACTION, t=2, cnt=1, w=2614648, Int=240000
06-12 15:08:46.924 1383-3180/? D/StatusBarManagerService: disable:userId=0 what=0x3200000 which=0x1 pkg=com.android.systemui
06-12 15:08:46.954 1383-1428/? D/StatusBarManagerService: disable:userId=0 what=0x400000 which=0x1 pkg=Window{72431d1 u0 StatusBar}
06-12 15:08:48.854 2930-3536/? I/ContextualWidget: handleMessage, what=1028 mode=GettingOut maxcount=8