尝试使用凭据管理器在我的 Android 应用程序中使用 Google 按钮登录,但登录显示在从弹出窗口中选择 Gmail 帐户时,活动已被用户取消。但它显示错误为 androidx.credentials.exceptions.GetCredentialCancellationException:活动已被用户取消。
这里我使用了GCP中的web_client_id,并且同一个应用程序有两个Android ClientID。
因此,我删除了 AppSigningKey (SHA-1) 可用的 client_id 并保留了已可用的 SHA-1(即上传密钥库 (SHA-1))
val googleIdOption: GetSignInWithGoogleOption = GetSignInWithGoogleOption.Builder(WEB_CLIENT_ID).build()
val request: GetCredentialRequest = GetCredentialRequest.Builder().addCredentialOption(googleIdOption).build()
R.id.btn_googleSignUp -> {
val credentialManager = CredentialManager.create(this@SignUpActivity)
coroutineScope.launch {
try {
Log.e("request.. ===", request.toString())
val result = credentialManager.getCredential(
request = request,
context = this@SignUpActivity,
)
handleSignIn(result)
} catch (e: GetCredentialCancellationException) {
Log.e("CredentialCancelException ===", e.message.toString())
} catch (e: GetCredentialException) {
Log.e("GetCredential ===", e.message.toString())
} catch (e: Exception) {
Log.e("Exception ===", e.message.toString())
}
}
}
fun handleSignIn(result: GetCredentialResponse) {
// Handle the successfully returned credential.
val credential = result.credential
when (credential) {
is PublicKeyCredential -> {
// Share responseJson such as a GetCredentialResponse on your server to
// validate and authenticate
//responseJson = credential.authenticationResponseJson
}
is PasswordCredential -> {
// Send ID and password to your server to validate and authenticate.
val username = credential.id
val password = credential.password
}
is CustomCredential -> {
if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
try {
// Use googleIdTokenCredential and extract id to validate and
// authenticate on your server.
val googleIdTokenCredential = GoogleIdTokenCredential
.createFrom(credential.data)
} catch (e: GoogleIdTokenParsingException) {
Log.e("Invalid GoogleId", "Received an invalid google id token response", e)
}
} else {
// Catch any unrecognized custom credential type here.
Log.e("Unexpected", "Unexpected type of credential")
}
}
else -> {
// Catch any unrecognized credential type here.
Log.e("Invalid GoogleID", "Unexpected type of credential")
}
}
}
好吧,我发现了我的错误
我使用 WEB_SERVER_ID(不是 android ID)创建 GetSignInWithGoogleOption,这是正确的,但还需要在 Google Play Console 中为运行的应用程序创建 android 凭据
就我而言,我有一个“由 Google 服务自动创建”的 Android 凭据,我认为这是正确的,但它是过去为另一位开发人员创建的,所以我需要为自己创建一个
因此,我转到 Google Play Console,点击“创建凭据”->“OAuth 客户端 ID”,然后创建一个“Android 应用程序类型”。 Package 是您可以在manifest.xml 中找到的包名称,SHA1 是调试包名称(因为我没有生产包名称)。可以使用以下命令找到您设备的 SHA1:
keytool -keystore ~/.android/debug.keystore -list -v
该调试密钥库的密码是“android”
解决我的问题的文档是这样的: https://support.google.com/cloud/answer/6158849?hl=es-419#installedapplications&android&zippy=%2Cnative-applications%2Candroid