1# @ohos.account.distributedAccount (Distributed Account Management) (System API) 2 3The **distributedAccount** module provides APIs for managing distributed accounts, including querying and updating account login states. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> - This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.account.distributedAccount (Distributed Account Management)](js-apis-distributed-account.md). 9 10## Modules to Import 11 12```ts 13import account_distributedAccount from '@ohos.account.distributedAccount'; 14``` 15 16## DistributedAccountAbility 17 18Provides APIs for querying and updating the login state of a distributed account. You must obtain a **DistributedAccountAbility** instance first. 19 20### getOsAccountDistributedInfoByLocalId<sup>10+</sup> 21 22getOsAccountDistributedInfoByLocalId(localId: number, callback: AsyncCallback<DistributedInfo>): void 23 24Obtains distributed information about a system account. This API uses an asynchronous callback to return the result. 25 26**System API**: This is a system API. 27 28**System capability**: SystemCapability.Account.OsAccount 29 30**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS or ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 31 32**Parameters** 33 34 | Name| Type| Mandatory| Description| 35 | -------- | -------- | -------- | -------- | 36 | localId | number | Yes| ID of the target system account.| 37 | callback | AsyncCallback<[DistributedInfo](js-apis-distributed-account.md#distributedinfo)> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **undefined** and **data** is the distributed account information obtained. Otherwise, **err** is an error object.| 38 39**Error codes** 40 41| ID| Error Message| 42| -------- | ------------------- | 43| 12300001 | System service exception. | 44| 12300003 | Account not found. | 45 46**Example** 47 48```ts 49import { BusinessError } from '@ohos.base'; 50 51const accountAbility: account_distributedAccount.DistributedAccountAbility = account_distributedAccount.getDistributedAccountAbility(); 52try { 53 accountAbility.getOsAccountDistributedInfoByLocalId(100, 54 (err: BusinessError, data: account_distributedAccount.DistributedInfo) => { 55 if (err) { 56 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 57 } else { 58 console.log('distributed information: ' + JSON.stringify(data)); 59 } 60 }); 61} catch (err) { 62 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 63} 64``` 65 66### getOsAccountDistributedInfoByLocalId<sup>10+</sup> 67 68getOsAccountDistributedInfoByLocalId(localId: number): Promise<DistributedInfo> 69 70Obtains distributed information about a system account. This API uses a promise to return the result. 71 72**System API**: This is a system API. 73 74**System capability**: SystemCapability.Account.OsAccount 75 76**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS or ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 77 78**Return value** 79 80 | Type| Description| 81 | -------- | -------- | 82 | Promise<[DistributedInfo](js-apis-distributed-account.md#distributedinfo)> | Promise used to return the distributed account information obtained.| 83 84**Error codes** 85 86| ID| Error Message| 87| -------- | ------------------- | 88| 12300001 | System service exception. | 89| 12300003 | Account not found. | 90 91**Example** 92 93```ts 94import { BusinessError } from '@ohos.base'; 95 96const accountAbility: account_distributedAccount.DistributedAccountAbility = account_distributedAccount.getDistributedAccountAbility(); 97try { 98 accountAbility.getOsAccountDistributedInfoByLocalId(100).then(( 99 data: account_distributedAccount.DistributedInfo) => { 100 console.log('distributed information: ' + JSON.stringify(data)); 101 }).catch((err: BusinessError) => { 102 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 103 }); 104} catch (err) { 105 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 106} 107``` 108 109### setOsAccountDistributedInfoByLocalId<sup>10+</sup> 110 111setOsAccountDistributedInfoByLocalId(localId: number, distributedInfo: DistributedInfo, callback: AsyncCallback<void>): void 112 113Sets the distributed information for a system account. This API uses an asynchronous callback to return the result. 114 115**System API**: This is a system API. 116 117**System capability**: SystemCapability.Account.OsAccount 118 119**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 120 121**Parameters** 122 123 | Name| Type| Mandatory| Description| 124 | -------- | -------- | -------- | -------- | 125 | localId | number | Yes| ID of the target system account.| 126 | accountInfo | [DistributedInfo](js-apis-distributed-account.md#distributedinfo) | Yes| Distributed account information to set.| 127 | callback | AsyncCallback<void> | Yes| Callback invoked to return the result. If the distributed information is set successfully, **err** is **undefined**. Otherwise, **err** is an error object.| 128 129**Error codes** 130 131| ID| Error Message| 132| -------- | ------------------- | 133| 12300001 | System service exception. | 134| 12300002 | Invalid distributedInfo. | 135| 12300003 | Account identified by localId or by distributedInfo not found. | 136| 12300008 | Restricted OS account. | 137 138**Example** 139 140```ts 141import { BusinessError } from '@ohos.base'; 142 143const accountAbility: account_distributedAccount.DistributedAccountAbility = account_distributedAccount.getDistributedAccountAbility(); 144let accountInfo: account_distributedAccount.DistributedInfo = 145 {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; 146try { 147 accountAbility.setOsAccountDistributedInfoByLocalId(100, accountInfo, (err: BusinessError) => { 148 if (err) { 149 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 150 } else { 151 console.log('setOsAccountDistributedInfoByLocalId successfully'); 152 } 153 }); 154} catch (err) { 155 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 156} 157``` 158 159### setOsAccountDistributedInfoByLocalId<sup>10+</sup> 160 161setOsAccountDistributedInfoByLocalId(localId: number, distributedInfo: DistributedInfo): Promise<void> 162 163Sets the distributed information for a system account. This API uses a promise to return the result. 164 165**System API**: This is a system API. 166 167**System capability**: SystemCapability.Account.OsAccount 168 169**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 170 171**Parameters** 172 173 | Name| Type| Mandatory| Description| 174 | -------- | -------- | -------- | -------- | 175 | localId | number | Yes| ID of the target system account.| 176 | distributedInfo | [DistributedInfo](js-apis-distributed-account.md#distributedinfo) | Yes| Distributed account information to set.| 177 178**Return value** 179 180 | Type| Description| 181 | -------- | -------- | 182 | Promise<void> | Promise that returns no value.| 183 184**Error codes** 185 186| ID| Error Message| 187| -------- | ------------------- | 188| 12300001 | System service exception. | 189| 12300002 | Invalid distributedInfo. | 190| 12300003 | Account identified by localId or by distributedInfo not found. | 191| 12300008 | Restricted OS account. | 192 193**Example** 194 195```ts 196import { BusinessError } from '@ohos.base'; 197 198const accountAbility: account_distributedAccount.DistributedAccountAbility = account_distributedAccount.getDistributedAccountAbility(); 199let accountInfo: account_distributedAccount.DistributedInfo = 200 {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; 201try { 202 accountAbility.setOsAccountDistributedInfoByLocalId(100, accountInfo).then(() => { 203 console.log('setOsAccountDistributedInfoByLocalId successfully'); 204 }).catch((err: BusinessError) => { 205 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 206 }); 207} catch (err) { 208 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 209} 210``` 211