核發數位憑證

您可以使用 Credential Manager API,將憑證發給 Android 持有人 (也稱為「錢包」) 應用程式。本指南說明如何將憑證儲存至使用者偏好的保存位置。

導入作業

本節詳細說明核發數位憑證的必要步驟。

新增依附元件

將下列依附元件新增至 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)

建立核發要求

數位憑證建立要求應包含遵循 OpenID4VCI 標準通訊協定的 JSON 字串。以下是 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 函式將憑證核發給使用者。這項函式會啟動 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
        }
    }
}