Когда я тестирую его на эмуляторе, я также получаю предупреждение:
"Сервисы Google Play устарели для com.example. Дхм20. Требуется 230815045, но найдено 201817022"
Может ли ошибка быть из-за этого? (Я пробовал обновить зависимости игровых сервисов, сейчас установлена последняя версия)
Код:
Код: Выделить всё
@Composable
fun SignInScreen(navController: NavController) {
val coroutineScope = rememberCoroutineScope()
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text("Sign in to your account")
Spacer(modifier = Modifier.height(20.dp))
val context = LocalContext.current
val onClick: () -> Unit = {
val credentialManager = CredentialManager.create(context)
val rawNonce = UUID.randomUUID().toString()
val bytes = rawNonce.toByteArray()
val md = MessageDigest.getInstance("SHA-256")
val digest = md.digest(bytes)
val hashedNonce = digest.fold("") { str, it -> str + "%02x".format(it) }
val googleIdOption = GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(false)
.setServerClientId(ClientID)
.setNonce(hashedNonce)
.build()
val request = GetCredentialRequest.Builder()
.addCredentialOption(googleIdOption)
.build()
coroutineScope.launch {
try {
val result = credentialManager.getCredential(
request = request,
context = context,
)
val credential = result.credential
val googleIdTokenCredential = GoogleIdTokenCredential
.createFrom(credential.data)
val googleIdToken = googleIdTokenCredential.idToken
Log.i(TAG, googleIdToken)
// Sign in with Firebase using the ID token
signInWithGoogle(googleIdToken)
Toast.makeText(context, "You are signed in!", Toast.LENGTH_SHORT).show()
// Navigate to your DHM main screen
navController.navigate("DHM_MAIN_SCREEN")
} catch (e: GetCredentialException) {
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
} catch (e: GoogleIdTokenParsingException) {
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
}
}
}
// Google Sign-In Button
Button(onClick = onClick) {
Text("Sign in with Google")
}
}
}
suspend fun signInWithGoogle(idToken: String) {
val firebaseCredential = GoogleAuthProvider.getCredential(idToken, null)
Firebase.auth.signInWithCredential(firebaseCredential).await()
}
Код: Выделить всё
implementation(platform("com.google.firebase:firebase-bom:33.4.0"))
implementation("com.google.firebase:firebase-auth")
implementation("com.google.firebase:firebase-database")
implementation("com.google.firebase:firebase-firestore")
implementation("com.google.accompanist:accompanist-permissions:0.34.0")
implementation ("com.firebaseui:firebase-ui-auth:8.0.2")
implementation ("com.google.firebase:firebase-auth-ktx")
//Authentication with Credential Manager
implementation("androidx.credentials:credentials:1.3.0")
implementation("androidx.credentials:credentials-play-services-auth:1.3.0")
implementation ("com.google.android.libraries.identity.googleid:googleid:1.1.1")
implementation ("com.google.android.gms:play-services-auth:21.2.0")
implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0")
Подробнее здесь: https://stackoverflow.com/questions/791 ... cies-found