尝试使用凭据管理器在我的android应用中使用Google Button登录,但登录显示Activity is cancelled by the user当从弹出窗口中选择gmail帐户时。但它显示错误为androidx.credentials.exceptions.GetCredentialCancellationException:Activity is cancelled by the user。
在这里,我在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")
}
}
}
字符串
1条答案
按热度按时间hgqdbh6s1#
好的,我找到了错误
在开始之前,我想指出的是,这种错误我调查tipically不与Android代码本身的关系,更多的是关于你使用的CLIENT_SERVER_ID和Google Play控制台凭据。
我的解决方案:
我使用WEB_SERVER_ID(而不是android ID)创建GetSignInWithGoogleOption,这是正确的,但也有必要在Google Play控制台中为运行的应用程序创建android凭据
在我的情况下,我有一个“由谷歌服务自动创建”的Android凭据,我认为这是正确的,但它是为另一个开发人员在过去创建的,所以我需要为我自己创建一个
所以我去了Google Play控制台,点击Create Credential -> OAuth Client ID,创建了一个“Android application type”。Package是你可以在manifest.xml中找到的包名,SHA1是调试包名(因为我没有生产包)。你的设备的SHA1可以使用这个命令找到(替换路径):
keytool -keystore path-to-debug-keystore -list -v
对于MAC,将是:
keytool -keystore ~/.android/debug.keystore -list -v
该调试密钥库的密码是“android”
帮助解决此问题的文档如下:https://support.google.com/cloud/answer/6158849?hl=es-419#installedapplications&android&zippy=%2Cnative-applications%2Candroid