fun SignInWithGoogleButton(modifier: Modifier = Modifier) {
val context = LocalContext.current
val credentialManager = remember(context) { CredentialManager.create(context) }
val coroutineScope = rememberCoroutineScope()
val WEB_CLIENT_ID = "[MyId]"
Button(
onClick = {
coroutineScope.launch {
try {
val signInWithGoogleOption: GetSignInWithGoogleOption = GetSignInWithGoogleOption.Builder(serverClientId = WEB_CLIENT_ID)
// add the nonce later
.build()
val request: GetCredentialRequest = GetCredentialRequest.Builder()
.addCredentialOption(signInWithGoogleOption)
.build()
val result = credentialManager.getCredential(
context = context,
request = request
)
handleSignInWithGoogleResult(result)
} catch (e: GetCredentialException) {
Log.e("GoogleSignIn", "Credential Manager Sign in with Google failed: ${e.message}", e)
} catch (e: Exception) {
Log.e("GoogleSignIn", "An unexpected error occurred: ${e.message}", e)
}
}
}
)
{
Text("Sign In with Google")
}
}
private fun handleSignInWithGoogleResult(result: GetCredentialResponse) {
val credential = result.credential
when (credential) {
is CustomCredential -> {
if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
try {
val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.data)
val idToken = googleIdTokenCredential.idToken
val email = googleIdTokenCredential.id
val displayName = googleIdTokenCredential.displayName
Log.d("GoogleSignIn", "ID Token: $idToken")
Log.d("GoogleSignIn", "Email (from googleIdTokenCredential.id): $email")
Log.d("GoogleSignIn", "Display Name: $displayName")
} catch (e: GoogleIdTokenParsingException) {
Log.e("GoogleSignIn", "Failed to parse Google ID token credential: ${e.message}", e)
}
} else {
Log.e("GoogleSignIn", "Unexpected type of CustomCredential: ${credential.type}")
}
}
else -> {
Log.e("GoogleSignIn", "Unexpected type of credential: ${credential.type}")
}
}
}
< /code>
зависимости < /p>
implementation("androidx.credentials:credentials:1.5.0")
implementation("androidx.credentials:credentials-play-services-auth:1.5.0")
implementation("com.google.android.libraries.identity.googleid:googleid:1.1.1")
< /code>
Проблема: < /strong> < /p>
- Когда я получаю доступ к googleidtokencredential.id, я получаю числовой идентификатор, а не адрес электронной почты пользователя. null.
my Google Cloud Console Configuration:
В моем проекте Google Cloud у меня есть следующие области:
- написано />
- ./auth/userinfo.profile
- openid
подтвердил, что настройка экрана согласия OAuth.>
Подробнее здесь: https://stackoverflow.com/questions/797 ... user-email
Мобильная версия