harmony 鸿蒙@ohos.account.appAccount (App Account Management)

  • 2022-08-09
  • 浏览 (655)

@ohos.account.appAccount (App Account Management)

The appAccount module provides APIs for adding, deleting, modifying, and querying app account information, and supports inter-app authentication and distributed data synchronization.

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_appAccount from '@ohos.account.appAccount';

account_appAccount.createAppAccountManager

createAppAccountManager(): AppAccountManager

Creates an AppAccountManager object.

System capability: SystemCapability.Account.AppAccount

Return value

Type Description
AppAccountManager AppAccountManager object created.

Example

  let appAccountManager = account_appAccount.createAppAccountManager();

AppAccountManager

Implements app account management.

createAccount9+

createAccount(name: string, callback: AsyncCallback<void>): void;

Creates an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the app account to create.
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name.
12300004 Account already exists.
12300007 The number of accounts reaches the upper limit.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.createAccount('WangWu', (err: BusinessError) => { 
        console.log('createAccount err: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('createAccount err: ' + JSON.stringify(err));
  }

createAccount9+

createAccount(name: string, options: CreateAccountOptions, callback: AsyncCallback<void>): void

Creates an app account with custom data. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the app account to create.
options CreateAccountOptions Yes Options for creating the app account. You can customize data based on service requirements, but do not add sensitive data (such as passwords and tokens).
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or options.
12300004 Account already exists.
12300007 The number of accounts reaches the upper limit.

Example

  import { BusinessError } from '@ohos.base';
  
  let options:account_appAccount.CreateAccountOptions  = {
    customData: {
      age: '10'
    }
  }
  try {
    appAccountManager.createAccount('LiSi', options, (err: BusinessError) => {
      if (err) {
        console.log('createAccount failed, error: ' + JSON.stringify(err));
      } else {
        console.log('createAccount successfully');
      }
    });
  } catch(err) {
    console.log('createAccount exception: ' + JSON.stringify(err));
  }

createAccount9+

createAccount(name: string, options?: CreateAccountOptions): Promise<void>

Creates an app account with custom data. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the app account to create.
options CreateAccountOptions No Options for creating the app account. You can customize data based on service requirements, but do not add sensitive data (such as passwords and tokens).
By default, no value is passed, which means no additional information needs to be added for the account.

Return value

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

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or options.
12300004 Account already exists.
12300007 The number of accounts reaches the upper limit.

Example

  import { BusinessError } from '@ohos.base';

  let options: account_appAccount.CreateAccountOptions = {
    customData: {
      age: '10'
    }
  }
  try {
    appAccountManager.createAccount('LiSi', options).then(() => {
      console.log('createAccount successfully');
    }).catch((err: BusinessError) => {
      console.log('createAccount failed, error: ' + JSON.stringify(err));
    });
  } catch(err) {
    console.log('createAccount exception: ' + JSON.stringify(err));
  }

createAccountImplicitly9+

createAccountImplicitly(owner: string, callback: AuthCallback): void

Creates an app account implicitly based on the specified account owner. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Owner of the app account. The value is the bundle name of the app.
callback AuthCallback Yes Authenticator callback used to return the result.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid owner.
12300007 The number of accounts reaches the upper limit.
12300010 Account service busy.
12300113 Authenticator service not found.
12300114 Authenticator service exception.

Example

  import { BusinessError } from '@ohos.base';
  import Want from '@ohos.app.ability.Want';
  import common from '@ohos.app.ability.common';

  let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext

  function onResultCallback(code: number, result?: account_appAccount.AuthResult): void {
    console.log('resultCode: ' + code);
    console.log('result: ' + JSON.stringify(result));
  }

  function onRequestRedirectedCallback(request: Want): void {
    let wantInfo: Want = {
      deviceId: '',
      bundleName: 'com.example.accountjsdemo',
      action: 'ohos.want.action.viewData',
      entities: ['entity.system.default'],
    }
    context.startAbility(wantInfo).then(() => {
      console.log('startAbility successfully');
    }).catch((err: BusinessError) => {
      console.log('startAbility err: ' + JSON.stringify(err));
    })
  }

  try {  
    appAccountManager.createAccountImplicitly('com.example.accountjsdemo', {
      onResult: onResultCallback,
      onRequestRedirected: onRequestRedirectedCallback
    });
  } catch (err) {
    console.log('createAccountImplicitly exception: ' + JSON.stringify(err));
  }

createAccountImplicitly9+

createAccountImplicitly(owner: string, options: CreateAccountImplicitlyOptions, callback: AuthCallback): void

Creates an app account implicitly based on the specified account owner and options. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Owner of the app account. The value is the bundle name of the app.
options CreateAccountImplicitlyOptions Yes Options for implicitly creating the account.
callback AuthCallback Yes Authenticator callback used to return the result.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid owner or options.
12300007 The number of accounts reaches the upper limit.
12300010 Account service busy.
12300113 Authenticator service not found.
12300114 Authenticator service exception.

Example

  import { BusinessError } from '@ohos.base';
  import Want from '@ohos.app.ability.Want';
  import common from '@ohos.app.ability.common';

  let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext

  function onResultCallback(code: number, result?: account_appAccount.AuthResult): void {
    console.log('resultCode: ' + code);
    console.log('result: ' + JSON.stringify(result));
  }

  function onRequestRedirectedCallback(request: Want): void {
    let wantInfo: Want = {
      deviceId: '',
      bundleName: 'com.example.accountjsdemo',
      action: 'ohos.want.action.viewData',
      entities: ['entity.system.default'],
    }
    context.startAbility(wantInfo).then(() => {
      console.log('startAbility successfully');
    }).catch((err: BusinessError) => {
      console.log('startAbility err: ' + JSON.stringify(err));
    })
  }

  let options: account_appAccount.CreateAccountImplicitlyOptions = {
    authType: 'getSocialData',
    requiredLabels: [ 'student' ]
  };
  try {
    appAccountManager.createAccountImplicitly('com.example.accountjsdemo', options, {
      onResult: onResultCallback,
      onRequestRedirected: onRequestRedirectedCallback
    });
  } catch (err) {
    console.log('createAccountImplicitly exception: ' + JSON.stringify(err));
  }

removeAccount9+

removeAccount(name: string, callback: AsyncCallback<void>): void

Removes an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the app account to remove.
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Error codes

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

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.removeAccount('ZhaoLiu', (err: BusinessError) => {
      if (err) {
        console.log('removeAccount failed, error: ' + JSON.stringify(err));
      } else {
        console.log('removeAccount successfully');
      }
   });
  } catch(err) {
    console.log('removeAccount exception: ' + JSON.stringify(err));
  }

removeAccount9+

removeAccount(name: string): Promise<void>

Removes an app account. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the app account to remove.

Return value

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

Error codes

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

Example

  import { BusinessError } from '@ohos.base';

  try {
    appAccountManager.removeAccount('Lisi').then(() => {
      console.log('removeAccount successfully');
    }).catch((err: BusinessError) => {
      console.log('removeAccount failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('removeAccount exception: ' + JSON.stringify(err));
  }

setAppAccess9+

setAppAccess(name: string, bundleName: string, isAccessible: boolean, callback: AsyncCallback<void>): void

Sets the access to the data of an account for an app. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
bundleName string Yes Bundle name of the app.
isAccessible boolean Yes Whether the access is allowed. The value true means to allow the access; the value false means the opposite.
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or bundleName.
12300003 Account not found.
12400001 Application not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.setAppAccess('ZhangSan', 'com.example.accountjsdemo', true, (err: BusinessError) => {
      if (err) {
        console.log('setAppAccess failed: ' + JSON.stringify(err));
      } else {
        console.log('setAppAccess successfully');
      }
    });
  } catch (err) {
    console.log('setAppAccess exception: ' + JSON.stringify(err));
  }

setAppAccess9+

setAppAccess(name: string, bundleName: string, isAccessible: boolean): Promise<void>

Sets the access to the data of an account for an app. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
bundleName string Yes Bundle name of the app.
isAccessible boolean Yes Whether the access is allowed. The value true means to allow the access; the value false means the opposite.

Return value

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

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or bundleName.
12300003 Account not found.
12400001 Application not found.

Example

  import { BusinessError } from '@ohos.base';

  try {
    appAccountManager.setAppAccess('ZhangSan', 'com.example.accountjsdemo', true).then(() => {
      console.log('setAppAccess successfully');
    }).catch((err: BusinessError) => {
      console.log('setAppAccess failed: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('setAppAccess exception: ' + JSON.stringify(err));
  }

checkAppAccess9+

checkAppAccess(name: string, bundleName: string, callback: AsyncCallback<boolean>): void

Checks whether an app can access the data of an account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
bundleName string Yes Bundle name of the app.
callback AsyncCallback<boolean> Yes Callback invoked to return the result. The value true means the app can access the account data; the value false means the opposite.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or bundleName.
12300003 Account not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.checkAppAccess('ZhangSan', 'com.example.accountjsdemo',
      (err: BusinessError, isAccessible: boolean) => {
        if (err) {
          console.log('checkAppAccess failed, error: ' + JSON.stringify(err));
        } else {
          console.log('checkAppAccess successfully');
        }
      });
  } catch (err) {
    console.log('checkAppAccess exception: ' + JSON.stringify(err));
  }

checkAppAccess9+

checkAppAccess(name: string, bundleName: string): Promise<boolean>

Checks whether an app can access the data of an account. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
bundleName string Yes Bundle name of the app.

Return value

Type Description
Promise<boolean> Promise used to return the result. The value true means the app can access the account data; the value false means the opposite.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or bundleName.
12300003 Account not found.

Example

  import { BusinessError } from '@ohos.base';

  try {
    appAccountManager.checkAppAccess('ZhangSan', 'com.example.accountjsdemo').then((isAccessible: boolean) => {
      console.log('checkAppAccess successfully, isAccessible: ' + isAccessible);
    }).catch((err: BusinessError) => {
      console.log('checkAppAccess failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('checkAppAccess exception: ' + JSON.stringify(err));
  }

setDataSyncEnabled9+

setDataSyncEnabled(name: string, isEnabled: boolean, callback: AsyncCallback<void>): void

Sets data synchronization for an app account. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
isEnabled boolean Yes Whether to enable data synchronization.
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Error codes

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

Example

  import { BusinessError } from '@ohos.base';
  
  try {
      appAccountManager.setDataSyncEnabled('ZhangSan', true, (err: BusinessError) => { 
          console.log('setDataSyncEnabled err: ' + JSON.stringify(err));
      });
  } catch (err) {
      console.log('setDataSyncEnabled err: ' + JSON.stringify(err));
  }

setDataSyncEnabled9+

setDataSyncEnabled(name: string, isEnabled: boolean): Promise<void>

Sets data synchronization for an app account. This API uses a promise to return the result.

Required permissions: ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
isEnabled boolean Yes Whether to enable data synchronization.

Return value

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

Error codes

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

Example

  import { BusinessError } from '@ohos.base';

  try {
      appAccountManager .setDataSyncEnabled('ZhangSan', true).then(() => { 
          console.log('setDataSyncEnabled Success');
      }).catch((err: BusinessError) => {
          console.log('setDataSyncEnabled err: ' + JSON.stringify(err));
      });
  } catch (err) {
      console.log('setDataSyncEnabled err: ' + JSON.stringify(err));
  }

checkDataSyncEnabled9+

checkDataSyncEnabled(name: string, callback: AsyncCallback<boolean>): void

Checks whether data synchronization is enabled for an app account. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
callback AsyncCallback<boolean> Yes Callback invoked to return the result. The value true means data synchronization is enabled for the app account; the value false means the opposite.

Error codes

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

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.checkDataSyncEnabled('ZhangSan', (err: BusinessError, isEnabled: boolean) => {
      if (err) {
        console.log('checkDataSyncEnabled failed, err: ' + JSON.stringify(err));
      } else {
        console.log('checkDataSyncEnabled successfully, isEnabled: ' + isEnabled);
      }
    });
  } catch (err) {
    console.log('checkDataSyncEnabled err: ' + JSON.stringify(err));
  }

checkDataSyncEnabled9+

checkDataSyncEnabled(name: string): Promise<boolean>

Checks whether data synchronization is enabled for an app account. This API uses a promise to return the result.

Required permissions: ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.

Return value

Type Description
Promise<boolean> Promise used to return the result. The value true means data synchronization is enabled for the app account; the value false means the opposite.

Error codes

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

Example

  import { BusinessError } from '@ohos.base';

  try {
    appAccountManager.checkDataSyncEnabled('ZhangSan').then((isEnabled: boolean) => {
        console.log('checkDataSyncEnabled successfully, isEnabled: ' + isEnabled);
    }).catch((err: BusinessError) => {
      console.log('checkDataSyncEnabled failed, err: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('checkDataSyncEnabled err: ' + JSON.stringify(err));
  }

setCredential9+

setCredential(name: string, credentialType: string, credential: string,callback: AsyncCallback<void>): void

Sets a credential for an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
credentialType string Yes Type of the credential to set.
credential string Yes Credential value.
callback AsyncCallback<void> Yes Callback invoked to return the result. If the credential is set successfully, err is null. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, credentialType, or credential.
12300003 Account not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.setCredential('ZhangSan', 'PIN_SIX', 'xxxxxx', (err: BusinessError) => {
      if (err) {
        console.log('setCredential failed, error: ' + JSON.stringify(err));
      } else {
        console.log('setCredential successfully');
      }
    });
  } catch (err) {
    console.log('setCredential exception: ' + JSON.stringify(err));
  }

setCredential9+

setCredential(name: string, credentialType: string, credential: string): Promise<void>

Sets a credential for an app account. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
credentialType string Yes Type of the credential to set.
credential string Yes Credential value.

Return value

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

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, credentialType, or credential.
12300003 Account not found.

Example

  import { BusinessError } from '@ohos.base';

  try {
    appAccountManager.setCredential('ZhangSan', 'PIN_SIX', 'xxxxxx').then(() => {
      console.log('setCredential successfully');
    }).catch((err: BusinessError) => {
      console.log('setCredential failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('setCredential exception: ' + JSON.stringify(err));
  }

getCredential9+

getCredential(name: string, credentialType: string, callback: AsyncCallback<string>): void

Obtains the credential of an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
credentialType string Yes Type of the credential to obtain.
callback AsyncCallback<string> Yes Callback invoked to return the result. If the operation is successful, err is null and data is the credential obtained. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or credentialType.
12300003 Account not found.
12300102 Credential not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
      appAccountManager.getCredential('ZhangSan', 'PIN_SIX', (err: BusinessError, result: string) => { 
        if (err) {
          console.log('getCredential failed, error: ' + JSON.stringify(err));
        } else {
          console.log('getCredential successfully, result: ' + result);
        }
      });
  } catch (err) {
      console.log('getCredential err: ' + JSON.stringify(err));
  }

getCredential9+

getCredential(name: string, credentialType: string): Promise<string>

Obtains the credential of an app account. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
credentialType string Yes Type of the credential to obtain.

Return value

Type Description
Promise<string> Promise used to return the credential obtained.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or credentialType.
12300003 Account not found.
12300102 Credential not found.

Example

  import { BusinessError } from '@ohos.base';

  try {
    appAccountManager.getCredential('ZhangSan', 'PIN_SIX').then((credential: string) => {
        console.log('getCredential successfully, credential: ' + credential);
    }).catch((err: BusinessError) => {
        console.log('getCredential failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('getCredential exception: ' + JSON.stringify(err));
  }

setCustomData9+

setCustomData(name: string, key: string, value: string, callback: AsyncCallback<void>): void

Sets custom data for an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
key string Yes Key of the custom data to set.
value string Yes Value of the custom data to set.
callback AsyncCallback<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, key, or value.
12300003 Account not found.
12400003 The number of custom data reaches the upper limit.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.setCustomData('ZhangSan', 'age', '12', (err: BusinessError) => {
      if (err) {
        console.log('setCustomData failed, error: ' + JSON.stringify(err));
      } else {
        console.log('setCustomData successfully');
      }
    });
  } catch (err) {
    console.log('setCustomData exception: ' + JSON.stringify(err));
  }

setCustomData9+

setCustomData(name: string, key: string, value: string): Promise<void>

Sets custom data for an app account. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
key string Yes Key of the custom data to set.
value string Yes Value of the custom data to set.

Return value

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

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, key, or value.
12300003 Account not found.
12400003 The number of custom data reaches the upper limit.

Example

  import { BusinessError } from '@ohos.base';

  try {
    appAccountManager.setCustomData('ZhangSan', 'age', '12').then(() => {
      console.log('setCustomData successfully');
    }).catch((err: BusinessError) => {
      console.log('setCustomData failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('setCustomData exception: ' + JSON.stringify(err));
  }

getCustomData9+

getCustomData(name: string, key: string, callback: AsyncCallback<string>): void

Obtains the custom data of an app account based on the specified key. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
key string Yes Key of the custom data to obtain.
callback AsyncCallback<string> Yes Callback invoked to return the result. If the operation is successful, err is null and data is the custom data value obtained. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or key.
12300003 Account not found.
12400002 Custom data not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.getCustomData('ZhangSan', 'age', (err: BusinessError, data: string) => {
      if (err) {
        console.log('getCustomData failed, error: ' + err);
      } else {
        console.log('getCustomData successfully, data: ' + data);
      }
    });
  } catch (err) {
    console.log('getCustomData exception: ' + JSON.stringify(err));
  }

getCustomData9+

getCustomData(name: string, key: string): Promise<string>

Obtains the custom data of an app account based on the specified key. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
key string Yes Key of the custom data to obtain.

Return value

Type Description
Promise<string> Promise used to return the custom data value obtained.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or key.
12300003 Account not found.
12400002 Custom data not found.

Example

  import { BusinessError } from '@ohos.base';

  try {
    appAccountManager.getCustomData('ZhangSan', 'age').then((data: string) => {
      console.log('getCustomData successfully, data: ' + data);
    }).catch((err: BusinessError) => {
      console.log('getCustomData failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('getCustomData exception: ' + JSON.stringify(err));
  }

getCustomDataSync9+

getCustomDataSync(name: string, key: string): string;

Obtains the custom data of an app account based on the specified key. The API returns the result synchronously.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
key string Yes Key of the custom data to obtain.

Return value

Type Description
string Value of the custom data obtained.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or key.
12300003 Account not found.
12400002 Custom data not found.

Example

  try {
      let value = appAccountManager.getCustomDataSync('ZhangSan', 'age');
      console.info('getCustomDataSync successfully, vaue: ' + value);
  } catch (err) {
    console.error('getCustomDataSync failed, error: ' + JSON.stringify(err));
  }

getAllAccounts9+

getAllAccounts(callback: AsyncCallback<Array<AppAccountInfo>>): void

Obtains information about all accessible app accounts. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
callback AsyncCallback<Array<AppAccountInfo>> Yes Callback invoked to return the result. If the operation is successful, err is null and data is a list of accessible app accounts. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.getAllAccounts((err: BusinessError, data: account_appAccount.AppAccountInfo[]) => {
      if (err) {
        console.debug('getAllAccounts failed, error: ' + JSON.stringify(err));
      } else {
        console.debug('getAllAccounts successfully');
      }
    });
  } catch (err) {
      console.debug('getAllAccounts exception: ' + JSON.stringify(err));
  }

getAllAccounts9+

getAllAccounts(): Promise<Array<AppAccountInfo>>

Obtains information about all accessible app accounts. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Return value

Type Description
Promise<Array<AppAccountInfo>> Promise used to return information about all accessible accounts.

Error codes

ID Error Message
12300001 System service exception.

Example

  import { BusinessError } from '@ohos.base';

  try {
    appAccountManager.getAllAccounts().then((data: account_appAccount.AppAccountInfo[]) => {
      console.debug('getAllAccounts successfully');
    }).catch((err: BusinessError) => {
      console.debug('getAllAccounts failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.debug('getAllAccounts exception: ' + JSON.stringify(err));
  }

getAccountsByOwner9+

getAccountsByOwner(owner: string, callback: AsyncCallback<Array<AppAccountInfo>>): void

Obtains the app accounts that can be accessed by the invoker based on the app account owner. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Owner of the app account. The value is the bundle name of the app.
callback AsyncCallback<Array<AppAccountInfo>> Yes Callback invoked to return the result. If the operation is successful, err is null and data is the app account information obtained. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid owner.
12400001 Application not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.getAccountsByOwner('com.example.accountjsdemo2',
      (err: BusinessError, data: account_appAccount.AppAccountInfo[]) => {
        if (err) {
          console.debug('getAccountsByOwner failed, error:' + JSON.stringify(err));
        } else {
          console.debug('getAccountsByOwner successfully, data:' + JSON.stringify(data));
        }
      });
  } catch (err) {
    console.debug('getAccountsByOwner exception:' + JSON.stringify(err));
  }

getAccountsByOwner9+

getAccountsByOwner(owner: string): Promise<Array<AppAccountInfo>>

Obtains the app accounts that can be accessed by the invoker based on the app account owner. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Owner of the app account. The value is the bundle name of the app.

Return value

Type Description
Promise<Array<AppAccountInfo>> Promise used to return the app account information obtained.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid owner.
12400001 Application not found.

Example

  import { BusinessError } from '@ohos.base';

  try {
    appAccountManager.getAccountsByOwner('com.example.accountjsdemo2').then((
      data: account_appAccount.AppAccountInfo[]) => {
      console.debug('getAccountsByOwner successfully, data: ' + JSON.stringify(data));
    }).catch((err: BusinessError) => {
      console.debug('getAccountsByOwner failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.debug('getAccountsByOwner exception: ' + JSON.stringify(err));
  }

on(‘accountChange’)9+

on(type: ‘accountChange’, owners: Array<string>, callback: Callback<Array<AppAccountInfo>>): void

Subscribes to account information changes of apps.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
type ‘accountChange’ Yes Event type to subscribe to. The value is ‘accountChange’. An event will be reported when the account information of the target app changes.
owners Array<string> Yes App bundle names of the account.
callback Callback<Array<AppAccountInfo>> Yes Callback registered to return the list of changed app accounts.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid type or owners.
12400001 Application not found.

Example

  function changeOnCallback(data: account_appAccount.AppAccountInfo[]): void {
  	console.log('receive change data:' + JSON.stringify(data));
  }
  try{
  	appAccountManager.on('accountChange', ['com.example.actsaccounttest'], changeOnCallback);
  } catch(err) {
  	console.error('on accountChange failed, error:' + JSON.stringify(err));
  }

off(‘accountChange’)9+

off(type: ‘accountChange’, callback?: Callback<Array<AppAccountInfo>>): void

Unsubscribes from account information changes.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
type ‘accountChange’ Yes Event type to unsubscribe from. The value is ‘accountChange’.
callback Callback<Array<AppAccountInfo>> No Callback to unregister. By default, no value is passed, which means to unregister all callbacks for the specified event.

Error codes

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

Example

  function changeOnCallback(data: account_appAccount.AppAccountInfo[]): void {
  	console.log('receive change data:' + JSON.stringify(data));
  }
  try{
  	appAccountManager.on('accountChange', ['com.example.actsaccounttest'], changeOnCallback);
  } catch(err) {
  	console.error('on accountChange failed, error:' + JSON.stringify(err));
  }
  try{
  	appAccountManager.off('accountChange', changeOnCallback);
  }
  catch(err){
  	console.error('off accountChange failed, error:' + JSON.stringify(err));
  }

auth9+

auth(name: string, owner: string, authType: string, callback: AuthCallback): void

Authenticates an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type.
callback AuthCallback Yes Callback invoked to return the authentication result.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, owner, or authType.
12300003 Account not found.
12300010 Account service busy.
12300113 Authenticator service not found.
12300114 Authenticator service exception.

Example

  import { BusinessError } from '@ohos.base';
  import Want from '@ohos.app.ability.Want';
  import common from '@ohos.app.ability.common';

  let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext

  function onResultCallback(code: number, authResult?: account_appAccount.AuthResult): void {
    console.log('resultCode: ' + code);
    console.log('authResult: ' + JSON.stringify(authResult));
  }

  function onRequestRedirectedCallback(request: Want): void {
    let wantInfo: Want = {
      deviceId: '',
      bundleName: 'com.example.accountjsdemo',
      action: 'ohos.want.action.viewData',
      entities: ['entity.system.default'],
    }
    context.startAbility(wantInfo).then(() => {
      console.log('startAbility successfully');
    }).catch((err: BusinessError) => {
      console.log('startAbility err: ' + JSON.stringify(err));
    })
  }

  try {
    appAccountManager.auth('LiSi', 'com.example.accountjsdemo', 'getSocialData', {
        onResult: onResultCallback,
        onRequestRedirected: onRequestRedirectedCallback
    });
  } catch (err) {
    console.log('auth exception: ' + JSON.stringify(err));
  }

auth9+

auth(name: string, owner: string, authType: string, options: {[key: string]: Object}, callback: AuthCallback): void

Authenticates an app account with customized options. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type.
options {[key: string]: Object} Yes Options for the authentication.
callback AuthCallback Yes Callback invoked to return the authentication result.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, owner, authType, or options.
12300003 Account not found.
12300010 Account service busy.
12300113 Authenticator service not found.
12300114 Authenticator service exception.

Example

  import { BusinessError } from '@ohos.base';
  import Want from '@ohos.app.ability.Want';
  import common from '@ohos.app.ability.common';

  let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext

  function onResultCallback(code: number, authResult?: account_appAccount.AuthResult): void {
    console.log('resultCode: ' + code);
    console.log('authResult: ' + JSON.stringify(authResult));
  }

  function onRequestRedirectedCallback(request: Want): void {
    let wantInfo: Want = {
      deviceId: '',
      bundleName: 'com.example.accountjsdemo',
      action: 'ohos.want.action.viewData',
      entities: ['entity.system.default'],
    }
    context.startAbility(wantInfo).then(() => {
      console.log('startAbility successfully');
    }).catch((err: BusinessError) => {
      console.log('startAbility err: ' + JSON.stringify(err));
    })
  }

  let options: Record<string, Object> = {
    'password': 'xxxx',
  };
  try {
    appAccountManager.auth('LiSi', 'com.example.accountjsdemo', 'getSocialData', options, {
        onResult: onResultCallback,
        onRequestRedirected: onRequestRedirectedCallback
    });
  } catch (err) {
    console.log('auth exception: ' + JSON.stringify(err));
  }

getAuthToken9+

getAuthToken(name: string, owner: string, authType: string, callback: AsyncCallback&lt;string&gt;): void

Obtains the authorization token of the specified authentication type for an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type.
callback AsyncCallback&lt;string&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is the authorization token value obtained. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, owner, or authType.
12300003 Account not found.
12300107 AuthType not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.getAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData',
      (err: BusinessError, token: string) => {
        if (err) {
          console.log('getAuthToken failed, error: ' + JSON.stringify(err));
        } else {
          console.log('getAuthToken successfully, token: ' + token);
        }
      });
  } catch (err) {
      console.log('getAuthToken exception: ' + JSON.stringify(err));
  }

getAuthToken9+

getAuthToken(name: string, owner: string, authType: string): Promise&lt;string&gt;

Obtains the authorization token of the specified authentication type for an app account. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type.

Return value

Type Description
Promise&lt;string&gt; Promise used to return the authorization token obtained.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, owner, or authType.
12300003 Account not found.
12300107 AuthType not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.getAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData').then((token: string) => {
      console.log('getAuthToken successfully, token: ' + token);
    }).catch((err: BusinessError) => {
      console.log('getAuthToken failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
      console.log('getAuthToken exception: ' + JSON.stringify(err));
  }

setAuthToken9+

setAuthToken(name: string, authType: string, token: string, callback: AsyncCallback&lt;void&gt;): void

Sets an authorization token of the specific authentication type for an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
token string Yes Token to set.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, authType, or token.
12300003 Account not found.
12400004 The number of tokens reaches the upper limit.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.setAuthToken('LiSi', 'getSocialData', 'xxxx', (err: BusinessError) => {
      if (err) {
        console.log('setAuthToken failed, error: ' + JSON.stringify(err));
      } else {
        console.log('setAuthToken successfully');
      }
    });
  } catch (err) {
    console.log('setAuthToken exception: ' + JSON.stringify(err));
  }

setAuthToken9+

setAuthToken(name: string, authType: string, token: string): Promise&lt;void&gt;

Sets an authorization token of the specific authentication type for an app account. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
token string Yes Token to set.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, authType, or token.
12300003 Account not found.
12400004 The number of tokens reaches the upper limit.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.setAuthToken('LiSi', 'getSocialData', 'xxxx').then(() => {
        console.log('setAuthToken successfully');
    }).catch((err: BusinessError) => {
        console.log('setAuthToken failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('setAuthToken exception: ' + JSON.stringify(err));
  }

deleteAuthToken9+

deleteAuthToken(name: string, owner: string, authType: string, token: string, callback: AsyncCallback&lt;void&gt;): void

Deletes the authorization token of the specified authentication type for an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type.
token string Yes Authorization token to delete.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, owner, authType, or token.
12300003 Account not found.
12300107 AuthType not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.deleteAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData', 'xxxxx',
      (err: BusinessError) => {
        if (err) {
          console.log('deleteAuthToken failed, error: ' + JSON.stringify(err));
        } else {
          console.log('deleteAuthToken successfully');
        }
      });
  } catch (err) {
    console.log('deleteAuthToken exception: ' + JSON.stringify(err));
  }

deleteAuthToken9+

deleteAuthToken(name: string, owner: string, authType: string, token: string): Promise&lt;void&gt;

Deletes the authorization token of the specified authentication type for an app account. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type.
token string Yes Authorization token to delete.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, owner, authType, or token.
12300003 Account not found.
12300107 AuthType not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.deleteAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData', 'xxxxx').then(() => {
      console.log('deleteAuthToken successfully');
    }).catch((err: BusinessError) => {
      console.log('deleteAuthToken failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('deleteAuthToken exception: ' + JSON.stringify(err));
  }

setAuthTokenVisibility9+

setAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean, callback: AsyncCallback&lt;void&gt;): void

Sets the visibility of an authorization token to an app. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
bundleName string Yes Bundle name of the app.
isVisible boolean Yes Whether the authorization token is visible to the app. The value true means the authorization token is visible to the app; the value false means the opposite.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, authType, or bundleName.
12300003 Account not found.
12300107 AuthType not found.
12400001 Application not found.
12400005 The size of authorization list reaches the upper limit.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.setAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo', true,
      (err: BusinessError) => {
        if (err) {
          console.log('setAuthTokenVisibility failed, error: ' + JSON.stringify(err));
        } else {
          console.log('setAuthTokenVisibility successfully');
        }
      });
  } catch (err) {
      console.log('setAuthTokenVisibility exception: ' + JSON.stringify(err));
  }

setAuthTokenVisibility9+

setAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean): Promise&lt;void&gt;

Sets the visibility of an authorization token to an app. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
bundleName string Yes Bundle name of the app.
isVisible boolean Yes Whether the authorization token is visible to the app. The value true means the authorization token is visible to the app; the value false means the opposite.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, authType, or bundleName.
12300003 Account not found.
12300107 AuthType not found.
12400001 Application not found.
12400005 The size of authorization list reaches the upper limit.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.setAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo', true).then(() => {
      console.log('setAuthTokenVisibility successfully');
    }).catch((err: BusinessError) => {
      console.log('setAuthTokenVisibility failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('setAuthTokenVisibility exception: ' + JSON.stringify(err));
  }

checkAuthTokenVisibility9+

checkAuthTokenVisibility(name: string, authType: string, bundleName: string, callback: AsyncCallback&lt;boolean&gt;): void

Checks the visibility of an authorization token of the specified authentication type to an app. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
bundleName string Yes Bundle name of the app.
callback AsyncCallback&lt;boolean&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data can be true (the authorization token is visible to the app) or false (the authorization token is not visible to the app). If the operation fails, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, authType, or bundleName.
12300003 Account not found.
12300107 AuthType not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.checkAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo',
      (err: BusinessError, isVisible: boolean) => {
        if (err) {
          console.log('checkAuthTokenVisibility failed, error: ' + JSON.stringify(err));
        } else {
          console.log('checkAuthTokenVisibility successfully, isVisible: ' + isVisible);
        }
      });
  } catch (err) {
    console.log('checkAuthTokenVisibility exception: ' + JSON.stringify(err));
  }

checkAuthTokenVisibility9+

checkAuthTokenVisibility(name: string, authType: string, bundleName: string): Promise&lt;boolean&gt;

Checks the visibility of an authorization token of the specified authentication type to an app. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
bundleName string Yes Bundle name of the app.

Return value

Type Description
Promise&lt;boolean&gt; Promise used to return the result. The value true means the authorization token is visible to the app; the value false means the opposite.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, authType, or bundleName.
12300003 Account not found.
12300107 AuthType not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.checkAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo').then((
      isVisible: boolean) => {
      console.log('checkAuthTokenVisibility successfully, isVisible: ' + isVisible);
    }).catch((err: BusinessError) => {
      console.log('checkAuthTokenVisibility failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('checkAuthTokenVisibility exception: ' + JSON.stringify(err));
  }

getAllAuthTokens9+

getAllAuthTokens(name: string, owner: string, callback: AsyncCallback&lt;Array&lt;AuthTokenInfo&gt;&gt;): void

Obtains all tokens visible to the invoker for an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
callback AsyncCallback&lt;Array&lt;AuthTokenInfo&gt;&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is a list of all tokens visible to the invoker. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or owner.
12300003 Account not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.getAllAuthTokens('LiSi', 'com.example.accountjsdemo',
      (err: BusinessError, tokenArr: account_appAccount.AuthTokenInfo[]) => {
        if (err) {
          console.log('getAllAuthTokens failed, error: ' + JSON.stringify(err));
        } else {
          console.log('getAllAuthTokens successfully, tokenArr: ' + tokenArr);
        }
      });
  } catch (err) {
    console.log('getAllAuthTokens exception: ' + JSON.stringify(err));
  }

getAllAuthTokens9+

getAllAuthTokens(name: string, owner: string): Promise&lt;Array&lt;AuthTokenInfo&gt;&gt;

Obtains all tokens visible to the invoker for an app account. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.

Return value

Type Description
Promise&lt;Array&lt;AuthTokenInfo&gt;&gt; Promise used to return the tokens obtained.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or owner.
12300003 Account not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.getAllAuthTokens('LiSi', 'com.example.accountjsdemo').then((
      tokenArr: account_appAccount.AuthTokenInfo[]) => {
      console.log('getAllAuthTokens successfully, tokenArr: ' + JSON.stringify(tokenArr));
    }).catch((err: BusinessError) => {
      console.log('getAllAuthTokens failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('getAllAuthTokens exception: ' + JSON.stringify(err));
  }

getAuthList9+

getAuthList(name: string, authType: string, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void

Obtains the authorization list of the specified authentication type for an app account. The authorization list contains all authorized bundles. The token authorization list is set by setAuthTokenVisibility. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
callback AsyncCallback&lt;Array&lt;string&gt;&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is a list of authorized bundles obtained. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or authType.
12300003 Account not found.
12300107 AuthType not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.getAuthList('LiSi', 'getSocialData', (err: BusinessError, authList: string[]) => {
      if (err) {
        console.log('getAuthList failed, error: ' + JSON.stringify(err));
      } else {
        console.log('getAuthList successfully, authList: ' + authList);
      }
    });
  } catch (err) {
    console.log('getAuthList exception: ' + JSON.stringify(err));
  }

getAuthList9+

getAuthList(name: string, authType: string): Promise&lt;Array&lt;string&gt;&gt;

Obtains the authorization list of the specified authentication type for an app account. The authorization list contains all authorized bundles. The token authorization list is set by setAuthTokenVisibility. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.

Return value

Type Description
Promise&lt;Array&lt;string&gt;&gt; Promise used to return a list of authorized bundles.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or authType.
12300003 Account not found.
12300107 AuthType not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.getAuthList('LiSi', 'getSocialData').then((authList: string[]) => {
        console.log('getAuthList successfully, authList: ' + authList);
    }).catch((err: BusinessError) => {
        console.log('getAuthList failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('getAuthList exception: ' + JSON.stringify(err));
  }

getAuthCallback9+

getAuthCallback(sessionId: string, callback: AsyncCallback&lt;AuthCallback&gt;): void

Obtains the authenticator callback for an authentication session. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
sessionId string Yes ID of the authentication session.
callback AsyncCallback&lt;AuthCallback&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is the authenticator callback object obtained. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid sessionId.
12300108 Session not found.

Example

  import { BusinessError } from '@ohos.base';
  import UIAbility from '@ohos.app.ability.UIAbility';
  import Want from '@ohos.app.ability.Want';
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';

  export default class EntryAbility extends UIAbility {
    onCreate(want: Want, param: AbilityConstant.LaunchParam) { // Ability lifecycle function.
      let sessionId: string = want.parameters![account_appAccount.Constants.KEY_SESSION_ID] as string;
      try {
        appAccountManager.getAuthCallback(sessionId, (err: BusinessError, callback: account_appAccount.AuthCallback) => {
          if (err != null) {
              console.log('getAuthCallback err: ' + JSON.stringify(err));
              return;
          }
          let result: account_appAccount.AuthResult = {
            account: {
              name: 'Lisi',
              owner: 'com.example.accountjsdemo',
            },
            tokenInfo: {
              token: 'xxxxxx',
              authType: 'getSocialData'
            }
          }; 
          callback.onResult(0, result);
        });
      } catch (err) {
          console.log('getAuthCallback exception: ' + JSON.stringify(err));
      }
    }
  }

getAuthCallback9+

getAuthCallback(sessionId: string): Promise&lt;AuthCallback&gt;

Obtains the authenticator callback for an authentication session. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
sessionId string Yes ID of the authentication session.

Return value

Type Description
Promise&lt;AuthCallback&gt; Promise used to return the authenticator callback obtained.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid sessionId.
12300108 Session not found.

Example

  import { BusinessError } from '@ohos.base';
  import UIAbility from '@ohos.app.ability.UIAbility';
  import Want from '@ohos.app.ability.Want';
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';

  export default class EntryAbility extends UIAbility {
    onCreate(want: Want, param: AbilityConstant.LaunchParam) { // Ability lifecycle function.
      let sessionId: string = want.parameters![account_appAccount.Constants.KEY_SESSION_ID] as string;
      try {
        appAccountManager.getAuthCallback(sessionId).then((callback: account_appAccount.AuthCallback) => {
        let result: account_appAccount.AuthResult = {
          account: {
            name: 'Lisi',
            owner: 'com.example.accountjsdemo',
          },
          tokenInfo: {
            token: 'xxxxxx',
            authType: 'getSocialData'
          }
        };
        callback.onResult(0, result);
        }).catch((err: BusinessError) => {
          console.log('getAuthCallback err: ' + JSON.stringify(err));
        });
      } catch (err) {
        console.log('getAuthCallback exception: ' + JSON.stringify(err));
      }
    }
  }

queryAuthenticatorInfo9+

queryAuthenticatorInfo(owner: string, callback: AsyncCallback&lt;AuthenticatorInfo&gt;): void

Obtains the authenticator information of an app. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Bundle name of the app.
callback AsyncCallback&lt;AuthenticatorInfo&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is the authenticator information obtained. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid owner.
12300113 Authenticator service not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.queryAuthenticatorInfo('com.example.accountjsdemo',
      (err: BusinessError, info: account_appAccount.AuthenticatorInfo) => {
        if (err) {
          console.log('queryAuthenticatorInfo failed, error: ' + JSON.stringify(err));
        } else {
          console.log('queryAuthenticatorInfo successfully, info: ' + JSON.stringify(info));
        }
      });
  } catch (err) {
    console.log('queryAuthenticatorInfo exception: ' + JSON.stringify(err));
  }

queryAuthenticatorInfo9+

queryAuthenticatorInfo(owner: string): Promise&lt;AuthenticatorInfo&gt;

Obtains the authenticator information of an app. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Bundle name of the app.

Return value

Type Description
Promise&lt;AuthenticatorInfo&gt; Promise used to return the authenticator information obtained.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid owner.
12300113 Authenticator service not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.queryAuthenticatorInfo('com.example.accountjsdemo').then((
      info: account_appAccount.AuthenticatorInfo) => { 
      console.log('queryAuthenticatorInfo successfully, info: ' + JSON.stringify(info));
    }).catch((err: BusinessError) => {
      console.log('queryAuthenticatorInfo failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('queryAuthenticatorInfo exception: ' + JSON.stringify(err));
  }

checkAccountLabels9+

checkAccountLabels(name: string, owner: string, labels: Array&lt;string&gt;, callback: AsyncCallback&lt;boolean&gt;): void;

Checks whether an app account has specific labels. This API uses an asynchronous callback to return the result. The labels are checked by the authenticator of the target app.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
labels Array&lt;string&gt; Yes Labels to check.
callback AsyncCallback&lt;boolean&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data can be true or false. The value true means the app account has the labels; the value false means the opposite. If the operation fails, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, owner, or labels.
12300003 Account not found.
12300010 Account service busy.
12300113 Authenticator service not found.
12300114 Authenticator service exception.

Example

  import { BusinessError } from '@ohos.base';
  
  let labels = ['student'];
  try {
    appAccountManager.checkAccountLabels('zhangsan', 'com.example.accountjsdemo', labels,
      (err: BusinessError, hasAllLabels: boolean) => {
        if (err) {
          console.log('checkAccountLabels failed, error: ' + JSON.stringify(err));
        } else {
          console.log('checkAccountLabels successfully, hasAllLabels: ' + hasAllLabels);
        }
      });
  } catch (err) {
    console.log('checkAccountLabels exception: ' + JSON.stringify(err));
  }

checkAccountLabels9+

checkAccountLabels(name: string, owner: string, labels: Array&lt;string&gt;): Promise&lt;boolean&gt;

Checks whether an app account has specific labels. This API uses a promise to return the result. The labels are checked by the authenticator of the target app.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
labels Array&lt;string&gt; Yes Labels to check.

Return value

Type Description
Promise&lt;boolean&gt; Promise used to return the result. The value true means the app account has the labels; the value false means the opposite.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, owner, or labels.
12300003 Account not found.
12300010 Account service busy.
12300113 Authenticator service not found.
12300114 Authenticator service exception.

Example

  import { BusinessError } from '@ohos.base';
  
  let labels = ['student'];
  try {
    appAccountManager.checkAccountLabels('zhangsan', 'com.example.accountjsdemo', labels).then((
      hasAllLabels: boolean) => {
      console.log('checkAccountLabels successfully: ' + hasAllLabels);
    }).catch((err: BusinessError) => {
      console.log('checkAccountLabels failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('checkAccountLabels exception: ' + JSON.stringify(err));
  }

deleteCredential9+

deleteCredential(name: string, credentialType: string, callback: AsyncCallback&lt;void&gt;): void

Deletes the credential of the specified type from an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
credentialType string Yes Type of the credential to delete.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or credentialType.
12300003 Account not found.
12300102 Credential not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.deleteCredential('zhangsan', 'PIN_SIX', (err: BusinessError) => {
      if (err) {
        console.log('deleteCredential failed, error: ' + JSON.stringify(err));
      } else {
        console.log('deleteCredential successfully');
      }
    });
  } catch (err) {
    console.log('deleteCredential exception: ' + JSON.stringify(err));
  }

deleteCredential9+

deleteCredential(name: string, credentialType: string): Promise&lt;void&gt;

Deletes the credential of the specified type from an app account. This API uses a promise to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
credentialType string Yes Type of the credential to delete.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or credentialType.
12300003 Account not found.
12300102 Credential not found.

Example

  import { BusinessError } from '@ohos.base';
  
  try {
    appAccountManager.deleteCredential('zhangsan', 'PIN_SIX').then(() => {
      console.log('deleteCredential successfully');
    }).catch((err: BusinessError) => {
      console.log('deleteCredential failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('deleteCredential exception: ' + JSON.stringify(err));
  }

selectAccountsByOptions9+

selectAccountsByOptions(options: SelectAccountsOptions, callback: AsyncCallback&lt;Array&lt;AppAccountInfo&gt;&gt;): void

Selects the accounts that can be accessed by the invoker based on the options. This API uses an asynchronous callback to return the result. If the options contain label constraints, the authenticator of the target app provides the capability of checking the labels.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
options SelectAccountsOptions Yes Options for selecting accounts.
callback AsyncCallback&lt;Array&lt;AppAccountInfo&gt;&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is a list of accounts selected. Otherwise, err is an error object.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid options.
12300010 Account service busy.
12300114 Authenticator service exception.

Example

  import { BusinessError } from '@ohos.base';
  
  let options: account_appAccount.SelectAccountsOptions = {
    allowedOwners: [ 'com.example.accountjsdemo' ],
    requiredLabels: [ 'student' ]
  };
  try {
    appAccountManager.selectAccountsByOptions(options,
      (err: BusinessError, accountArr: account_appAccount.AppAccountInfo[]) => {
        if (err) {
          console.log('selectAccountsByOptions failed, error: ' + JSON.stringify(err));
        } else {
          console.log('selectAccountsByOptions successfully, accountArr: ' + JSON.stringify(accountArr));
        }
      });
  } catch (err) {
    console.log('selectAccountsByOptions exception: ' + JSON.stringify(err));
  }

selectAccountsByOptions9+

selectAccountsByOptions(options: SelectAccountsOptions): Promise&lt;Array&lt;AppAccountInfo&gt;&gt;

Selects the accounts that can be accessed by the invoker based on the options. This API uses a promise to return the result. If the options contain label constraints, the authenticator of the target app provides the capability of checking the labels.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
options SelectAccountsOptions Yes Options for selecting accounts.

Return value

Type Description
Promise&lt;AppAccountInfo&gt; Promise used to return the accounts selected.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid options.
12300010 Account service busy.
12300114 Authenticator service exception.

Example

  import { BusinessError } from '@ohos.base';
  
  let options: account_appAccount.SelectAccountsOptions = {
    allowedOwners: ['com.example.accountjsdemo']
  };
  try {
    appAccountManager.selectAccountsByOptions(options).then((accountArr: account_appAccount.AppAccountInfo[]) => {
      console.log('selectAccountsByOptions successfully, accountArr: ' + JSON.stringify(accountArr));
    }).catch((err: BusinessError) => {
      console.log('selectAccountsByOptions failed, error: ' + JSON.stringify(err));
    });
  } catch (err) {
    console.log('selectAccountsByOptions exception: ' + JSON.stringify(err));
  }

verifyCredential9+

verifyCredential(name: string, owner: string, callback: AuthCallback): void;

Verifies the credential of an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
callback AuthCallback Yes Callback invoked to return the result.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name or owner.
12300003 Account not found.
12300010 Account service busy.
12300113 Authenticator service not found.
12300114 Authenticator service exception.

Example

  import Want from '@ohos.app.ability.Want';

  try {
      appAccountManager.verifyCredential('zhangsan', 'com.example.accountjsdemo', {
          onResult: (resultCode: number, result?: account_appAccount.AuthResult) => {
              console.log('verifyCredential onResult, resultCode: ' + JSON.stringify(resultCode));
              console.log('verifyCredential onResult, result: ' + JSON.stringify(result));
          },
          onRequestRedirected: (request: Want) => {
              console.log('verifyCredential onRequestRedirected, request: ' + JSON.stringify(request));
          }
      });
  } catch (err) {
      console.log('verifyCredential err: ' + JSON.stringify(err));
  }

verifyCredential9+

verifyCredential(name: string, owner: string, options: VerifyCredentialOptions, callback: AuthCallback): void;

Verifies the user credential. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
options VerifyCredentialOptions Yes Options for verifying the user credential.
callback AuthCallback Yes Callback invoked to return the result.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid name, owner, or options.
12300003 Account not found.
12300010 Account service busy.
12300113 Authenticator service not found.
12300114 Authenticator service exception.

Example

  import Want from '@ohos.app.ability.Want';

  let options: account_appAccount.VerifyCredentialOptions = {
    credentialType: 'pin',
    credential: '123456'
  };
  try {
    appAccountManager.verifyCredential('zhangsan', 'com.example.accountjsdemo', options, {
      onResult: (resultCode: number, result?: account_appAccount.AuthResult) => {
        console.log('verifyCredential onResult, resultCode: ' + JSON.stringify(resultCode));
        console.log('verifyCredential onResult, result: ' + JSON.stringify(result));
      },
      onRequestRedirected: (request: Want) => {
        console.log('verifyCredential onRequestRedirected, request: ' + JSON.stringify(request));
      }
    });
  } catch (err) {
    console.log('verifyCredential err: ' + JSON.stringify(err));
  }

setAuthenticatorProperties9+

setAuthenticatorProperties(owner: string, callback: AuthCallback): void;

Sets the authenticator attributes of an app. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Owner of the authenticator.
callback AuthCallback Yes Callback invoked to return the result.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid owner.
12300010 Account service busy.
12300113 Authenticator service not found.
12300114 Authenticator service exception.

Example

  import Want from '@ohos.app.ability.Want';

  try {
    appAccountManager.setAuthenticatorProperties('com.example.accountjsdemo', {
      onResult: (resultCode: number, result?: account_appAccount.AuthResult) => {
        console.log('setAuthenticatorProperties onResult, resultCode: ' + JSON.stringify(resultCode));
        console.log('setAuthenticatorProperties onResult, result: ' + JSON.stringify(result));
      },
      onRequestRedirected: (request: Want) => {
        console.log('setAuthenticatorProperties onRequestRedirected, request: ' + JSON.stringify(request));
      }
    });
  } catch (err) {
    console.log('setAuthenticatorProperties err: ' + JSON.stringify(err));
  }

setAuthenticatorProperties9+

setAuthenticatorProperties(owner: string, options: SetPropertiesOptions, callback: AuthCallback): void;

Set authenticator properties. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Owner of the authenticator.
options SetPropertiesOptions Yes Authenticator properties to set.
callback AuthCallback Yes Authenticator callback invoked to return the result.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid owner or options.
12300010 Account service busy.
12300113 Authenticator service not found.
12300114 Authenticator service exception.

Example

  import Want from '@ohos.app.ability.Want';

  let options: account_appAccount.SetPropertiesOptions = {
    properties: {prop1: 'value1'}
  };
  try {
    appAccountManager.setAuthenticatorProperties('com.example.accountjsdemo', options, {
      onResult: (resultCode: number, result?: account_appAccount.AuthResult) => {
        console.log('setAuthenticatorProperties onResult, resultCode: ' + JSON.stringify(resultCode));
        console.log('setAuthenticatorProperties onResult, result: ' + JSON.stringify(result));
      },
      onRequestRedirected: (request: Want) => {
        console.log('setAuthenticatorProperties onRequestRedirected, request: ' + JSON.stringify(request));
      }
    });
  } catch (err) {
    console.log('setAuthenticatorProperties err: ' + JSON.stringify(err));
  } 

addAccount(deprecated)

addAccount(name: string, callback: AsyncCallback&lt;void&gt;): void

Adds an app account. 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 createAccount.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the app account to add.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.addAccount('WangWu', (err: BusinessError) => { 
      console.log('addAccount err: ' + JSON.stringify(err));
  });

addAccount(deprecated)

addAccount(name: string, extraInfo: string, callback: AsyncCallback&lt;void&gt;): void

Adds an app account name and additional 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 createAccount.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
extraInfo string Yes Additional information (information that can be converted to the string type). It cannot contain sensitive information, such as the app account password and token.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.addAccount('LiSi', 'token101', (err: BusinessError) => { 
    console.log('addAccount err: ' + JSON.stringify(err));
  });

addAccount(deprecated)

addAccount(name: string, extraInfo?: string): Promise&lt;void&gt;

Adds an app account name and additional information. This API uses an asynchronous callback to return the result. 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 createAccount.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
extraInfo string No Additional information (information that can be converted to the string type).
The additional information cannot be sensitive information (such as the password and token) of the app account.
By default, no value is passed, which means no additional information needs to be added for the account.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.addAccount('LiSi', 'token101').then(()=> { 
    console.log('addAccount Success');
  }).catch((err: BusinessError) => {
    console.log('addAccount err: ' + JSON.stringify(err));
  });

addAccountImplicitly(deprecated)

addAccountImplicitly(owner: string, authType: string, options: {[key: string]: any}, callback: AuthenticatorCallback): void

Adds an app account implicitly based on the specified owner. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use createAccountImplicitly.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type. The authentication type is customized.
options {[key: string]: any} Yes Authentication options, which can be set as required.
callback AuthenticatorCallback Yes Authenticator callback invoked to return the result.

Example

  import { BusinessError } from '@ohos.base';
  import Want from '@ohos.app.ability.Want';
  import common from '@ohos.app.ability.common';

  let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext

  function onResultCallback(code: number, result: Record<string, Object>): void {
    console.log('resultCode: ' + code);
    console.log('result: ' + JSON.stringify(result));
  }

  function onRequestRedirectedCallback(request: Want): void {
    let wantInfo: Want = {
      deviceId: '',
      bundleName: 'com.example.accountjsdemo',
      action: 'ohos.want.action.viewData',
      entities: ['entity.system.default'],
    }
    context.startAbility(wantInfo).then(() => {
      console.log('startAbility successfully');
    }).catch((err: BusinessError) => {
      console.log('startAbility err: ' + JSON.stringify(err));
    })
  }

  appAccountManager.addAccountImplicitly('com.example.accountjsdemo', 'getSocialData', {}, {
    onResult: onResultCallback,
    onRequestRedirected: onRequestRedirectedCallback
  });

deleteAccount(deprecated)

deleteAccount(name: string, callback: AsyncCallback&lt;void&gt;): void

Deletes an app account. 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 removeAccount.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the app account to delete.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.deleteAccount('ZhaoLiu', (err: BusinessError) => { 
      console.log('deleteAccount err: ' + JSON.stringify(err));
   });

deleteAccount(deprecated)

deleteAccount(name: string): Promise&lt;void&gt;

Deletes an app account. 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 removeAccount.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the app account to delete.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Example

  import { BusinessError } from '@ohos.base';

  appAccountManager.deleteAccount('ZhaoLiu').then(() => { 
        console.log('deleteAccount Success');
   }).catch((err: BusinessError) => {
      console.log('deleteAccount err: ' + JSON.stringify(err));
  });

disableAppAccess(deprecated)

disableAppAccess(name: string, bundleName: string, callback: AsyncCallback&lt;void&gt;): void

Disables an app account from accessing an app. 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 setAppAccess.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
bundleName string Yes Bundle name of the app.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';

  appAccountManager.disableAppAccess('ZhangSan', 'com.example.accountjsdemo', (err: BusinessError) => { 
      console.log('disableAppAccess err: ' + JSON.stringify(err));
  });

disableAppAccess(deprecated)

disableAppAccess(name: string, bundleName: string): Promise&lt;void&gt;

Disables an app account from accessing an app. 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 setAppAccess.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
bundleName string Yes Bundle name of the app.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Example

  import { BusinessError } from '@ohos.base';

  appAccountManager.disableAppAccess('ZhangSan', 'com.example.accountjsdemo').then(() => { 
      console.log('disableAppAccess Success');
  }).catch((err: BusinessError) => {
      console.log('disableAppAccess err: ' + JSON.stringify(err));
  });

enableAppAccess(deprecated)

enableAppAccess(name: string, bundleName: string, callback: AsyncCallback&lt;void&gt;): void

Enables an app account to access an app. 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 setAppAccess.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
bundleName string Yes Bundle name of the app.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.enableAppAccess('ZhangSan', 'com.example.accountjsdemo', (err: BusinessError) => { 
      console.log('enableAppAccess: ' + JSON.stringify(err));
   });

enableAppAccess(deprecated)

enableAppAccess(name: string, bundleName: string): Promise&lt;void&gt;

Enables an app account to access an app. 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 setAppAccess.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
bundleName string Yes Bundle name of the app.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.enableAppAccess('ZhangSan', 'com.example.accountjsdemo').then(() => { 
       console.log('enableAppAccess Success');
  }).catch((err: BusinessError) => {
      console.log('enableAppAccess err: ' + JSON.stringify(err));
  });

checkAppAccountSyncEnable(deprecated)

checkAppAccountSyncEnable(name: string, callback: AsyncCallback&lt;boolean&gt;): void

Checks whether data synchronization is enabled for an app account. 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 checkDataSyncEnabled.

Required permissions: ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
callback AsyncCallback&lt;boolean&gt; Yes Callback invoked to return the result. The value true means data synchronization is enabled for the app account; the value false means the opposite.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.checkAppAccountSyncEnable('ZhangSan', (err: BusinessError, result: boolean) => { 
      console.log('checkAppAccountSyncEnable err: ' + JSON.stringify(err));
      console.log('checkAppAccountSyncEnable result: ' + result);
  });

checkAppAccountSyncEnable(deprecated)

checkAppAccountSyncEnable(name: string): Promise&lt;boolean&gt;

Checks whether data synchronization is enabled for an app account. 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 checkDataSyncEnabled.

Required permissions: ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.

Return value

Type Description
Promise&lt;boolean&gt; Promise used to return the result. The value true means data synchronization is enabled for the app account; the value false means the opposite.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.checkAppAccountSyncEnable('ZhangSan').then((data: boolean) => { 
      console.log('checkAppAccountSyncEnable, result: ' + data);
  }).catch((err: BusinessError) => {
      console.log('checkAppAccountSyncEnable err: ' + JSON.stringify(err));
  });

setAccountCredential(deprecated)

setAccountCredential(name: string, credentialType: string, credential: string,callback: AsyncCallback&lt;void&gt;): void

Set credentials for an app account. 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 setCredential.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
credentialType string Yes Type of the credential to set.
credential string Yes Credential value.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.setAccountCredential('ZhangSan', 'credentialType001', 'credential001', (err: BusinessError) => { 
      console.log('setAccountCredential err: ' + JSON.stringify(err));
  });

setAccountCredential(deprecated)

setAccountCredential(name: string, credentialType: string, credential: string): Promise&lt;void&gt;

Set credentials for an app account. 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 setCredential.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
credentialType string Yes Type of the credential to set.
credential string Yes Credential value.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.setAccountCredential('ZhangSan', 'credentialType001', 'credential001').then(() => { 
      console.log('setAccountCredential Success');
  }).catch((err: BusinessError) => {
      console.log('setAccountCredential err: ' + JSON.stringify(err));
  });

setAccountExtraInfo(deprecated)

setAccountExtraInfo(name: string, extraInfo: string, callback: AsyncCallback&lt;void&gt;): void

Sets additional information for an app account. 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 setCustomData.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
extraInfo string Yes Additional information (information that can be converted to the string type). It cannot contain sensitive information, such as the app account password and token.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.setAccountExtraInfo('ZhangSan', 'Tk002', (err: BusinessError) => { 
      console.log('setAccountExtraInfo err: ' + JSON.stringify(err));
  });

setAccountExtraInfo(deprecated)

setAccountExtraInfo(name: string, extraInfo: string): Promise&lt;void&gt;

Sets additional information for an app account. 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 setCustomData.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
extraInfo string Yes Additional information (information that can be converted to the string type). It cannot contain sensitive information, such as the app account password and token.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.setAccountExtraInfo('ZhangSan', 'Tk002').then(() => { 
      console.log('setAccountExtraInfo Success');
  }).catch((err: BusinessError) => {
      console.log('setAccountExtraInfo err: ' + JSON.stringify(err));
  });

setAppAccountSyncEnable(deprecated)

setAppAccountSyncEnable(name: string, isEnable: boolean, callback: AsyncCallback&lt;void&gt;): void

Sets data synchronization for an app account. 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 setDataSyncEnabled.

Required permissions: ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
isEnable boolean Yes Whether to enable data synchronization.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.setAppAccountSyncEnable('ZhangSan', true, (err: BusinessError) => { 
      console.log('setAppAccountSyncEnable err: ' + JSON.stringify(err));
  });

setAppAccountSyncEnable(deprecated)

setAppAccountSyncEnable(name: string, isEnable: boolean): Promise&lt;void&gt;

Sets data synchronization for an app account. 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 setDataSyncEnabled.

Required permissions: ohos.permission.DISTRIBUTED_DATASYNC

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
isEnable boolean Yes Whether to enable data synchronization.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager .setAppAccountSyncEnable('ZhangSan', true).then(() => { 
      console.log('setAppAccountSyncEnable Success');
  }).catch((err: BusinessError) => {
      console.log('setAppAccountSyncEnable err: ' + JSON.stringify(err));
  });

setAssociatedData(deprecated)

setAssociatedData(name: string, key: string, value: string, callback: AsyncCallback&lt;void&gt;): void

Sets data to be associated with an app account. 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 setCustomData.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
key string Yes Key of the data to set.
value string Yes Value of the data to set.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.setAssociatedData('ZhangSan', 'k001', 'v001', (err: BusinessError) => { 
      console.log('setAssociatedData err: ' + JSON.stringify(err));
  });

setAssociatedData(deprecated)

setAssociatedData(name: string, key: string, value: string): Promise&lt;void&gt;

Sets data to be associated with an app account. 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 setCustomData.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
key string Yes Key of the data to set.
value string Yes Value of the data to set.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.setAssociatedData('ZhangSan', 'k001', 'v001').then(() => { 
      console.log('setAssociatedData Success');
  }).catch((err: BusinessError) => {
      console.log('setAssociatedData err: ' + JSON.stringify(err));
  });

getAllAccessibleAccounts(deprecated)

getAllAccessibleAccounts(callback: AsyncCallback&lt;Array&lt;AppAccountInfo&gt;&gt;): void

Obtains information about all accessible app accounts. 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 getAllAccounts.

Required permissions: ohos.permission.GET_ALL_APP_ACCOUNTS

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
callback AsyncCallback&lt;Array&lt;AppAccountInfo&gt;&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is a list of accessible app accounts. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAllAccessibleAccounts((err: BusinessError, data: account_appAccount.AppAccountInfo[])=>{
  	console.debug('getAllAccessibleAccounts err: ' + JSON.stringify(err));
  	console.debug('getAllAccessibleAccounts data: ' + JSON.stringify(data));
  });

getAllAccessibleAccounts(deprecated)

getAllAccessibleAccounts(): Promise&lt;Array&lt;AppAccountInfo&gt;&gt;

Obtains information about all accessible app accounts. 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 getAllAccounts.

Required permissions: ohos.permission.GET_ALL_APP_ACCOUNTS

System capability: SystemCapability.Account.AppAccount

Return value

Type Description
Promise&lt;Array&lt;AppAccountInfo&gt;&gt; Promise used to return the accessible app accounts.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAllAccessibleAccounts().then((data: account_appAccount.AppAccountInfo[]) => { 
       console.log('getAllAccessibleAccounts: ' + data);
  }).catch((err: BusinessError) => {
      console.log('getAllAccessibleAccounts err: ' + JSON.stringify(err));
  });

getAllAccounts(deprecated)

getAllAccounts(owner: string, callback: AsyncCallback&lt;Array&lt;AppAccountInfo&gt;&gt;): void

Obtains the app accounts that can be accessed by the invoker based on the app account owner. 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 getAccountsByOwner.

Required permissions: ohos.permission.GET_ALL_APP_ACCOUNTS

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Owner of the app account. The value is the bundle name of the app.
callback AsyncCallback&lt;Array&lt;AppAccountInfo&gt;&gt; Yes Callback invoked to return information about all accessible app accounts.

Example

  import { BusinessError } from '@ohos.base';
  
  const selfBundle = 'com.example.actsgetallaaccounts';
  appAccountManager.getAllAccounts(selfBundle, (err: BusinessError, data: account_appAccount.AppAccountInfo[])=>{
  	console.debug('getAllAccounts err: ' + JSON.stringify(err));
  	console.debug('getAllAccounts data:' + JSON.stringify(data));
  });

getAllAccounts(deprecated)

getAllAccounts(owner: string): Promise&lt;Array&lt;AppAccountInfo&gt;&gt;

Obtains the app accounts that can be accessed by the invoker based on the app account owner. 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 getAccountsByOwner.

Required permissions: ohos.permission.GET_ALL_APP_ACCOUNTS (available only to system applications)

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Owner of the app account. The value is the bundle name of the app.

Return value

Type Description
Promise&lt;Array&lt;AppAccountInfo&gt;&gt; Promise use to return the app accounts that can be accessed by the invoker.

Example

  import { BusinessError } from '@ohos.base';
  
  const selfBundle = 'com.example.actsgetallaaccounts';
  appAccountManager.getAllAccounts(selfBundle).then((data: account_appAccount.AppAccountInfo[]) => { 
       console.log('getAllAccounts: ' + data);
  }).catch((err: BusinessError) => {
      console.log('getAllAccounts err: ' + JSON.stringify(err));
  });

getAccountCredential(deprecated)

getAccountCredential(name: string, credentialType: string, callback: AsyncCallback&lt;string&gt;): void

Obtains the credential of an app account. 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 getCredential.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
credentialType string Yes Type of the credential to obtain.
callback AsyncCallback&lt;string&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is the credential obtained. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAccountCredential('ZhangSan', 'credentialType001', (err: BusinessError, result: string) => { 
      console.log('getAccountCredential err: ' + JSON.stringify(err));
      console.log('getAccountCredential result: ' + result);
  });

getAccountCredential(deprecated)

getAccountCredential(name: string, credentialType: string): Promise&lt;string&gt;

Obtains the credential of an app account. 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 getCredential.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
credentialType string Yes Type of the credential to obtain.

Return value

Type Description
Promise&lt;string&gt; Promise used to return the credential obtained.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAccountCredential('ZhangSan', 'credentialType001').then((data: string) => { 
      console.log('getAccountCredential, result: ' + data);
  }).catch((err: BusinessError) => {
      console.log('getAccountCredential err: ' + JSON.stringify(err));
  });

getAccountExtraInfo(deprecated)

getAccountExtraInfo(name: string, callback: AsyncCallback&lt;string&gt;): void

Obtains additional information of an app account. Additional information refers to other information that can be converted to the string type. It cannot contain sensitive information, such as the app account password and token. 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 getCustomData.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
callback AsyncCallback&lt;string&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is the additional information obtained. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAccountExtraInfo('ZhangSan', (err: BusinessError, result: string) => { 
      console.log('getAccountExtraInfo err: ' + JSON.stringify(err));
      console.log('getAccountExtraInfo result: ' + result);
  });

getAccountExtraInfo(deprecated)

getAccountExtraInfo(name: string): Promise&lt;string&gt;

Obtains additional information of an app account. Additional information refers to other information that can be converted to the string type. It cannot contain sensitive information, such as the app account password and token. 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 getCustomData.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.

Return value

Type Description
Promise&lt;string&gt; Promise used to return the additional information obtained.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAccountExtraInfo('ZhangSan').then((data: string) => { 
      console.log('getAccountExtraInfo, result: ' + data);
  }).catch((err: BusinessError) => {
      console.log('getAccountExtraInfo err: ' + JSON.stringify(err));
  });

getAssociatedData(deprecated)

getAssociatedData(name: string, key: string, callback: AsyncCallback&lt;string&gt;): void

Obtains data associated with an app account. 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 getCustomData.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
key string Yes Key of the data to obtain.
callback AsyncCallback&lt;string&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is the data obtained. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAssociatedData('ZhangSan', 'k001', (err: BusinessError, result: string) => { 
      console.log('getAssociatedData err: ' + JSON.stringify(err));
      console.log('getAssociatedData result: ' + result);
  });

getAssociatedData(deprecated)

getAssociatedData(name: string, key: string): Promise&lt;string&gt;

Obtains data associated with an app account. 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 getCustomData.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
key string Yes Key of the data to obtain.

Return value

Type Description
Promise&lt;string&gt; Promise used to return the data obtained.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAssociatedData('ZhangSan', 'k001').then((data: string) => { 
       console.log('getAssociatedData: ' + data);
  }).catch((err: BusinessError) => {
      console.log('getAssociatedData err: ' + JSON.stringify(err));
  });

on(‘change’)(deprecated)

on(type: ‘change’, owners: Array&lt;string&gt;, callback: Callback&lt;Array&lt;AppAccountInfo&gt;&gt;): void

Subscribes to account information changes of apps.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use on(‘accountChange’).

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
type ‘change’ Yes Event type to subscribe to. The value is ‘change’. An event will be reported when the account information changes.
owners Array&lt;string&gt; Yes App bundle names of the account.
callback Callback&lt;Array&lt;AppAccountInfo&gt;&gt; Yes Callback registered to return the list of changed app accounts.

Example

  function changeOnCallback(data: account_appAccount.AppAccountInfo[]): void {
  	console.debug('receive change data:' + JSON.stringify(data));
  }
  try{
  	appAccountManager.on('change', ['com.example.actsaccounttest'], changeOnCallback);
  }
  catch(err){
  	console.error('on accountOnOffDemo err:' + JSON.stringify(err));
  }

off(‘change’)(deprecated)

off(type: ‘change’, callback?: Callback&lt;Array&lt;AppAccountInfo&gt;&gt;): void

Unsubscribes from account information changes.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use off(‘accountChange’).

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
type ‘change’ Yes Event type to unsubscribe from. The value is ‘change’, which indicates the account change event.
callback Callback&lt;Array&lt;AppAccountInfo&gt;&gt; No Callback to unregister. By default, no value is passed, which means to unregister all callbacks for the specified event.

Example

  function changeOnCallback(data: account_appAccount.AppAccountInfo[]): void {
  	console.debug('receive change data: ' + JSON.stringify(data));
  	appAccountManager.off('change', () => {
  		console.debug('off finish');
  	})
  }
  try{
  	appAccountManager.on('change', ['com.example.actsaccounttest'], changeOnCallback);
  }
  catch(err){
  	console.error('on accountOnOffDemo err: ' + JSON.stringify(err));
  }

authenticate(deprecated)

authenticate(name: string, owner: string, authType: string, options: {[key: string]: any}, callback: AuthenticatorCallback): void

Authenticates an app account with customized options. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use auth.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type.
options {[key: string]: any} Yes Options for the authentication.
callback AuthenticatorCallback Yes Callback invoked to return the result.

Example

  import { BusinessError } from '@ohos.base';
  import Want from '@ohos.app.ability.Want';
  import common from '@ohos.app.ability.common';

  let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext

  function onResultCallback(code: number, result: Record<string, Object>): void {
      console.log('resultCode: ' + code);
      console.log('result: ' + JSON.stringify(result));
  }

  function onRequestRedirectedCallback(request: Want): void {
    let wantInfo: Want = {
      deviceId: '',
      bundleName: 'com.example.accountjsdemo',
      action: 'ohos.want.action.viewData',
      entities: ['entity.system.default'],
    }
    context.startAbility(wantInfo).then(() => {
      console.log('startAbility successfully');
    }).catch((err: BusinessError) => {
      console.log('startAbility err: ' + JSON.stringify(err));
    })
  }

  appAccountManager.authenticate('LiSi', 'com.example.accountjsdemo', 'getSocialData', {}, {
    onResult: onResultCallback,
    onRequestRedirected: onRequestRedirectedCallback
  });

getOAuthToken(deprecated)

getOAuthToken(name: string, owner: string, authType: string, callback: AsyncCallback&lt;string&gt;): void

Obtains the authorization token of the specified authentication type for an app account. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getAuthToken.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type.
callback AsyncCallback&lt;string&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is the authorization token value obtained. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getOAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData',
    (err: BusinessError, data: string) => {
      console.log('getOAuthToken err: ' + JSON.stringify(err));
      console.log('getOAuthToken token: ' + data);
    });

getOAuthToken(deprecated)

getOAuthToken(name: string, owner: string, authType: string): Promise&lt;string&gt;

Obtains the authorization token of the specified authentication type for an app account. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getAuthToken.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type.

Return value

Type Description
Promise&lt;string&gt; Promise used to return the result.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getOAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData').then((data: string) => {
       console.log('getOAuthToken token: ' + data);
  }).catch((err: BusinessError) => {
      console.log('getOAuthToken err: ' + JSON.stringify(err));
  });

setOAuthToken(deprecated)

setOAuthToken(name: string, authType: string, token: string, callback: AsyncCallback&lt;void&gt;): void

Sets an authorization token of the specific authentication type for an app account. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use setAuthToken.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
token string Yes Token to set.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.setOAuthToken('LiSi', 'getSocialData', 'xxxx', (err: BusinessError) => {
      console.log('setOAuthToken err: ' + JSON.stringify(err));
  });

setOAuthToken(deprecated)

setOAuthToken(name: string, authType: string, token: string): Promise&lt;void&gt;

Sets an authorization token of the specific authentication type for an app account. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use setAuthToken.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
token string Yes Authorization token to set.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.setOAuthToken('LiSi', 'getSocialData', 'xxxx').then(() => {
      console.log('setOAuthToken successfully');
  }).catch((err: BusinessError) => {
      console.log('setOAuthToken err: ' + JSON.stringify(err));
  });

deleteOAuthToken(deprecated)

deleteOAuthToken(name: string, owner: string, authType: string, token: string, callback: AsyncCallback&lt;void&gt;): void

Deletes the authorization token of the specified authentication type for an app account. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use deleteAuthToken.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type.
token string Yes Authorization token to delete.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.deleteOAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData', 'xxxxx',
    (err: BusinessError) => {
      console.log('deleteOAuthToken err: ' + JSON.stringify(err));
    });

deleteOAuthToken(deprecated)

deleteOAuthToken(name: string, owner: string, authType: string, token: string): Promise&lt;void&gt;

Deletes the authorization token of the specified authentication type for an app account. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use deleteAuthToken.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
authType string Yes Authentication type.
token string Yes Authorization token to delete.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.deleteOAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData', 'xxxxx').then(() => {
       console.log('deleteOAuthToken successfully');
  }).catch((err: BusinessError) => {
      console.log('deleteOAuthToken err: ' + JSON.stringify(err));
  });

setOAuthTokenVisibility(deprecated)

setOAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean, callback: AsyncCallback&lt;void&gt;): void

Sets the visibility of an authorization token to an app. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use setAuthTokenVisibility.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
bundleName string Yes Bundle name of the app.
isVisible boolean Yes Whether the authorization token is visible to the app. The value true means the authorization token is visible to the app; the value false means the opposite.
callback AsyncCallback&lt;void&gt; Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.setOAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo', true,
    (err: BusinessError) => {
      console.log('setOAuthTokenVisibility err: ' + JSON.stringify(err));
    });

setOAuthTokenVisibility(deprecated)

setOAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean): Promise&lt;void&gt;

Sets the visibility of an authorization token to an app. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use setAuthTokenVisibility.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
bundleName string Yes Bundle name of the app.
isVisible boolean Yes Whether the authorization token is visible to the app. The value true means the authorization token is visible to the app; the value false means the opposite.

Return value

Type Description
Promise&lt;void&gt; Promise that returns no value.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.setOAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo', true).then(() => {
      console.log('setOAuthTokenVisibility successfully');
  }).catch((err: BusinessError) => {
      console.log('setOAuthTokenVisibility err: ' + JSON.stringify(err));
  });

checkOAuthTokenVisibility(deprecated)

checkOAuthTokenVisibility(name: string, authType: string, bundleName: string, callback: AsyncCallback&lt;boolean&gt;): void

Checks the visibility of an authorization token of the specified authentication type to an app. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use checkAuthTokenVisibility.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
bundleName string Yes Bundle name of the app.
callback AsyncCallback&lt;boolean&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data can be true (the authorization token is visible to the app) or false (the authorization token is not visible to the app). If the operation fails, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.checkOAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo',
    (err: BusinessError, data: boolean) => {
      console.log('checkOAuthTokenVisibility err: ' + JSON.stringify(err));
      console.log('checkOAuthTokenVisibility isVisible: ' + data);
    });

checkOAuthTokenVisibility(deprecated)

checkOAuthTokenVisibility(name: string, authType: string, bundleName: string): Promise&lt;boolean&gt;

Checks the visibility of an authorization token of the specified authentication type to an app. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use checkAuthTokenVisibility.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
bundleName string Yes Bundle name of the app.

Return value

Type Description
Promise&lt;boolean&gt; Promise used to return the result. The value true means the authorization token is visible to the app; the value false means the opposite.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.checkOAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo').then((
    data: boolean) => {
    console.log('checkOAuthTokenVisibility isVisible: ' + data);
  }).catch((err: BusinessError) => {
    console.log('checkOAuthTokenVisibility err: ' + JSON.stringify(err));
  });

getAllOAuthTokens(deprecated)

getAllOAuthTokens(name: string, owner: string, callback: AsyncCallback&lt;Array&lt;OAuthTokenInfo&gt;&gt;): void

Obtains all tokens visible to the invoker for an app account. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getAllAuthTokens.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.
callback AsyncCallback&lt;Array&lt;OAuthTokenInfo&gt;&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is a list of all tokens visible to the invoker. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAllOAuthTokens('LiSi', 'com.example.accountjsdemo',
    (err: BusinessError, data: account_appAccount.OAuthTokenInfo[]) => {
      console.log('getAllOAuthTokens err: ' + JSON.stringify(err));
      console.log('getAllOAuthTokens data: ' + JSON.stringify(data));
    });

getAllOAuthTokens(deprecated)

getAllOAuthTokens(name: string, owner: string): Promise&lt;Array&lt;OAuthTokenInfo&gt;&gt;

Obtains all tokens visible to the invoker for an app account. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getAllAuthTokens.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
owner string Yes Owner of the app account. The value is the bundle name of the app.

Return value

Type Description
Promise&lt;Array&lt; OAuthTokenInfo&gt;&gt; Promise used to return the tokens obtained.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAllOAuthTokens('LiSi', 'com.example.accountjsdemo').then((
    data: account_appAccount.OAuthTokenInfo[]) => {
    console.log('getAllOAuthTokens data: ' + JSON.stringify(data));
  }).catch((err: BusinessError) => {
    console.log('getAllOAuthTokens err: ' + JSON.stringify(err));
  });

getOAuthList(deprecated)

getOAuthList(name: string, authType: string, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void

Obtains the authorization list of the specified authentication type for an app account. The authorization list contains all authorized bundles. The token authorization list is set by setOAuthTokenVisibility(#setoauthtokenvisibilitydeprecated). This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getAuthList.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
callback AsyncCallback&lt;Array&lt;string&gt;&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is a list of authorized bundles obtained. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getOAuthList('LiSi', 'getSocialData', (err: BusinessError, data: string[]) => {
    console.log('getOAuthList err: ' + JSON.stringify(err));
    console.log('getOAuthList data: ' + JSON.stringify(data));
  });

getOAuthList(deprecated)

getOAuthList(name: string, authType: string): Promise&lt;Array&lt;string&gt;&gt;

Obtains the authorization list of the specified authentication type for an app account. The authorization list contains all authorized bundles. The token authorization list is set by setOAuthTokenVisibility(#setoauthtokenvisibilitydeprecated). This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getAuthList.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.

Return value

Type Description
Promise&lt;Array&lt;string&gt;&gt; Promise used to return a list of authorized bundles.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getOAuthList('LiSi', 'getSocialData').then((data: string[]) => {
       console.log('getOAuthList data: ' + JSON.stringify(data));
  }).catch((err: BusinessError) => {
      console.log('getOAuthList err: ' + JSON.stringify(err));
  });

getAuthenticatorCallback(deprecated)

getAuthenticatorCallback(sessionId: string, callback: AsyncCallback&lt;AuthenticatorCallback&gt;): void

Obtains the authenticator callback for an authentication session. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getAuthCallback.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
sessionId string Yes ID of the authentication session.
callback AsyncCallback&lt;AuthenticatorCallback&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is the authenticator callback obtained. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  import UIAbility from '@ohos.app.ability.UIAbility';
  import Want from '@ohos.app.ability.Want';
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';

  export default class EntryAbility extends UIAbility {
    onCreate(want: Want, param: AbilityConstant.LaunchParam) { // Ability lifecycle function.
      let sessionId: string = want.parameters![account_appAccount.Constants.KEY_SESSION_ID] as string;
      appAccountManager.getAuthenticatorCallback(sessionId,
          (err: BusinessError, callback: account_appAccount.AuthenticatorCallback) => {
          if (err.code != account_appAccount.ResultCode.SUCCESS) {
              console.log('getAuthenticatorCallback err: ' + JSON.stringify(err));
              return;
          }
          callback.onResult(account_appAccount.ResultCode.SUCCESS, {
            name: 'LiSi',
            owner: 'com.example.accountjsdemo',
            authType: 'getSocialData',
            token: 'xxxxxx'}
          );
        });
    }
  }

getAuthenticatorCallback(deprecated)

getAuthenticatorCallback(sessionId: string): Promise&lt;AuthenticatorCallback&gt;

Obtains the authenticator callback for an authentication session. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use getAuthCallback.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
sessionId string Yes ID of the authentication session.

Return value

Type Description
Promise&lt;AuthenticatorCallback&gt; Promise used to return the authenticator callback obtained.

Example

  import { BusinessError } from '@ohos.base';
  import UIAbility from '@ohos.app.ability.UIAbility';
  import Want from '@ohos.app.ability.Want';
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';

  export default class EntryAbility extends UIAbility {
    onCreate(want: Want, param: AbilityConstant.LaunchParam) { // Ability lifecycle function.
      let sessionId: string = want.parameters![account_appAccount.Constants.KEY_SESSION_ID] as string;
      appAccountManager.getAuthenticatorCallback(sessionId).then((
        callback: account_appAccount.AuthenticatorCallback) => {
        callback.onResult(account_appAccount.ResultCode.SUCCESS, {
          name: 'LiSi',
          owner: 'com.example.accountjsdemo',
          authType: 'getSocialData',
          token: 'xxxxxx'}
        );
      }).catch((err: BusinessError) => {
        console.log('getAuthenticatorCallback err: ' + JSON.stringify(err));
      });
    }
  }

getAuthenticatorInfo(deprecated)

getAuthenticatorInfo(owner: string, callback: AsyncCallback&lt;AuthenticatorInfo&gt;): void

Obtains the authenticator information of an app. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use queryAuthenticatorInfo.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Owner of the app account. The value is the bundle name of the app.
callback AsyncCallback&lt;AuthenticatorInfo&gt; Yes Callback invoked to return the result. If the operation is successful, err is null and data is the authenticator information obtained. Otherwise, err is an error object.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAuthenticatorInfo('com.example.accountjsdemo',
    (err: BusinessError, data: account_appAccount.AuthenticatorInfo) => {
      console.log('getAuthenticatorInfo err: ' + JSON.stringify(err));
      console.log('getAuthenticatorInfo data: ' + JSON.stringify(data));
    });

getAuthenticatorInfo(deprecated)

getAuthenticatorInfo(owner: string): Promise&lt;AuthenticatorInfo&gt;

Obtains the authenticator information of an app. This API uses a promise to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use queryAuthenticatorInfo.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
owner string Yes Owner of the app account. The value is the bundle name of the app.

Return value

Type Description
Promise&lt;AuthenticatorInfo&gt; Promise used to return the authenticator information obtained.

Example

  import { BusinessError } from '@ohos.base';
  
  appAccountManager.getAuthenticatorInfo('com.example.accountjsdemo').then((
    data: account_appAccount.AuthenticatorInfo) => { 
    console.log('getAuthenticatorInfo: ' + JSON.stringify(data));
  }).catch((err: BusinessError) => {
    console.log('getAuthenticatorInfo err: ' + JSON.stringify(err));
  });

AppAccountInfo

Defines app account information.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
owner string Yes Owner of the app account. The value is the bundle name of the app.
name string Yes Name of the target app account.

AuthTokenInfo9+

Defines authorization token information.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
authType9+ string Yes Authentication type.
token9+ string Yes Value of the authorization token.
account9+ AppAccountInfo No Information about the account to which the token belongs. By default, no value is passed.

OAuthTokenInfo(deprecated)

Defines authorization token information.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use AuthTokenInfo.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
authType string Yes Authentication type.
token string Yes Value of the authorization token.
account9+ AppAccountInfo No Information about the account to which the token belongs. By default, no value is passed.

AuthenticatorInfo8+

Defines OAuth authenticator information.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
owner string Yes Owner of the authenticator. The value is the bundle name of the app.
iconId number Yes ID of the authenticator icon.
labelId number Yes ID of the authenticator label.

AuthResult9+

Defines the authentication result.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
account AppAccountInfo No Information about the account to which the token belongs. By default, no value is passed.
tokenInfo AuthTokenInfo No Token information. By default, no value is passed.

CreateAccountOptions9+

Defines the options for creating an app account.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
customData {[key: string]: string} No Custom data. By default, no value is passed.

CreateAccountImplicitlyOptions9+

Defines the options for implicitly creating an app account.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
requiredLabels Array&lt;string&gt; No Required labels. By default, no value is passed.
authType string No Authentication type. By default, no value is passed.
parameters {[key: string]: Object} No Custom parameter object. By default, no value is passed.

SelectAccountsOptions9+

Defines the options for selecting accounts.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
allowedAccounts Array&lt;AppAccountInfo&gt; No Array of allowed accounts. By default, no value is passed.
allowedOwners Array&lt;string&gt; No Array of the owners of the allowed accounts. By default, no value is passed.
requiredLabels Array&lt;string&gt; No Labels of the authenticator. By default, no value is passed.

VerifyCredentialOptions9+

Represents the options for verifying the user credential.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
credentialType string No Credential type. By default, no value is passed.
credential string No Credential value. By default, no value is passed.
parameters {[key: string]: Object} No Custom parameter object. By default, no value is passed.

SetPropertiesOptions9+

Represents the options for setting authenticator properties.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
properties {[key: string]: Object} No Property object. By default, no value is passed.
parameters {[key: string]: Object} No Custom parameter object. By default, no value is passed.

Constants8+

Enumerates the constants.

System capability: SystemCapability.Account.AppAccount

Name Value Description
ACTION_ADD_ACCOUNT_IMPLICITLY(deprecated) ‘addAccountImplicitly’ Operation of adding an account implicitly.
ACTION_AUTHENTICATE(deprecated) ‘authenticate’ Authentication operation.
ACTION_CREATE_ACCOUNT_IMPLICITLY9+ ‘createAccountImplicitly’ Operation of creating an account implicitly.
ACTION_AUTH9+ ‘auth’ Authentication operation.
ACTION_VERIFY_CREDENTIAL9+ ‘verifyCredential’ Operation of verifying credentials.
ACTION_SET_AUTHENTICATOR_PROPERTIES9+ ‘setAuthenticatorProperties’ Operation of setting authenticator properties.
KEY_NAME ‘name’ Name of the app account.
KEY_OWNER ‘owner’ Owner of the app account.
KEY_TOKEN ‘token’ Token.
KEY_ACTION ‘action’ Operation.
KEY_AUTH_TYPE ‘authType’ Authentication type.
KEY_SESSION_ID ‘sessionId’ Session ID.
KEY_CALLER_PID ‘callerPid’ PID of the caller.
KEY_CALLER_UID ‘callerUid’ UID of the caller.
KEY_CALLER_BUNDLE_NAME ‘callerBundleName’ Bundle name of the caller.
KEY_REQUIRED_LABELS9+ ‘requiredLabels’ Required labels.
KEY_BOOLEAN_RESULT9+ ‘booleanResult’ Return value of the Boolean type.

ResultCode(deprecated)

Enumerates the result codes.

NOTE

This enum is supported since API version 8 and deprecated since API version 9. Error codes are used from API version 9. For details, see Account Management Error Codes.

System capability: SystemCapability.Account.AppAccount

Name Value Description
SUCCESS 0 The operation is successful.
ERROR_ACCOUNT_NOT_EXIST 10001 The app account does not exist.
ERROR_APP_ACCOUNT_SERVICE_EXCEPTION 10002 The AppAccountManager service is abnormal.
ERROR_INVALID_PASSWORD 10003 The password is invalid.
ERROR_INVALID_REQUEST 10004 The request is invalid.
ERROR_INVALID_RESPONSE 10005 The response is invalid.
ERROR_NETWORK_EXCEPTION 10006 The network is abnormal.
ERROR_OAUTH_AUTHENTICATOR_NOT_EXIST 10007 The authenticator does not exist.
ERROR_OAUTH_CANCELED 10008 The authentication is canceled.
ERROR_OAUTH_LIST_TOO_LARGE 10009 The size of the OAuth list exceeds the limit.
ERROR_OAUTH_SERVICE_BUSY 10010 The OAuth service is busy.
ERROR_OAUTH_SERVICE_EXCEPTION 10011 The OAuth service is abnormal.
ERROR_OAUTH_SESSION_NOT_EXIST 10012 The session to be authenticated does not exist.
ERROR_OAUTH_TIMEOUT 10013 The authentication timed out.
ERROR_OAUTH_TOKEN_NOT_EXIST 10014 The authorization token does not exist.
ERROR_OAUTH_TOKEN_TOO_MANY 10015 The number of OAuth tokens reaches the limit.
ERROR_OAUTH_UNSUPPORT_ACTION 10016 The authentication operation is not supported.
ERROR_OAUTH_UNSUPPORT_AUTH_TYPE 10017 The authentication type is not supported.
ERROR_PERMISSION_DENIED 10018 The required permission is missing.

AuthCallback9+

Implements authenticator callbacks.

onResult9+

onResult: (code: number, result?: AuthResult) =&gt; void

Called to return the result of an authentication request.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
code number Yes Authentication result code.
result AuthResult No Authentication result. By default, no value is passed, which means the authentication result is not received.

Example

  import { BusinessError } from '@ohos.base';
  
  let appAccountManager = account_appAccount.createAppAccountManager();
  let sessionId = '1234';
  appAccountManager.getAuthCallback(sessionId).then((callback: account_appAccount.AuthCallback) => {
      let result: account_appAccount.AuthResult = {
          account: {
            name: 'Lisi',
            owner: 'com.example.accountjsdemo',
          },
          tokenInfo: {
            token: 'xxxxxx',
            authType: 'getSocialData'
          }
      };
      callback.onResult(account_appAccount.ResultCode.SUCCESS, result);
  }).catch((err: BusinessError) => {
      console.log('getAuthCallback err: ' + JSON.stringify(err));
  });

onRequestRedirected9+

onRequestRedirected: (request: Want) =&gt; void

Called to redirect a request.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
request Want Yes Request to be redirected.

Example

  class MyAuthenticator extends account_appAccount.Authenticator {
      createAccountImplicitly(
        options: account_appAccount.CreateAccountImplicitlyOptions, callback: account_appAccount.AuthCallback) {
          let want: Want = {
            bundleName: 'com.example.accountjsdemo',
            abilityName: 'com.example.accountjsdemo.LoginAbility',
          };
          callback.onRequestRedirected(want);
      }

      auth(name: string, authType: string,
        options: Record<string, Object>, callback: account_appAccount.AuthCallback) {
          let result: account_appAccount.AuthResult = {
            account: {
              name: 'Lisi',
              owner: 'com.example.accountjsdemo',
            },
            tokenInfo: {
              token: 'xxxxxx',
              authType: 'getSocialData'
            }
          };
          callback.onResult(account_appAccount.ResultCode.SUCCESS, result);
      }
  }

onRequestContinued9+

onRequestContinued?: () =&gt; void

Called to continue to process the request.

System capability: SystemCapability.Account.AppAccount

Example

  import { BusinessError } from '@ohos.base';
  
  let appAccountManager = account_appAccount.createAppAccountManager();
  let sessionId = '1234';
  appAccountManager.getAuthCallback(sessionId).then((callback: account_appAccount.AuthCallback) => {
    if (callback.onRequestContinued != undefined) {
      callback.onRequestContinued();
    }
  }).catch((err: BusinessError) => {
    console.log('getAuthCallback err: ' + JSON.stringify(err));
  });

AuthenticatorCallback(deprecated)

Provides OAuth authenticator callbacks.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use AuthCallback.

onResult8+

onResult: (code: number, result: {[key: string]: any}) =&gt; void

Called to return the result of an authentication request.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
code number Yes Authentication result code.
result {[key: string]: any} Yes Authentication result.

Example

  import { BusinessError } from '@ohos.base';
  
  let appAccountManager = account_appAccount.createAppAccountManager();
  let sessionId = '1234';
  appAccountManager.getAuthenticatorCallback(sessionId).then((callback: account_appAccount.AuthenticatorCallback) => {
      callback.onResult(account_appAccount.ResultCode.SUCCESS, {
        name: 'LiSi',
        owner: 'com.example.accountjsdemo',
        authType: 'getSocialData',
        token: 'xxxxxx'}
      );
  }).catch((err: BusinessError) => {
      console.log('getAuthenticatorCallback err: ' + JSON.stringify(err));
  });

onRequestRedirected8+

onRequestRedirected: (request: Want) =&gt; void

Called to redirect a request.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
request Want Yes Request to be redirected.

Example

  class MyAuthenticator extends account_appAccount.Authenticator {
      addAccountImplicitly(authType: string, callerBundleName: string,
        options: Record<string, Object>, callback: account_appAccount.AuthenticatorCallback) {
          let want: Want = {
            bundleName: 'com.example.accountjsdemo',
            abilityName: 'com.example.accountjsdemo.LoginAbility',
          };
          callback.onRequestRedirected(want);
      }

      authenticate(name: string, authType: string, callerBundleName: string,
        options: Record<string, Object>, callback: account_appAccount.AuthenticatorCallback) {
          callback.onResult(account_appAccount.ResultCode.SUCCESS, {
            name: name,
            authType: authType,
            token: 'xxxxxx'}
          );
      }
  }

Authenticator8+

Provides APIs to operate the authenticator.

createAccountImplicitly9+

createAccountImplicitly(options: CreateAccountImplicitlyOptions, callback: AuthCallback): void

Creates an app account implicitly based on the specified account owner. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
options CreateAccountImplicitlyOptions Yes Options for implicitly creating an account.
callback AuthCallback Yes Authenticator callback invoked to return the result.

addAccountImplicitly(deprecated)

addAccountImplicitly(authType: string, callerBundleName: string, options: {[key: string]: any}, callback: AuthenticatorCallback): void

Adds an app account implicitly based on the specified authentication type and options. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use createAccountImplicitly.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
authType string Yes Authentication type.
callerBundleName string Yes Bundle name of the authentication requester.
options {[key: string]: any} Yes Options for the authentication.
callback AuthenticatorCallback Yes Authenticator callback invoked to return the authentication result.

auth9+

auth(name: string, authType: string, options: {[key:string]: Object}, callback: AuthCallback): void

Authenticates an app account to obtain the authorization token. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
callerBundleName string Yes Authentication type.
options {[key: string]: Object} Yes Options for the authentication.
callback AuthCallback Yes Callback invoked to return the result.

authenticate(deprecated)

authenticate(name: string, authType: string, callerBundleName: string, options: {[key: string]: any}, callback: AuthenticatorCallback): void

Authenticates an app account to obtain the authorization token. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use auth.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
authType string Yes Authentication type.
callerBundleName string Yes Bundle name of the authentication requester.
options {[key: string]: any} Yes Options for the authentication.
callback AuthenticatorCallback Yes Authenticator callback invoked to return the authentication result.

verifyCredential9+

verifyCredential(name: string, options: VerifyCredentialOptions, callback: AuthCallback): void;

Verifies the credential of an app account. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
options VerifyCredentialOptions Yes Options for credential verification.
callback AuthCallback Yes Authenticator callback invoked to return the verification result.

setProperties9+

setProperties(options: SetPropertiesOptions, callback: AuthCallback): void;

Sets the authenticator properties. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
options SetPropertiesOptions Yes Authenticator properties to set.
callback AuthCallback Yes Authenticator callback invoked to return the result.

checkAccountLabels9+

checkAccountLabels(name: string, labels: Array&lt;string&gt;, callback: AuthCallback): void;

Checks the account labels. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
labels Array&lt;string&gt; Yes Labels to check.
callback AuthCallback Yes Authenticator callback invoked to return the check result.

checkAccountRemovable9+

checkAccountRemovable(name: string, callback: AuthCallback): void;

Checks whether an app account can be deleted. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Account.AppAccount

Parameters

Name Type Mandatory Description
name string Yes Name of the target app account.
callback AuthCallback Yes Authenticator callback invoked to return the result.

getRemoteObject9+

getRemoteObject(): rpc.RemoteObject;

Obtains the remote object of an authenticator. This API cannot be overloaded.

System capability: SystemCapability.Account.AppAccount

Example

  import rpc from '@ohos.rpc';
  
  class MyAuthenticator extends account_appAccount.Authenticator {
    addAccountImplicitly(authType: string, callerBundleName: string,
      options: Record<string, Object>, callback: account_appAccount.AuthenticatorCallback) {
        let want: Want = {
          bundleName: 'com.example.accountjsdemo',
          abilityName: 'com.example.accountjsdemo.LoginAbility',
        };
        callback.onRequestRedirected(want);
    }

    authenticate(name: string, authType: string, callerBundleName: string,
      options: Record<string, Object>, callback: account_appAccount.AuthenticatorCallback) {
        callback.onResult(account_appAccount.ResultCode.SUCCESS, {
          name: name,
          authType: authType,
          token: 'xxxxxx'}
        );
    }

    verifyCredential(name: string,
      options: account_appAccount.VerifyCredentialOptions, callback: account_appAccount.AuthCallback) {
        let want: Want = {
          bundleName: 'com.example.accountjsdemo',
          abilityName: 'com.example.accountjsdemo.VerifyAbility',
          parameters: {
            name: name
          }
        };
        callback.onRequestRedirected(want);
    }

    setProperties(options: account_appAccount.SetPropertiesOptions, callback: account_appAccount.AuthCallback) {
      let want: Want = {
          bundleName: 'com.example.accountjsdemo',
          abilityName: 'com.example.accountjsdemo.SetPropertiesAbility',
          parameters: {
            options: options
          }
        };
        callback.onRequestRedirected(want);
    }

    checkAccountLabels(name: string, labels: string[], callback: account_appAccount.AuthCallback) {
      callback.onResult(account_appAccount.ResultCode.SUCCESS);
    }
  
    checkAccountRemovable(name: string, callback: account_appAccount.AuthCallback) {
      callback.onResult(account_appAccount.ResultCode.SUCCESS);
    }
  }

  export default {
    onConnect(want: Want): rpc.RemoteObject { // serviceAbility lifecycle function.
      let authenticator = new MyAuthenticator();
      return authenticator.getRemoteObject();
    }
  }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞