Credential Manager API ช่วยให้คุณออกข้อมูลเข้าสู่ระบบไปยังแอปผู้ถือ (หรือที่เรียกว่า "Wallet") ของ Android ได้ คู่มือนี้อธิบายวิธีออกข้อมูลเข้าสู่ระบบให้แก่ผู้ถือที่ผู้ใช้ต้องการ
การใช้งาน
ส่วนนี้จะอธิบายรายละเอียดขั้นตอนที่จำเป็นในการออกเอกสารรับรองดิจิทัล
เพิ่มทรัพยากร Dependency
เพิ่มทรัพยากร Dependency ต่อไปนี้ลงในสคริปต์บิลด์ Gradle
Kotlin
dependencies { implementation("androidx.credentials:credentials:1.7.0-alpha02") implementation("androidx.credentials:credentials-play-services-auth:1.7.0-alpha02") }
Groovy
dependencies { implementation "androidx.credentials:credentials:1.7.0-alpha02" implementation "androidx.credentials:credentials-play-services-auth:1.7.0-alpha02" }
เริ่มต้นใช้งาน Credential Manager
เริ่มต้นอินสแตนซ์ของคลาส CredentialManager
val credentialManager = CredentialManager.create(context)
สร้างคำขอการออกใบรับรอง
คำขอสร้างเอกสารรับรองดิจิทัลควรมีสตริง JSON ที่เป็นไปตามโปรโตคอลมาตรฐาน OpenID4VCI ตัวอย่างคำขอ OpenID4VCI มีลักษณะดังนี้
"requests": [
{
"protocol": "openid4vci-v1",
"data": {
"credential_issuer": "https://digital-credentials.dev",
"credential_configuration_ids": [
"com.emvco.payment_card"
],
"grants": {
"urn:ietf:params:oauth:grant-type:pre-authorized_code": {
"pre-authorized_code": "..."
}
}
}
}
]
สร้าง CreateDigitalCredentialRequest ที่มีคำขอการออกใบรับรอง
val issuanceRequestJson = "{ ... }" // Your issuance JSON
val createRequest = CreateDigitalCredentialRequest(
requestJson = issuanceRequestJson,
origin = null
)
ส่งคำขอออกใบรับรอง
ออกข้อมูลเข้าสู่ระบบให้แก่ผู้ถือของผู้ใช้โดยใช้ฟังก์ชัน createCredential ฟังก์ชันนี้จะเปิดตัวเลือก Bottom Sheet ของ Credential Manager ซึ่งช่วยให้ผู้ใช้เลือกแอปผู้ถือที่ต้องการจัดเก็บข้อมูลเข้าสู่ระบบได้
try {
val response = credentialManager.createCredential(
context = context,
request = createRequest
)
handleSuccess(response as CreateDigitalCredentialResponse)
} catch (e: CreateCredentialException) {
handleCreateException(e)
}
จัดการการตอบกลับ
หลังจากส่งคำขอการออกแล้ว ระบบจะแสดงผล CreateDigitalCredentialResponse การตอบกลับนี้มีสตริง responseJson ซึ่งอธิบายผลลัพธ์ของการออก
fun handleSuccess(response: CreateDigitalCredentialResponse) {
val responseJson = response.responseJson
// Parse responseJson according to your protocol (e.g. OpenID4VCI)
}
จัดการข้อยกเว้น
หากขั้นตอนการออกบัตรไม่สำเร็จ createCredential จะแสดง CreateCredentialException ซึ่งแอปของคุณควรจัดการดังนี้
fun handleCreateException(e: CreateCredentialException) {
when (e) {
is CreateCredentialCancellationException -> {
// The user canceled the flow
}
is CreateCredentialInterruptedException -> {
// The flow was interrupted (e.g. by another UI element)
}
is CreateCredentialNoCreateOptionException -> {
// No wallet application is available to handle the request
}
is CreateCredentialUnsupportedException -> {
// The device or the system doesn't support this request
}
is CreateCredentialProviderConfigurationException -> {
// There is a configuration issue with the wallet provider
}
is CreateCredentialCustomException -> {
// A protocol-specific error occurred
val errorType = e.type
val errorMessage = e.message
}
is CreateCredentialUnknownException -> {
// An unknown error occurred
}
else -> {
// Generic error handling
}
}
}