我看到CredentialsClient可以保存和删除凭据,但不能更新。
尝试更新应用程序中的密码时,是否需要再次删除并保存凭据?
不,只需调用save函数,它就会覆盖密码。
例如:
val credentialsClient: CredentialsClient = Credentials.getClient(context)
credentialsClient.save(credential).addOnCompleteListener { task -> onCredentialSaved(task) }
credential是com.google.android.gms.auth.api.credentials.credential的一个实例。它包含一个已保存的凭据,但有新密码。
在回叫中,我发送的onCredentialSaved(task)
是这样的:
{ task ->
if (task.isSuccessful) {
Log.d(TAG, "SAVE: OK")
Toast.makeText(this, "Credentials saved", Toast.LENGTH_SHORT).show()
return@saveCredentials
}
val exception = task.exception
if (exception is ResolvableApiException) {
// Try to resolve the save request. This will prompt the user if
// the credential is new.
val rae: ResolvableApiException? = exception as ResolvableApiException?
try {
rae?.startResolutionForResult(this, ON_SAVED_CREDENTIALS)
} catch (exception: SendIntentException) {
// Could not resolve the request
Log.e(
TAG,
"Failed to send resolution.",
exception
)
Toast.makeText(this, "Save failed", Toast.LENGTH_SHORT).show()
}
} else {
// Request has no resolution
Toast.makeText(this, "Save failed", Toast.LENGTH_SHORT).show()
}
}
如果您进行调试,您会注意到在更新凭据时,它会落入第一个if
子句,因为任务返回为成功,如果它是新凭据,它不会落入其中,您必须用其余代码处理它。
我希望它能帮助你。