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)
कार्ड जारी करने का अनुरोध करना
डिजिटल क्रेडेंशियल बनाने के अनुरोध में, एक 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 फ़ंक्शन का इस्तेमाल करके, उपयोगकर्ता के क्रेडेंशियल जारी करें. यह फ़ंक्शन, क्रेडेंशियल मैनेजर बॉटम शीट सिलेक्टर लॉन्च करता है. इससे उपयोगकर्ता, उस ऐप्लिकेशन को चुन सकता है जिसमें उसे क्रेडेंशियल सेव करना है.
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
}
}
}