ออกข้อมูลเข้าสู่ระบบดิจิทัล

Credential Manager API ช่วยให้คุณออกข้อมูลเข้าสู่ระบบให้กับแอปผู้ถือ (หรือที่เรียกว่า "กระเป๋าเงิน") ใน 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
        }
    }
}