I am trying to build signin with google button in android studio and after running the code and downloading all the dependencies after following the video in this documentation and referring the steps in this android’s documentation I get the error (as shown in screen shot) .
As i am testing it on Emulator i also get warning:
"Google Play services out of date for com.example.dhm20. Requires 230815045 but found 201817022"
Could the error be because of this? (I tried updating the play services dependencies and is currently on the latest)
Code:
@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()
}
Installed dependencies:
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")
I tried debugging it by setting and deleting the Google OAuth provider in Firebase and the Api console, but I can’t seem to get rid of the error.
2
Answers
I recently encountered the same problem on my end, this is how I approached it by:
AndroidManifest.xml
, ensure you have the following permissions:INTERNET
,ACCESS_NETWORK_STATE
, and any other specific permission required for the Google services you’re using.At times, the error messages might be misleading…
Please ensure following
Confirm that you have initialized Firebase in your
main.dart
file and also ensure that you have added propergoogle-services.json
to your projectMake sure you have enabled providers in firebase console.
Clear and rebuild the project