@ohos.account.distributedAccount (Distributed Account Management)

The distributedAccount module provides APIs for managing distributed accounts, including querying and updating account login states.

NOTE

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.

Modules to Import

import account_distributedAccount from '@ohos.account.distributedAccount';

account_distributedAccount.getDistributedAccountAbility

getDistributedAccountAbility(): DistributedAccountAbility

Obtains a DistributedAccountAbility instance.

System capability: SystemCapability.Account.OsAccount

Return value

Type Description
DistributedAccountAbility DistributedAccountAbility instance obtained. This instance provides APIs for querying and updating the login state of a distributed account.

Example

const accountAbility = account_distributedAccount.getDistributedAccountAbility();

DistributedAccountAbility

Provides APIs for querying and updating the login state of a distributed account. You must obtain a DistributedAccountAbility instance first.

getOsAccountDistributedInfo9+

getOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void

Obtains distributed account information. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS, ohos.permission.GET_DISTRIBUTED_ACCOUNTS, or ohos.permission.DISTRIBUTED_DATASYNC

Parameters

Name Type Mandatory Description
callback AsyncCallback<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.

Error codes

ID Error Message
12300001 System service exception.

Example

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
try {
  accountAbility.getOsAccountDistributedInfo((err, data) => {
    console.log("getOsAccountDistributedInfo err: " + JSON.stringify(err));
    console.log('Query account info name: ' + data.name);
    console.log('Query account info id: ' + data.id);
  });
} catch (e) {
  console.log("getOsAccountDistributedInfo exception: " + JSON.stringify(e));
}

getOsAccountDistributedInfo9+

getOsAccountDistributedInfo(): Promise<DistributedInfo>

Obtains distributed account information. This API uses a promise to return the result.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS, ohos.permission.GET_DISTRIBUTED_ACCOUNTS, or ohos.permission.DISTRIBUTED_DATASYNC

Return value

Type Description
Promise<DistributedInfo> Promise used to return the distributed account information obtained.

Error codes

ID Error Message
12300001 System service exception.

Example

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
try {
  accountAbility.getOsAccountDistributedInfo().then((data) => {
      console.log('Query account info name: ' + data.name);
      console.log('Query account info id: ' + data.id);
  }).catch((err) => {
      console.log("getOsAccountDistributedInfo err: "  + JSON.stringify(err));
  });
} catch (e) {
  console.log("getOsAccountDistributedInfo exception: " + JSON.stringify(e));
}

queryOsAccountDistributedInfo(deprecated)

queryOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void

Obtains distributed account information. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use getOsAccountDistributedInfo.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS or ohos.permission.DISTRIBUTED_DATASYNC

Parameters

Name Type Mandatory Description
callback AsyncCallback<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.

Example

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
accountAbility.queryOsAccountDistributedInfo((err, data) => {
    console.log("queryOsAccountDistributedInfo err: " + JSON.stringify(err));
    console.log('Query account info name: ' + data.name);
    console.log('Query account info id: ' + data.id);
});

queryOsAccountDistributedInfo(deprecated)

queryOsAccountDistributedInfo(): Promise<DistributedInfo>

Obtains distributed account information. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use getOsAccountDistributedInfo.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS or ohos.permission.DISTRIBUTED_DATASYNC

Return value

Type Description
Promise<DistributedInfo> Promise used to return the distributed account information obtained.

Example

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
accountAbility.queryOsAccountDistributedInfo().then((data) => {
    console.log('Query account info name: ' + data.name);
    console.log('Query account info id: ' + data.id);
}).catch((err) => {
    console.log("queryOsAccountDistributedInfoerr: "  + JSON.stringify(err));
});

setOsAccountDistributedInfo9+

setOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void

Sets the distributed account information. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS

Parameters

Name Type Mandatory Description
accountInfo DistributedInfo Yes Distributed account information to set.
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.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid accountInfo.
12300003 Account not found.

Example

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
let accountInfo = {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
try {
  accountAbility.setOsAccountDistributedInfo(accountInfo, (err) => {
      console.log("setOsAccountDistributedInfo err: " + JSON.stringify(err));
  });
} catch (e) {
  console.log("setOsAccountDistributedInfo exception: " + JSON.stringify(e));
}

setOsAccountDistributedInfo9+

setOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>

Sets the distributed account information. This API uses a promise to return the result.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS

Parameters

Name Type Mandatory Description
accountInfo DistributedInfo Yes Distributed account information to set.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid accountInfo.

Example

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
let accountInfo = {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
try {
  accountAbility.setOsAccountDistributedInfo(accountInfo).then(() => {
      console.log('setOsAccountDistributedInfo Success');
  }).catch((err) => {
      console.log("setOsAccountDistributedInfo err: "  + JSON.stringify(err));
  });
} catch (e) {
  console.log("setOsAccountDistributedInfo exception: " + JSON.stringify(e));
}

updateOsAccountDistributedInfo(deprecated)

updateOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void

Updates the distributed account information. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use setOsAccountDistributedInfo.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS

Parameters

Name Type Mandatory Description
accountInfo DistributedInfo Yes New distributed account information.
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.

Example

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
let accountInfo = {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
accountAbility.updateOsAccountDistributedInfo(accountInfo, (err) => {
    console.log("queryOsAccountDistributedInfo err: " + JSON.stringify(err));
});

updateOsAccountDistributedInfo(deprecated)

updateOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>

Updates the distributed account information. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use setOsAccountDistributedInfo. System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS

Parameters

Name Type Mandatory Description
accountInfo DistributedInfo Yes New distributed account information.

Return value

Type Description
Promise<void> Promise that returns no value.

Example

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
let accountInfo = {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
accountAbility.updateOsAccountDistributedInfo(accountInfo).then(() => {
    console.log('updateOsAccountDistributedInfo Success');
 }).catch((err) => {
    console.log("updateOsAccountDistributedInfo err: "  + JSON.stringify(err));
});

DistributedInfo

Defines distributed OS account information.

System capability: SystemCapability.Account.OsAccount

Name Type Mandatory Description
name string Yes Name of the distributed account. It must be a non-null string.
id string Yes UID of the distributed account. It must be a non-null string.
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:
- Ohos.account.event.LOGIN
- Ohos.account.event.LOGOUT
- Ohos.account.event.TOKEN_INVALID
- Ohos.account.event.LOGOFF
nickname9+ string No Nickname of the distributed account. It must be a non-null string.
avatar9+ string No Avatar of the distributed account. It must be a non-null string.
scalableData object No Extended information about the distributed account, passed in key-value (KV) pairs.
NOTE
This parameter is reserved and not used in the setters and getters.