I was trying to add google sign in system in my project. I have fulfilled every criteria on API Console. But whenever I select any google account for signing in it sends request code 0. I followed bellow approach.
Google Sign in client
fun getGoogleSignInClient(context: Context): GoogleSignInClient {
val gso =
GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(CLIENT_ID)
.requestEmail()
.requestScopes(Scope(Scopes.DRIVE_FILE))
.build()
return GoogleSignIn.getClient(context, gso)
}
From where invoking sign in operation
`Row(
modifier =
Modifier
.fillMaxWidth()
.padding(top = 24.dp)
.clickable {
val existingAccount = GoogleSignIn.getLastSignedInAccount(context)
if (existingAccount != null) {
Toast.makeText(context, "Already signed in", Toast.LENGTH_SHORT).show()
} else {
val googleSignInClient = getGoogleSignInClient(context)
launcher.launch(googleSignInClient.signInIntent)
}
},
verticalAlignment = Alignment.CenterVertically,
)`
Launcher to launch intent
`val launcher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult()) { result ->
val intent = result.data
println("Result code is ${result.resultCode}")
if (result.resultCode == Activity.RESULT_OK) {
if (result.data != null) {
val task: Task<GoogleSignInAccount> =
GoogleSignIn.getSignedInAccountFromIntent(intent)
if (task.isSuccessful) {
Toast.makeText(context, "Sign in successful", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "Sign in failed", Toast.LENGTH_SHORT).show()
}
}
} else {
Toast.makeText(context, "Sign in failed", Toast.LENGTH_SHORT).show()
}
}`