1# OAID Service 2 3## When to Use 4 5An Open Anonymous Device Identifier (OAID) is a non-permanent device identifier. The OAID service is useful for media application developers, ad platforms, and tracking platforms alike. Specifically, it provides personalized ads for users while protecting their personal data privacy, and also interact with third-party tracking platforms to provide conversion attribution analysis for advertisers. 6 7An OAID is a 32-bit Universally Unique Identifier (UUID) generated using a Huawei algorithm. The format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. 8 9The OAID has the following features: 10- The OAID is device specific. Different applications on the same device obtain the same OAID. 11- The OAID obtained depends on the switch of the **ohos.permission.APP_TRACKING_CONSENT** permission. When the switch is enabled, the application obtains a valid OAID that is not all zeros. When the switch is disabled, the application obtains an all-zero OAID. 12- An OAID is generated when any application on the device enables the **ohos.permission.APP_TRACKING_CONSENT** permission for the first time. 13 14The OAID changes in the following scenarios: 15- A user restores the factory settings of the device. 16- A user resets the OAID. 17 18### Available APIs 19 20| API| Description| 21| -------- | -------- | 22| <!--Del-->[<!--DelEnd-->getOAID<!--Del-->](../../reference/apis-ads-kit/js-apis-oaid.md#identifiergetoaid)<!--DelEnd-->(): Promise<string> | Obtains an OAID. This API uses a promise to return the result.| 23| <!--Del-->[<!--DelEnd-->getOAID<!--Del-->](../../reference/apis-ads-kit/js-apis-oaid.md#identifiergetoaid-1)<!--DelEnd-->(callback: AsyncCallback<string>): void | Obtains an OAID. This API uses an asynchronous callback to return the result.| 24 25> **NOTE** 26> To call **getOAID()**, the application must request the permission **ohos.permission.APP_TRACKING_CONSENT** and user authorization. Three situations are possible: 27> 28> - If the application has configured the permission **ohos.permission.APP_TRACKING_CONSENT** and the permission is allowed, the OAID is returned. 29> - If the application has configured the permission **ohos.permission.APP_TRACKING_CONSENT** and the permission is disallowed, 00000000-0000-0000-0000-000000000000 is returned. 30> - If the application has not configured the permission **ohos.permission.APP_TRACKING_CONSENT**, 00000000-0000-0000-0000-000000000000 is returned. 31 32 33## How to Develop 341. In the **module.json5** file of the module, configure the permission [ohos.permission.APP_TRACKING_CONSENT](../../security/AccessToken/permissions-for-all-user.md#ohospermissionapp_tracking_consent), which is a user_grant permission. In this case, the **reason** and **abilities** fields are mandatory. For details about the configuration, see [Declaring Permissions in the Configuration File](../../security/AccessToken/declare-permissions.md#declaring-permissions-in-the-configuration-file). 35 36 The sample code is as follows: 37 38 ```ts 39 { 40 "module": { 41 "requestPermissions": [ 42 { 43 "name": "ohos.permission.APP_TRACKING_CONSENT", 44 "reason": "$string:reason", 45 "usedScene": { 46 "abilities": [ 47 "EntryFormAbility" 48 ], 49 "when": "inuse" 50 } 51 } 52 ] 53 } 54 } 55 ``` 56 572. To obtain the OAID, the application must call **requestPermissionFromUser** to obtain the corresponding permission. For details about how to obtain the context, see [Context](../../application-models/application-context-stage.md). 58 59 The sample code is as follows: 60 61 ```ts 62 import { identifier } from '@kit.AdsKit'; 63 import { abilityAccessCtrl, common } from '@kit.AbilityKit'; 64 import { hilog } from '@kit.PerformanceAnalysisKit'; 65 import { BusinessError } from '@kit.BasicServicesKit'; 66 67 function requestOAIDTrackingConsentPermissions(context: common.Context): void { 68 // When the page is displayed, request the user to grant the permission ohos.permission.APP_TRACKING_CONSENT. 69 const atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 70 try { 71 atManager.requestPermissionsFromUser(context, ["ohos.permission.APP_TRACKING_CONSENT"]).then((data) => { 72 if (data.authResults[0] == 0) { 73 hilog.info(0x0000, 'testTag', '%{public}s', 'succeeded in requesting permission'); 74 identifier.getOAID((err: BusinessError, data: string) => { 75 if (err.code) { 76 hilog.error(0x0000, 'testTag', '%{public}s', `get oaid failed, error: ${err.code} ${err.message}`); 77 } else { 78 const oaid: string = data; 79 hilog.info(0x0000, 'testTag', '%{public}s', `succeeded in getting oaid by callback , oaid: ${oaid}`); 80 } 81 }); 82 } else { 83 hilog.error(0x0000, 'testTag', '%{public}s', 'user rejected'); 84 } 85 }).catch((err: BusinessError) => { 86 hilog.error(0x0000, 'testTag', '%{public}s', `request permission failed, error: ${err.code} ${err.message}`); 87 }) 88 } catch (err) { 89 hilog.error(0x0000, 'testTag', '%{public}s', `catch err->${err.code}, ${err.message}`); 90 } 91 } 92 ``` 93