1# @ohos.account.distributedAccount (Distributed Account Management) 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 9## Modules to Import 10 11```ts 12import account_distributedAccount from '@ohos.account.distributedAccount'; 13``` 14 15## account_distributedAccount.getDistributedAccountAbility 16 17getDistributedAccountAbility(): DistributedAccountAbility 18 19Obtains a **DistributedAccountAbility** instance. 20 21**System capability**: SystemCapability.Account.OsAccount 22 23**Return value** 24 25 | Type| Description| 26 | -------- | -------- | 27 | [DistributedAccountAbility](#distributedaccountability) | **DistributedAccountAbility** instance obtained. This instance provides APIs for querying and updating the login state of a distributed account.| 28 29**Example** 30 ```ts 31 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 32 ``` 33 34## DistributedAccountAbility 35 36Provides APIs for querying and updating the login state of a distributed account. You must obtain a **DistributedAccountAbility** instance first. 37 38### getOsAccountDistributedInfo<sup>9+</sup> 39 40getOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void 41 42Obtains distributed account information. This API uses an asynchronous callback to return the result. 43 44**System capability**: SystemCapability.Account.OsAccount 45 46**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS, ohos.permission.GET_DISTRIBUTED_ACCOUNTS, or ohos.permission.DISTRIBUTED_DATASYNC 47 48**Parameters** 49 50 | Name| Type| Mandatory| Description| 51 | -------- | -------- | -------- | -------- | 52 | callback | AsyncCallback<[DistributedInfo](#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.| 53 54**Error codes** 55 56| ID| Error Message| 57| -------- | ------------------- | 58| 12300001 | System service exception. | 59 60**Example** 61 ```ts 62 import { BusinessError } from '@ohos.base'; 63 64 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 65 try { 66 accountAbility.getOsAccountDistributedInfo( 67 (err: BusinessError, data: account_distributedAccount.DistributedInfo) => { 68 if (err) { 69 console.log('getOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 70 } else { 71 console.log('distributed information: ' + JSON.stringify(data)); 72 } 73 }); 74 } catch (err) { 75 console.log('getOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 76 } 77 ``` 78 79### getOsAccountDistributedInfo<sup>9+</sup> 80 81getOsAccountDistributedInfo(): Promise<DistributedInfo> 82 83Obtains distributed account information. This API uses a promise to return the result. 84 85**System capability**: SystemCapability.Account.OsAccount 86 87**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS, ohos.permission.GET_DISTRIBUTED_ACCOUNTS, or ohos.permission.DISTRIBUTED_DATASYNC 88 89**Return value** 90 91 | Type| Description| 92 | -------- | -------- | 93 | Promise<[DistributedInfo](#distributedinfo)> | Promise used to return the distributed account information obtained.| 94 95**Error codes** 96 97| ID| Error Message| 98| -------- | ------------------- | 99| 12300001 | System service exception. | 100 101**Example** 102 ```ts 103 import { BusinessError } from '@ohos.base'; 104 105 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 106 try { 107 accountAbility.getOsAccountDistributedInfo().then((data: account_distributedAccount.DistributedInfo) => { 108 console.log('distributed information: ' + JSON.stringify(data)); 109 }).catch((err: BusinessError) => { 110 console.log('getOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 111 }); 112 } catch (err) { 113 console.log('getOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 114 } 115 ``` 116 117### getOsAccountDistributedInfoByLocalId<sup>10+</sup> 118 119getOsAccountDistributedInfoByLocalId(localId: number, callback: AsyncCallback<DistributedInfo>): void 120 121Obtains distributed information about an OS account. This API uses an asynchronous callback to return the result. 122 123**System API**: This is a system API. 124 125**System capability**: SystemCapability.Account.OsAccount 126 127**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS or ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 128 129**Parameters** 130 131 | Name| Type| Mandatory| Description| 132 | -------- | -------- | -------- | -------- | 133 | localId | number | Yes| ID of the target OS account.| 134 | callback | AsyncCallback<[DistributedInfo](#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.| 135 136**Error codes** 137 138| ID| Error Message| 139| -------- | ------------------- | 140| 12300001 | System service exception. | 141| 12300003 | Account not found. | 142 143**Example** 144 ```ts 145 import { BusinessError } from '@ohos.base'; 146 147 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 148 try { 149 accountAbility.getOsAccountDistributedInfoByLocalId(100, 150 (err: BusinessError, data: account_distributedAccount.DistributedInfo) => { 151 if (err) { 152 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 153 } else { 154 console.log('distributed information: ' + JSON.stringify(data)); 155 } 156 }); 157 } catch (err) { 158 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 159 } 160 ``` 161 162### getOsAccountDistributedInfoByLocalId<sup>10+</sup> 163 164getOsAccountDistributedInfoByLocalId(localId: number): Promise<DistributedInfo> 165 166Obtains distributed information about an OS account. This API uses a promise to return the result. 167 168**System API**: This is a system API. 169 170**System capability**: SystemCapability.Account.OsAccount 171 172**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS or ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 173 174**Return value** 175 176 | Type| Description| 177 | -------- | -------- | 178 | Promise<[DistributedInfo](#distributedinfo)> | Promise used to return the distributed account information obtained.| 179 180**Error codes** 181 182| ID| Error Message| 183| -------- | ------------------- | 184| 12300001 | System service exception. | 185| 12300003 | Account not found. | 186 187**Example** 188 ```ts 189 import { BusinessError } from '@ohos.base'; 190 191 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 192 try { 193 accountAbility.getOsAccountDistributedInfoByLocalId(100).then(( 194 data: account_distributedAccount.DistributedInfo) => { 195 console.log('distributed information: ' + JSON.stringify(data)); 196 }).catch((err: BusinessError) => { 197 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 198 }); 199 } catch (err) { 200 console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 201 } 202 ``` 203 204### queryOsAccountDistributedInfo<sup>(deprecated)</sup> 205 206queryOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void 207 208Obtains distributed account information. This API uses an asynchronous callback to return the result. 209> **NOTE** 210> 211> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [getOsAccountDistributedInfo](#getosaccountdistributedinfo9). 212 213**System capability**: SystemCapability.Account.OsAccount 214 215**Required permissions**: ohos.permission.MANAGE_LOCAL_ACCOUNTS or ohos.permission.DISTRIBUTED_DATASYNC 216 217**Parameters** 218 219 | Name| Type| Mandatory| Description| 220 | -------- | -------- | -------- | -------- | 221 | callback | AsyncCallback<[DistributedInfo](#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.| 222 223**Example** 224 ```ts 225 import { BusinessError } from '@ohos.base'; 226 227 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 228 accountAbility.queryOsAccountDistributedInfo( 229 (err: BusinessError, data: account_distributedAccount.DistributedInfo) => { 230 if (err) { 231 console.log('queryOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 232 } else { 233 console.log('distributed information: ' + JSON.stringify(data)); 234 } 235 }); 236 ``` 237 238### queryOsAccountDistributedInfo<sup>(deprecated)</sup> 239 240queryOsAccountDistributedInfo(): Promise<DistributedInfo> 241 242Obtains distributed account information. This API uses a promise to return the result. 243 244> **NOTE** 245> 246> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [getOsAccountDistributedInfo](#getosaccountdistributedinfo9-1). 247 248**System capability**: SystemCapability.Account.OsAccount 249 250**Required permissions**: ohos.permission.MANAGE_LOCAL_ACCOUNTS or ohos.permission.DISTRIBUTED_DATASYNC 251 252**Return value** 253 254 | Type| Description| 255 | -------- | -------- | 256 | Promise<[DistributedInfo](#distributedinfo)> | Promise used to return the distributed account information obtained.| 257 258**Example** 259 ```ts 260 import { BusinessError } from '@ohos.base'; 261 262 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 263 accountAbility.queryOsAccountDistributedInfo().then((data: account_distributedAccount.DistributedInfo) => { 264 console.log('distributed information: ' + JSON.stringify(data)); 265 }).catch((err: BusinessError) => { 266 console.log('queryOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 267 }); 268 ``` 269 270### setOsAccountDistributedInfo<sup>9+</sup> 271 272setOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void 273 274Sets the distributed account information. This API uses an asynchronous callback to return the result. 275 276**System capability**: SystemCapability.Account.OsAccount 277 278**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 279 280**Parameters** 281 282 | Name| Type| Mandatory| Description| 283 | -------- | -------- | -------- | -------- | 284 | accountInfo | [DistributedInfo](#distributedinfo) | Yes| Distributed account information to set.| 285 | callback | AsyncCallback<void> | Yes| Callback invoked to return the result. If the distributed account information is set successfully, **err** is **undefined**. Otherwise, **err** is an error object.| 286 287**Error codes** 288 289| ID| Error Message| 290| -------- | ------------------- | 291| 12300001 | System service exception. | 292| 12300002 | Invalid accountInfo. | 293| 12300003 | Account not found. | 294 295**Example** 296 ```ts 297 import { BusinessError } from '@ohos.base'; 298 299 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 300 let accountInfo: account_distributedAccount.DistributedInfo = 301 {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; 302 try { 303 accountAbility.setOsAccountDistributedInfo(accountInfo, (err: BusinessError) => { 304 if (err) { 305 console.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 306 } else { 307 console.log('setOsAccountDistributedInfo successfully'); 308 } 309 }); 310 } catch (err) { 311 console.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 312 } 313 ``` 314 315### setOsAccountDistributedInfo<sup>9+</sup> 316 317setOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void> 318 319Sets the distributed account information. This API uses a promise to return the result. 320 321**System capability**: SystemCapability.Account.OsAccount 322 323**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 324 325**Parameters** 326 327 | Name| Type| Mandatory| Description| 328 | -------- | -------- | -------- | -------- | 329 | accountInfo | [DistributedInfo](#distributedinfo) | Yes| Distributed account information to set.| 330 331**Return value** 332 333 | Type| Description| 334 | -------- | -------- | 335 | Promise<void> | Promise that returns no value.| 336 337**Error codes** 338 339| ID| Error Message| 340| -------- | ------------------- | 341| 12300001 | System service exception. | 342| 12300002 | Invalid accountInfo. | 343| 12300003 | Account not found. | 344 345**Example** 346 ```ts 347 import { BusinessError } from '@ohos.base'; 348 349 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 350 let accountInfo: account_distributedAccount.DistributedInfo = 351 {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; 352 try { 353 accountAbility.setOsAccountDistributedInfo(accountInfo).then(() => { 354 console.log('setOsAccountDistributedInfo successfully'); 355 }).catch((err: BusinessError) => { 356 console.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 357 }); 358 } catch (err) { 359 console.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 360 } 361 ``` 362### setOsAccountDistributedInfoByLocalId<sup>10+</sup> 363 364setOsAccountDistributedInfoByLocalId(localId: number, distributedInfo: DistributedInfo, callback: AsyncCallback<void>): void 365 366Sets the distributed information for an OS account. This API uses an asynchronous callback to return the result. 367 368**System API**: This is a system API. 369 370**System capability**: SystemCapability.Account.OsAccount 371 372**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 373 374**Parameters** 375 376 | Name| Type| Mandatory| Description| 377 | -------- | -------- | -------- | -------- | 378 | localId | number | Yes| ID of the target OS account.| 379 | accountInfo | [DistributedInfo](#distributedinfo) | Yes| Distributed account information to set.| 380 | 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.| 381 382**Error codes** 383 384| ID| Error Message| 385| -------- | ------------------- | 386| 12300001 | System service exception. | 387| 12300002 | Invalid distributedInfo. | 388| 12300003 | Account identified by localId or by distributedInfo not found. | 389| 12300008 | Restricted OS account. | 390 391**Example** 392 ```ts 393 import { BusinessError } from '@ohos.base'; 394 395 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 396 let accountInfo: account_distributedAccount.DistributedInfo = 397 {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; 398 try { 399 accountAbility.setOsAccountDistributedInfoByLocalId(100, accountInfo, (err: BusinessError) => { 400 if (err) { 401 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 402 } else { 403 console.log('setOsAccountDistributedInfoByLocalId successfully'); 404 } 405 }); 406 } catch (err) { 407 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 408 } 409 ``` 410 411### setOsAccountDistributedInfoByLocalId<sup>10+</sup> 412 413setOsAccountDistributedInfoByLocalId(localId: number, distributedInfo: DistributedInfo): Promise<void> 414 415Sets the distributed information for an OS account. This API uses a promise to return the result. 416 417**System API**: This is a system API. 418 419**System capability**: SystemCapability.Account.OsAccount 420 421**Required permissions**: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS 422 423**Parameters** 424 425 | Name| Type| Mandatory| Description| 426 | -------- | -------- | -------- | -------- | 427 | localId | number | Yes| ID of the target OS account.| 428 | distributedInfo | [DistributedInfo](#distributedinfo) | Yes| Distributed account information to set.| 429 430**Return value** 431 432 | Type| Description| 433 | -------- | -------- | 434 | Promise<void> | Promise that returns no value.| 435 436**Error codes** 437 438| ID| Error Message| 439| -------- | ------------------- | 440| 12300001 | System service exception. | 441| 12300002 | Invalid distributedInfo. | 442| 12300003 | Account identified by localId or by distributedInfo not found. | 443| 12300008 | Restricted OS account. | 444 445**Example** 446 ```ts 447 import { BusinessError } from '@ohos.base'; 448 449 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 450 let accountInfo: account_distributedAccount.DistributedInfo = 451 {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; 452 try { 453 accountAbility.setOsAccountDistributedInfoByLocalId(100, accountInfo).then(() => { 454 console.log('setOsAccountDistributedInfoByLocalId successfully'); 455 }).catch((err: BusinessError) => { 456 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 457 }); 458 } catch (err) { 459 console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err)); 460 } 461 ``` 462 463### updateOsAccountDistributedInfo<sup>(deprecated)</sup> 464 465updateOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void 466 467Updates the distributed account information. This API uses an asynchronous callback to return the result. 468 469> **NOTE** 470> 471> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [setOsAccountDistributedInfo](#setosaccountdistributedinfo9). 472 473**System capability**: SystemCapability.Account.OsAccount 474 475**Required permissions**: ohos.permission.MANAGE_LOCAL_ACCOUNTS 476 477**Parameters** 478 479 | Name| Type| Mandatory| Description| 480 | -------- | -------- | -------- | -------- | 481 | accountInfo | [DistributedInfo](#distributedinfo) | Yes| New distributed account information.| 482 | callback | AsyncCallback<void> | Yes| Callback invoked to return the result. If the distributed account information is set successfully, **err** is **undefined**. Otherwise, **err** is an error object.| 483 484**Example** 485 ```ts 486 import { BusinessError } from '@ohos.base'; 487 488 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 489 let accountInfo: account_distributedAccount.DistributedInfo = 490 {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; 491 accountAbility.updateOsAccountDistributedInfo(accountInfo, (err: BusinessError) => { 492 if (err) { 493 console.log('queryOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 494 } else { 495 console.log('queryOsAccountDistributedInfo successfully'); 496 } 497 }); 498 ``` 499 500### updateOsAccountDistributedInfo<sup>(deprecated)</sup> 501 502updateOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void> 503 504Updates the distributed account information. This API uses a promise to return the result. 505> **NOTE** 506> 507> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [setOsAccountDistributedInfo](#setosaccountdistributedinfo9-1). 508**System capability**: SystemCapability.Account.OsAccount 509 510**Required permissions**: ohos.permission.MANAGE_LOCAL_ACCOUNTS 511 512**Parameters** 513 514 | Name| Type| Mandatory| Description| 515 | -------- | -------- | -------- | -------- | 516 | accountInfo | [DistributedInfo](#distributedinfo) | Yes| New distributed account information.| 517 518**Return value** 519 520 | Type| Description| 521 | -------- | -------- | 522 | Promise<void> | Promise that returns no value.| 523 524**Example** 525 ```ts 526 import { BusinessError } from '@ohos.base'; 527 528 const accountAbility = account_distributedAccount.getDistributedAccountAbility(); 529 let accountInfo: account_distributedAccount.DistributedInfo = 530 {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; 531 accountAbility.updateOsAccountDistributedInfo(accountInfo).then(() => { 532 console.log('updateOsAccountDistributedInfo successfully'); 533 }).catch((err: BusinessError) => { 534 console.log('updateOsAccountDistributedInfo exception: ' + JSON.stringify(err)); 535 }); 536 ``` 537## DistributedInfo 538 539Defines the distributed information about an OS account. 540 541**System capability**: SystemCapability.Account.OsAccount 542 543| Name| Type| Mandatory| Description| 544| -------- | -------- | -------- | -------- | 545| name | string |Yes| Name of the distributed account. It must be a non-null string.| 546| id | string |Yes| UID of the distributed account. It must be a non-null string.| 547| event | string |Yes| Login state of the distributed account. The state can be login, logout, token invalid, or logoff, which correspond to the following strings respectively:<br>- Ohos.account.event.LOGIN<br>- Ohos.account.event.LOGOUT<br>- Ohos.account.event.TOKEN_INVALID<br>- Ohos.account.event.LOGOFF | 548| nickname<sup>9+</sup> | string |No| Nickname of the distributed account. By default, no value is passed.| 549| avatar<sup>9+</sup> | string |No| Avatar of the distributed account. By default, no value is passed.| 550| status<sup>10+</sup> | [DistributedAccountStatus](#distributedaccountstatus10) |No| Status of the distributed account. The value is of the enumerated type. The default status is unlogged.| 551| scalableData<sup>8+</sup> | object |No| Extended information about the distributed account, passed in key-value (KV) pairs based on service requirements. By default, no value is passed.| 552 553## DistributedAccountStatus<sup>10+</sup> 554 555Enumerates the statuses of a distributed account. 556 557**System capability**: SystemCapability.Account.OsAccount 558 559| Name | Value| Description | 560| ---- | ------ | ----------- | 561| NOT_LOGGED_IN | 0 | The account has not logged in.| 562| LOGGED_IN | 1 | The account has logged in.| 563