// The request in the JSON format to conform with// the JSON-ified Credential Manager - Verifier API request definition.valrequestJson=generateRequestFromServer()valdigitalCredentialOption=GetDigitalCredentialOption(requestJson=requestJson)// Use the option from the previous step to build the `GetCredentialRequest`.valgetCredRequest=GetCredentialRequest(listOf(digitalCredentialOption))
coroutineScope.launch{try{valresult=credentialManager.getCredential(context=activityContext,request=getCredRequest)verifyResult(result)}catch(e:GetCredentialException){handleFailure(e)}}// Handle the successfully returned credential.funverifyResult(result:GetCredentialResponse){valcredential=result.credentialwhen(credential){isDigitalCredential->{valresponseJson=credential.credentialJsonvalidateResponseOnServer(responseJson)}else->{// Catch any unrecognized credential type here.Log.e(TAG,"Unexpected type of credential ${credential.type}")}}}// Handle failure.funhandleFailure(e:GetCredentialException){when(e){isGetCredentialCancellationException->{// The user intentionally canceled the operation and chose not// to share the credential.}isGetCredentialInterruptedException->{// Retry-able error. Consider retrying the call.}isNoCredentialException->{// No credential was available.}isCreateCredentialUnknownException->{// An unknown, usually unexpected, error has occurred. Check the// message error for any additional debugging information.}isCreateCredentialCustomException->{// You have encountered a custom error thrown by the wallet.// If you made the API call with a request object that's a// subclass of CreateCustomCredentialRequest using a 3rd-party SDK,// then you should check for any custom exception type constants// within that SDK to match with e.type. Otherwise, drop or log the// exception.}else->Log.w(TAG,"Unexpected exception type ${e::class.java}")}}
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Credential Manager - Verifier API\n\nDigital credential verification within Android apps can be used to authenticate\nand authorize a user's identity (such as a government ID), properties about that\nuser (such as a driver's license, academic degree, or attributes such as age or\naddress), or other scenarios where a credential needs to be issued and verified\nto assert the authenticity of an entity.\n\nDigital Credentials is a public W3C standard that specifies how to access a\nuser's verifiable digital credentials from a digital wallet, and is implemented\nfor web use cases with the [W3C Credential Management API](https://www.w3.org/TR/credential-management-1/). On\nAndroid, Credential Manager's [`DigitalCredential`](/reference/kotlin/androidx/credentials/DigitalCredential) API is used for\nverifying digital credentials.\n| **Note:** The Credential Manager - Verifier API is a public alpha release. Alpha releases are functionally stable, but may not be feature complete and reserve the right to make breaking changes to them as the API evolves.\n\n### Implementation\n\nTo verify digital credentials in your Android project, do the following:\n\n1. Add dependencies to your app's build script and initialize a `CredentialManager` class.\n2. Construct a digital credential request and use it to initialize a `DigitalCredentialOption`, followed by building the `GetCredentialRequest`.\n3. Launch the `getCredential` flow with the constructed request to receive a successful `GetCredentialResponse` or handle any exceptions that may occur. Upon successful retrieval, validate the response.\n\n#### Add dependencies and initialize\n\nAdd the following dependencies to your Gradle build script: \n\n dependencies {\n implementation(\"androidx.credentials:credentials:\")\n implementation(\"androidx.credentials:credentials-play-services-auth:\")\n }\n\nNext, Initialize an instance of the `CredentialManager` class. \n\n val credentialManager = CredentialManager.create(context)\n\n#### Construct a digital credential request\n\nConstruct a digital credential request and use it to initialize a\n`DigitalCredentialOption`. \n\n // The request in the JSON format to conform with\n // the JSON-ified Credential Manager - Verifier API request definition.\n val requestJson = generateRequestFromServer()\n val digitalCredentialOption =\n GetDigitalCredentialOption(requestJson = requestJson)\n\n // Use the option from the previous step to build the `GetCredentialRequest`.\n val getCredRequest = GetCredentialRequest(\n listOf(digitalCredentialOption)\n )\n\nHere is an example of an OpenId4Vp request. A full reference can be found at\nthis [website](https://openid.net/specs/openid-4-verifiable-presentations-1_0.html). \n\n {\n \"digital\": {\n \"requests\": [\n {\n \"protocol\": \"openid4vp-v1-unsigned\",\n \"data\": {\n \"response_type\": \"vp_token\",\n \"response_mode\": \"dc_api\",\n \"nonce\": \"OD8eP8BYfr0zyhgq4QCVEGN3m7C1Ht_No9H5fG5KJFk\",\n \"dcql_query\": {\n \"credentials\": [\n {\n \"id\": \"cred1\",\n \"format\": \"mso_mdoc\",\n \"meta\": {\n \"doctype_value\": \"org.iso.18013.5.1.mDL\"\n },\n \"claims\": [\n {\n \"path\": [\n \"org.iso.18013.5.1\",\n \"family_name\"\n ]\n },\n {\n \"path\": [\n \"org.iso.18013.5.1\",\n \"given_name\"\n ]\n },\n {\n \"path\": [\n \"org.iso.18013.5.1\",\n \"age_over_21\"\n ]\n }\n ]\n }\n ]\n }\n }\n }\n ]\n }\n }\n\n#### Get the credential\n\nLaunch the `getCredential` flow with the constructed request. You will receive\neither a successful `GetCredentialResponse`, or a `GetCredentialException` if\nthe request fails.\n\nThe `getCredential` flow triggers Android system dialogs to present the user's\navailable credential options and collect their selection. Next, the wallet app\nthat contains the chosen credential option will display UIs to collect consent\nand perform actions needed to generate a digital credential response. \n\n coroutineScope.launch {\n try {\n val result = credentialManager.getCredential(\n context = activityContext,\n request = getCredRequest\n )\n verifyResult(result)\n } catch (e : GetCredentialException) {\n handleFailure(e)\n }\n }\n\n // Handle the successfully returned credential.\n fun verifyResult(result: GetCredentialResponse) {\n val credential = result.credential\n when (credential) {\n is DigitalCredential -\u003e {\n val responseJson = credential.credentialJson\n validateResponseOnServer(responseJson)\n }\n else -\u003e {\n // Catch any unrecognized credential type here.\n Log.e(TAG, \"Unexpected type of credential ${credential.type}\")\n }\n }\n }\n\n // Handle failure.\n fun handleFailure(e: GetCredentialException) {\n when (e) {\n is GetCredentialCancellationException -\u003e {\n // The user intentionally canceled the operation and chose not\n // to share the credential.\n }\n is GetCredentialInterruptedException -\u003e {\n // Retry-able error. Consider retrying the call.\n }\n is NoCredentialException -\u003e {\n // No credential was available.\n }\n is CreateCredentialUnknownException -\u003e {\n // An unknown, usually unexpected, error has occurred. Check the\n // message error for any additional debugging information.\n }\n is CreateCredentialCustomException -\u003e {\n // You have encountered a custom error thrown by the wallet.\n // If you made the API call with a request object that's a\n // subclass of CreateCustomCredentialRequest using a 3rd-party SDK,\n // then you should check for any custom exception type constants\n // within that SDK to match with e.type. Otherwise, drop or log the\n // exception.\n }\n else -\u003e Log.w(TAG, \"Unexpected exception type ${e::class.java}\")\n }\n }"]]