我正在尝试在 android studio 中使用 google 按钮构建登录,在运行代码并下载所有依赖项后,按照此 documentation 中的视频并参考此 android documentation 中的步骤,我收到错误(如所示屏幕截图)getCredentialAsync 错误。
当我在模拟器上测试它时,我也收到警告:
“com.example.dhm20 的 Google Play 服务已过期。需要 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")
我尝试通过在 Firebase 和 Api 控制台中设置和删除 Google OAuth 提供程序来调试它,但我似乎无法摆脱该错误。
我最近遇到了同样的问题,这就是我的解决方法:
AndroidManifest.xml
中,确保您拥有以下权限:INTERNET
、ACCESS_NETWORK_STATE
,以及您正在使用的 Google 服务所需的任何其他特定权限。