@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

try {
  appAccountManager.createAccount("WangWu", (err) => { 
      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

let options = {
  customData: {
    "age": "10"
  }
}
try {
  appAccountManager.createAccount("LiSi", options, (err) => {
    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). This parameter can be left empty.

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.
12400003 The number of custom data reaches the upper limit.

Example

let options = {
  customData: {
    "age": "10"
  }
}
try {
  appAccountManager.createAccount("LiSi", options).then(() => {
    console.log("createAccount successfully");
  }).catch((err) => {
    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

function onResultCallback(code, result) {
  console.log("resultCode: "  + code);
  console.log("result: "  + JSON.stringify(result));
}

function onRequestRedirectedCallback(request) {
  let wantInfo = {
    deviceId: '',
    bundleName: 'com.example.accountjsdemo',
    action: 'ohos.want.action.viewData',
    entities: ['entity.system.default'],
  }
  this.context.startAbility(wantInfo).then(() => {
    console.log("startAbility successfully");
  }).catch((err) => {
    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 name 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

function onResultCallback(code, result) {
  console.log("resultCode: "  + code);
  console.log("result: "  + JSON.stringify(result));
}

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

let options = {
  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

try {
  appAccountManager.removeAccount("ZhaoLiu", (err) => {
    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

try {
  appAccountManager.removeAccount("Lisi").then(() => {
    console.log("removeAccount successfully");
  }).catch((err) => {
    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

try {
  appAccountManager.setAppAccess("ZhangSan", "com.example.accountjsdemo", true, (err) => {
    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

try {
  appAccountManager.setAppAccess("ZhangSan", "com.example.accountjsdemo", true).then(() => {
    console.log("setAppAccess successfully");
  }).catch((err) => {
    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.
12400001 Application not found.

Example

try {
  appAccountManager.checkAppAccess("ZhangSan", "com.example.accountjsdemo", (err, isAccessible) => {
    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.
12400001 Application not found.

Example

try {
  appAccountManager.checkAppAccess("ZhangSan", "com.example.accountjsdemo").then((isAccessible) => {
    console.log("checkAppAccess successfully, isAccessible: " + isAccessible);
  }).catch((err) => {
    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

try {
    appAccountManager.setDataSyncEnabled("ZhangSan", true, (err) => { 
        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

try {
    appAccountManager .setDataSyncEnabled("ZhangSan", true).then(() => { 
        console.log('setDataSyncEnabled Success');
    }).catch((err) => {
        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

try {
  appAccountManager.checkDataSyncEnabled("ZhangSan", (err, isEnabled) => {
    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

try {
  appAccountManager.checkDataSyncEnabled("ZhangSan").then((isEnabled) => {
      console.log("checkDataSyncEnabled successfully, isEnabled: " + isEnabled);
  }).catch((err) => {
    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 or credentialType or credential.
12300003 Account not found.

Example

try {
  appAccountManager.setCredential("ZhangSan", "PIN_SIX", "xxxxxx", (err) => {
    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 or credentialType or credential.
12300003 Account not found.

Example

try {
  appAccountManager.setCredential("ZhangSan", "PIN_SIX", "xxxxxx").then(() => {
    console.log("setCredential successfully");
  }).catch((err) => {
    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

try {
    appAccountManager.getCredential("ZhangSan", "PIN_SIX", (err, result) => { 
      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

try {
  appAccountManager.getCredential("ZhangSan", "PIN_SIX").then((credential) => {
      console.log("getCredential successfully, credential: " + credential);
  }).catch((err) => {
      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

try {
  appAccountManager.setCustomData("ZhangSan", "age", "12", (err) => {
    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

try {
  appAccountManager.setCustomData("ZhangSan", "age", "12").then(() => {
    console.log("setCustomData successfully");
  }).catch((err) => {
    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

try {
  appAccountManager.getCustomData("ZhangSan", "age", (err, data) => {
    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

try {
  appAccountManager.getCustomData("ZhangSan", "age").then((data) => {
    console.log("getCustomData successfully, data: " + data);
  }).catch((err) => {
    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

try {
  appAccountManager.getAllAccounts((err, data) => {
    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

try {
  appAccountManager.getAllAccounts().then((data) => {
    console.debug("getAllAccounts successfully");
  }).catch((err) => {
    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

try {
  appAccountManager.getAccountsByOwner("com.example.accountjsdemo2", (err, data) => {
    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

try {
  appAccountManager.getAccountsByOwner("com.example.accountjsdemo2").then((data) => {
    console.debug("getAccountsByOwner successfully, data:" + JSON.stringify(data));
  }).catch((err) => {
    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 invoked to return a list of app accounts whose information is changed.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid type or owners.
12300011 Callback has been registered.
12400001 Application not found.

Example

function changeOnCallback(data){
	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.

Error codes

ID Error Message
12300001 System service exception.
12300002 Invalid type.
12300012 Callback has not been registered.

Example

function changeOnCallback(data){
	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



function onResultCallback(code, authResult) {
  console.log("resultCode: "  + code);
  console.log("authResult: "  + JSON.stringify(authResult));
}

function onRequestRedirectedCallback(request) {
  let wantInfo = {
    deviceId: '',
    bundleName: 'com.example.accountjsdemo',
    action: 'ohos.want.action.viewData',
    entities: ['entity.system.default'],
  }
  this.context.startAbility(wantInfo).then(() => {
    console.log("startAbility successfully");
  }).catch((err) => {
    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 exist.
12300010 Account service busy.
12300113 Authenticator service not found.
12300114 Authenticator service exception.

Example



function onResultCallback(code, authResult) {
  console.log("resultCode: "  + code);
  console.log("authResult: "  + JSON.stringify(authResult));
}

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

let options = {
  "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<string>): 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<string> 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

try {
  appAccountManager.getAuthToken("LiSi", "com.example.accountjsdemo", "getSocialData", (err, token) => {
    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<string>

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<string> 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

try {
  appAccountManager.getAuthToken("LiSi", "com.example.accountjsdemo", "getSocialData").then((token) => {
    console.log("getAuthToken successfully, token: " + token);
  }).catch((err) => {
    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<void>): 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<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, authType, or token.
12300003 Account not found.
12400004 The number of token reaches the upper limit.

Example

try {
  appAccountManager.setAuthToken("LiSi", "getSocialData", "xxxx", (err) => {
    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<void>

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<void> 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 token reaches the upper limit.

Example

try {
  appAccountManager.setAuthToken("LiSi", "getSocialData", "xxxx").then(() => {
      console.log("setAuthToken successfully");
  }).catch((err) => {
      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<void>): 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<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, owner, authType, or token.
12300003 Account not found.
12300107 AuthType not found.

Example

try {
    appAccountManager.deleteAuthToken("LiSi", "com.example.accountjsdemo", "getSocialData", "xxxxx", (err) => {
      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<void>

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<void> 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

try {
  appAccountManager.deleteAuthToken("LiSi", "com.example.accountjsdemo", "getSocialData", "xxxxx").then(() => {
    console.log("deleteAuthToken successfully");
  }).catch((err) => {
    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<void>): 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<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, 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

try {
    appAccountManager.setAuthTokenVisibility("LiSi", "getSocialData", "com.example.accountjsdemo", true, (err) => {
      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<void>

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<void> 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

try {
  appAccountManager.setAuthTokenVisibility("LiSi", "getSocialData", "com.example.accountjsdemo", true).then(() => {
    console.log("setAuthTokenVisibility successfully");
  }).catch((err) => {
    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<boolean>): 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<boolean> 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.
12400001 Application not found.

Example

try {
  appAccountManager.checkAuthTokenVisibility("LiSi", "getSocialData", "com.example.accountjsdemo", (err, isVisible) => {
    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<boolean>

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<boolean> 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.
12400001 Application not found.

Example

try {
  appAccountManager.checkAuthTokenVisibility("LiSi", "getSocialData", "com.example.accountjsdemo").then((isVisible) => {
    console.log("checkAuthTokenVisibility successfully, isVisible: " + isVisible);
  }).catch((err) => {
    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<Array<AuthTokenInfo>>): 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<Array<AuthTokenInfo>> 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

try {
  appAccountManager.getAllAuthTokens("LiSi", "com.example.accountjsdemo", (err, tokenArr) => {
    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<Array<AuthTokenInfo>>

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<Array<AuthTokenInfo>> 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

try {
  appAccountManager.getAllAuthTokens("LiSi", "com.example.accountjsdemo").then((tokenArr) => {
      console.log('getAllAuthTokens successfully, tokenArr: ' + JSON.stringify(tokenArr));
  }).catch((err) => {
      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<Array<string>>): 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<Array<string>> 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

try {
  appAccountManager.getAuthList("com.example.accountjsdemo", "getSocialData", (err, authList) => {
    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<Array<string>>

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<Array<string>> 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

try {
  appAccountManager.getAuthList("com.example.accountjsdemo", "getSocialData").then((authList) => {
      console.log("getAuthList successfully, authList: " + authList);
  }).catch((err) => {
      console.log("getAuthList failed, error: "  + JSON.stringify(err));
  });
} catch (err) {
  console.log("getAuthList exception: "  + JSON.stringify(err));
}

getAuthCallback9+

getAuthCallback(sessionId: string, callback: AsyncCallback<AuthCallback>): void

Obtains the authenticator callback for the 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<AuthCallback> 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 UIAbility from '@ohos.app.ability.UIAbility';

export default class EntryAbility extends UIAbility {
  onCreate(want, param) {
    var sessionId = want.parameters[account_appAccount.Constants.KEY_SESSION_ID];
    try {
      appAccountManager.getAuthCallback(sessionId, (err, callback) => {
        if (err != null) {
            console.log("getAuthCallback err: "  + JSON.stringify(err));
            return;
        }
        var result = {
          accountInfo: {
            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<AuthCallback>

Obtains the authenticator callback for the 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<AuthCallback> 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 UIAbility from '@ohos.app.ability.UIAbility';

export default class EntryAbility extends UIAbility {
  onCreate(want, param) {
    var sessionId = want.parameters[account_appAccount.Constants.KEY_SESSION_ID];
    try {
      appAccountManager.getAuthCallback(sessionId).then((callback) => {
      var result = {
        accountInfo: {
          name: "Lisi",
          owner: "com.example.accountjsdemo",
        },
        tokenInfo: {
          token: "xxxxxx",
          authType: "getSocialData"
        }
      };
      callback.onResult(0, result);
      }).catch((err) => {
        console.log("getAuthCallback err: "  + JSON.stringify(err));
      });
    } catch (err) {
      console.log("getAuthCallback exception: "  + JSON.stringify(err));
    }
  }
}

queryAuthenticatorInfo9+

queryAuthenticatorInfo(owner: string, callback: AsyncCallback<AuthenticatorInfo>): 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<AuthenticatorInfo> 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

try {
  appAccountManager.queryAuthenticatorInfo("com.example.accountjsdemo", (err, info) => {
    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<AuthenticatorInfo>

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<AuthenticatorInfo> 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

try {
  appAccountManager.queryAuthenticatorInfo("com.example.accountjsdemo").then((info) => { 
      console.log("queryAuthenticatorInfo successfully, info: " + JSON.stringify(info));
  }).catch((err) => {
      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<string>, callback: AsyncCallback<boolean>): 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<string> Yes Labels to check.
callback AsyncCallback<boolean> 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

let labels = ["student"];
try {
  appAccountManager.checkAccountLabels("zhangsan", "com.example.accountjsdemo", labels, (err, hasAllLabels) => {
    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<string>): Promise<boolean>

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<string> Yes Labels to check.

Return value

Type Description
Promise<boolean> 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

let labels = ["student"];
try {
  appAccountManager.checkAccountLabels("zhangsan", "com.example.accountjsdemo", labels).then((hasAllLabels) => {
    console.log('checkAccountLabels successfully: ' + hasAllLabels);
  }).catch((err) => {
    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<void>): 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<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 credentialType.
12300003 Account not found.
12300102 Credential not found.

Example

try {
  appAccountManager.deleteCredential("zhangsan", "PIN_SIX", (err) => {
    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<void>

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<void> 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

try {
  appAccountManager.deleteCredential("zhangsan", "PIN_SIX").then(() => {
    console.log("deleteCredential successfully");
  }).catch((err) => {
    console.log("deleteCredential failed, error: " + JSON.stringify(err));
  });
} catch (err) {
  console.log("deleteCredential exception: "  + JSON.stringify(err));
}

selectAccountsByOptions9+

selectAccountsByOptions(options: SelectAccountsOptions, callback: AsyncCallback<Array<AppAccountInfo>>): 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<Array<AppAccountInfo>> 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

let options = {
  allowedOwners: [ "com.example.accountjsdemo" ],
  requiredLabels: [ "student" ]
};
try {
  appAccountManager.selectAccountsByOptions(options, (err, accountArr) => {
    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<Array<AppAccountInfo>>

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<AppAccountInfo> 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

let options = {
  allowedOwners: ["com.example.accountjsdemo"]
};
try {
  appAccountManager.selectAccountsByOptions(options).then((accountArr) => {
    console.log("selectAccountsByOptions successfully, accountArr: " + JSON.stringify(accountArr));
  }).catch((err) => {
    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

try {
    appAccountManager.verifyCredential("zhangsan", "com.example.accountjsdemo", {
        onResult: (resultCode, result) => {
            console.log("verifyCredential onResult, resultCode:" + JSON.stringify(resultCode));
            console.log("verifyCredential onResult, result:" + JSON.stringify(result));
        },
        onRequestRedirected: (request) => {
            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

let options = {
  credentialType: "pin",
  credential: "123456"
};
try {
  appAccountManager.verifyCredential("zhangsan", "com.example.accountjsdemo", options, {
    onResult: (resultCode, result) => {
      console.log("verifyCredential onResult, resultCode:" + JSON.stringify(resultCode));
      console.log("verifyCredential onResult, result:" + JSON.stringify(result));
    },
    onRequestRedirected: (request) => {
      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

try {
  appAccountManager.setAuthenticatorProperties("com.example.accountjsdemo", {
    onResult: (resultCode, result) => {
      console.log("setAuthenticatorProperties onResult, resultCode:" + JSON.stringify(resultCode));
      console.log("setAuthenticatorProperties onResult, result:" + JSON.stringify(result));
    },
    onRequestRedirected: (request) => {
      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

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

addAccount(deprecated)

addAccount(name: string, callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

appAccountManager.addAccount("WangWu", (err) => { 
    console.log("addAccount err: " + JSON.stringify(err));
});

addAccount(deprecated)

addAccount(name: string, extraInfo: string, callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

appAccountManager.addAccount("LiSi", "token101", (err) => { 
  console.log("addAccount err: " + JSON.stringify(err));
});

addAccount(deprecated)

addAccount(name: string, extraInfo?: string): Promise<void>

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). It cannot contain sensitive information, such as the app account password and token.

Return value

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

Example

appAccountManager.addAccount("LiSi", "token101").then(()=> { 
  console.log('addAccount Success');
}).catch((err) => {
  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



function onResultCallback(code, result) {
  console.log("resultCode: "  + code);
  console.log("result: "  + JSON.stringify(result));
}

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

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

deleteAccount(deprecated)

deleteAccount(name: string, callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

appAccountManager.deleteAccount("ZhaoLiu", (err) => { 
    console.log("deleteAccount err: " + JSON.stringify(err));
 });

deleteAccount(deprecated)

deleteAccount(name: string): Promise<void>

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<void> Promise that returns no value.

Example

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

disableAppAccess(deprecated)

disableAppAccess(name: string, bundleName: string, callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

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

disableAppAccess(deprecated)

disableAppAccess(name: string, bundleName: string): Promise<void>

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<void> Promise that returns no value.

Example

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

enableAppAccess(deprecated)

enableAppAccess(name: string, bundleName: string, callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

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

enableAppAccess(deprecated)

enableAppAccess(name: string, bundleName: string): Promise<void>

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<void> Promise that returns no value.

Example

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

checkAppAccountSyncEnable(deprecated)

checkAppAccountSyncEnable(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.

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<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.

Example

appAccountManager.checkAppAccountSyncEnable("ZhangSan", (err, result) => { 
    console.log("checkAppAccountSyncEnable err: " + JSON.stringify(err));
    console.log('checkAppAccountSyncEnable result: ' + result);
});

checkAppAccountSyncEnable(deprecated)

checkAppAccountSyncEnable(name: string): Promise<boolean>

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<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.

Example

appAccountManager.checkAppAccountSyncEnable("ZhangSan").then((data) => { 
    console.log('checkAppAccountSyncEnable, result: ' + data);
}).catch((err) => {
    console.log("checkAppAccountSyncEnable err: "  + JSON.stringify(err));
});

setAccountCredential(deprecated)

setAccountCredential(name: string, credentialType: string, credential: string,callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

appAccountManager.setAccountCredential("ZhangSan", "credentialType001", "credential001", (err) => { 
    console.log("setAccountCredential err: " + JSON.stringify(err));
});

setAccountCredential(deprecated)

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

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<void> Promise that returns no value.

Example

appAccountManager.setAccountCredential("ZhangSan", "credentialType001", "credential001").then(() => { 
    console.log('setAccountCredential Success');
}).catch((err) => {
    console.log("setAccountCredential err: "  + JSON.stringify(err));
});

setAccountExtraInfo(deprecated)

setAccountExtraInfo(name: string, extraInfo: string, callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

appAccountManager.setAccountExtraInfo("ZhangSan", "Tk002", (err) => { 
    console.log("setAccountExtraInfo err: " + JSON.stringify(err));
});

setAccountExtraInfo(deprecated)

setAccountExtraInfo(name: string, extraInfo: string): Promise<void>

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<void> Promise that returns no value.

Example

appAccountManager.setAccountExtraInfo("ZhangSan", "Tk002").then(() => { 
    console.log('setAccountExtraInfo Success');
}).catch((err) => {
    console.log("setAccountExtraInfo err: "  + JSON.stringify(err));
});

setAppAccountSyncEnable(deprecated)

setAppAccountSyncEnable(name: string, isEnable: boolean, callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

appAccountManager.setAppAccountSyncEnable("ZhangSan", true, (err) => { 
    console.log("setAppAccountSyncEnable err: " + JSON.stringify(err));
});

setAppAccountSyncEnable(deprecated)

setAppAccountSyncEnable(name: string, isEnable: boolean): Promise<void>

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<void> Promise that returns no value.

Example

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

setAssociatedData(deprecated)

setAssociatedData(name: string, key: string, value: string, callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

appAccountManager.setAssociatedData("ZhangSan", "k001", "v001", (err) => { 
    console.log("setAssociatedData err: " + JSON.stringify(err));
});

setAssociatedData(deprecated)

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

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<void> Promise that returns no value.

Example

appAccountManager.setAssociatedData("ZhangSan", "k001", "v001").then(() => { 
    console.log('setAssociatedData Success');
}).catch((err) => {
    console.log("setAssociatedData err: "  + JSON.stringify(err));
});

getAllAccessibleAccounts(deprecated)

getAllAccessibleAccounts(callback: AsyncCallback<Array<AppAccountInfo>>): 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<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.

Example

appAccountManager.getAllAccessibleAccounts((err, data)=>{
	console.debug("getAllAccessibleAccounts err:" + JSON.stringify(err));
	console.debug("getAllAccessibleAccounts data:" + JSON.stringify(data));
});

getAllAccessibleAccounts(deprecated)

getAllAccessibleAccounts(): Promise<Array<AppAccountInfo>>

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<Array<AppAccountInfo>> Promise used to return the accessible app accounts.

Example

appAccountManager.getAllAccessibleAccounts().then((data) => { 
     console.log('getAllAccessibleAccounts: ' + data);
}).catch((err) => {
    console.log("getAllAccessibleAccounts err: "  + JSON.stringify(err));
});

getAllAccounts(deprecated)

getAllAccounts(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.

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<Array<AppAccountInfo>> Yes Callback invoked to return information about all accessible app accounts.

Example

const selfBundle = "com.example.actsgetallaaccounts";
appAccountManager.getAllAccounts(selfBundle, (err, data)=>{
	console.debug("getAllAccounts err:" + JSON.stringify(err));
	console.debug("getAllAccounts data:" + JSON.stringify(data));
});

getAllAccounts(deprecated)

getAllAccounts(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.

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<Array<AppAccountInfo>> Promise use to return the app accounts that can be accessed by the invoker.

Example

const selfBundle = "com.example.actsgetallaaccounts";
appAccountManager.getAllAccounts(selfBundle).then((data) => { 
     console.log('getAllAccounts: ' + data);
}).catch((err) => {
    console.log("getAllAccounts err: "  + JSON.stringify(err));
});

getAccountCredential(deprecated)

getAccountCredential(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.

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<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.

Example

appAccountManager.getAccountCredential("ZhangSan", "credentialType001", (err, result) => { 
    console.log("getAccountCredential err: " + JSON.stringify(err));
    console.log('getAccountCredential result: ' + result);
});

getAccountCredential(deprecated)

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

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<string> Promise used to return the credential obtained.

Example

appAccountManager.getAccountCredential("ZhangSan", "credentialType001").then((data) => { 
    console.log('getAccountCredential, result: ' + data);
}).catch((err) => {
    console.log("getAccountCredential err: "  + JSON.stringify(err));
});

getAccountExtraInfo(deprecated)

getAccountExtraInfo(name: string, callback: AsyncCallback<string>): 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<string> 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

appAccountManager.getAccountExtraInfo("ZhangSan", (err, result) => { 
    console.log("getAccountExtraInfo err: " + JSON.stringify(err));
    console.log('getAccountExtraInfo result: ' + result);
});

getAccountExtraInfo(deprecated)

getAccountExtraInfo(name: string): Promise<string>

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<string> Promise used to return the additional information obtained.

Example

appAccountManager.getAccountExtraInfo("ZhangSan").then((data) => { 
    console.log('getAccountExtraInfo, result: ' + data);
}).catch((err) => {
    console.log("getAccountExtraInfo err: "  + JSON.stringify(err));
});

getAssociatedData(deprecated)

getAssociatedData(name: string, key: string, callback: AsyncCallback<string>): 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<string> 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

appAccountManager.getAssociatedData("ZhangSan", "k001", (err, result) => { 
    console.log("getAssociatedData err: " + JSON.stringify(err));
    console.log('getAssociatedData result: ' + result);
});

getAssociatedData(deprecated)

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

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<string> Promise used to return the data obtained.

Example

appAccountManager.getAssociatedData("ZhangSan", "k001").then((data) => { 
     console.log('getAssociatedData: ' + data);
}).catch((err) => {
    console.log("getAssociatedData err: "  + JSON.stringify(err));
});

on('change')(deprecated)

on(type: 'change', owners: Array<string>, callback: Callback<Array<AppAccountInfo>>): 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<string> Yes App bundle names of the account.
callback Callback<Array<AppAccountInfo>> Yes Callback invoked to return the account changes.

Example

function changeOnCallback(data){
	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<Array<AppAccountInfo>>): 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<Array<AppAccountInfo>> No Callback to unregister.

Example

function changeOnCallback(data){
	console.debug("receive change data:" + JSON.stringify(data));
	appAccountManager.off('change', function(){
		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

function onResultCallback(code, result) {
    console.log("resultCode: "  + code);
    console.log("result: "  + JSON.stringify(result));
}

function onRequestRedirectedCallback(request) {
  let wantInfo = {
    deviceId: '',
    bundleName: 'com.example.accountjsdemo',
    action: 'ohos.want.action.viewData',
    entities: ['entity.system.default'],
  }
  this.context.startAbility(wantInfo).then(() => {
    console.log("startAbility successfully");
  }).catch((err) => {
    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<string>): 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<string> 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

appAccountManager.getOAuthToken("LiSi", "com.example.accountjsdemo", "getSocialData", (err, data) => {
     console.log('getOAuthToken err: ' + JSON.stringify(err));
     console.log('getOAuthToken token: ' + data);
});

getOAuthToken(deprecated)

getOAuthToken(name: string, owner: string, authType: string): Promise<string>

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<string> Promise used to return the result.

Example

appAccountManager.getOAuthToken("LiSi", "com.example.accountjsdemo", "getSocialData").then((data) => {
     console.log('getOAuthToken token: ' + data);
}).catch((err) => {
    console.log("getOAuthToken err: "  + JSON.stringify(err));
});

setOAuthToken(deprecated)

setOAuthToken(name: string, authType: string, token: string, callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

appAccountManager.setOAuthToken("LiSi", "getSocialData", "xxxx", (err) => {
    console.log('setOAuthToken err: ' + JSON.stringify(err));
});

setOAuthToken(deprecated)

setOAuthToken(name: string, authType: string, token: string): Promise<void>

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<void> Promise that returns no value.

Example

appAccountManager.setOAuthToken("LiSi", "getSocialData", "xxxx").then(() => {
    console.log('setOAuthToken successfully');
}).catch((err) => {
    console.log('setOAuthToken err: ' + JSON.stringify(err));
});

deleteOAuthToken(deprecated)

deleteOAuthToken(name: string, owner: string, authType: string, token: string, callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

appAccountManager.deleteOAuthToken("LiSi", "com.example.accountjsdemo", "getSocialData", "xxxxx", (err) => {
     console.log('deleteOAuthToken err: ' + JSON.stringify(err));
});

deleteOAuthToken(deprecated)

deleteOAuthToken(name: string, owner: string, authType: string, token: string): Promise<void>

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<void> Promise that returns no value.

Example

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

setOAuthTokenVisibility(deprecated)

setOAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean, callback: AsyncCallback<void>): 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<void> Yes Callback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Example

appAccountManager.setOAuthTokenVisibility("LiSi", "getSocialData", "com.example.accountjsdemo", true, (err) => {
     console.log('setOAuthTokenVisibility err: ' + JSON.stringify(err));
});

setOAuthTokenVisibility(deprecated)

setOAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean): Promise<void>

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<void> Promise that returns no value.

Example

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

checkOAuthTokenVisibility(deprecated)

checkOAuthTokenVisibility(name: string, authType: string, bundleName: string, callback: AsyncCallback<boolean>): 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<boolean> 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

appAccountManager.checkOAuthTokenVisibility("LiSi", "getSocialData", "com.example.accountjsdemo", (err, data) => {
    console.log('checkOAuthTokenVisibility err: ' + JSON.stringify(err));
    console.log('checkOAuthTokenVisibility isVisible: ' + data);
});

checkOAuthTokenVisibility(deprecated)

checkOAuthTokenVisibility(name: string, authType: string, bundleName: string): Promise<boolean>

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<boolean> 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

appAccountManager.checkOAuthTokenVisibility("LiSi", "getSocialData", "com.example.accountjsdemo").then((data) => {
    console.log('checkOAuthTokenVisibility isVisible: ' + data);
}).catch((err) => {
    console.log('checkOAuthTokenVisibility err: ' + JSON.stringify(err));
});

getAllOAuthTokens(deprecated)

getAllOAuthTokens(name: string, owner: string, callback: AsyncCallback<Array<OAuthTokenInfo>>): 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<Array<OAuthTokenInfo>> 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

appAccountManager.getAllOAuthTokens("LiSi", "com.example.accountjsdemo", (err, data) => {
    console.log("getAllOAuthTokens err: "  + JSON.stringify(err));
    console.log('getAllOAuthTokens data: ' + JSON.stringify(data));
});

getAllOAuthTokens(deprecated)

getAllOAuthTokens(name: string, owner: string): Promise<Array<OAuthTokenInfo>>

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<Array< OAuthTokenInfo>> Promise used to return the tokens obtained.

Example

appAccountManager.getAllOAuthTokens("LiSi", "com.example.accountjsdemo").then((data) => {
    console.log('getAllOAuthTokens data: ' + JSON.stringify(data));
}).catch((err) => {
    console.log("getAllOAuthTokens err: "  + JSON.stringify(err));
});

getOAuthList(deprecated)

getOAuthList(name: string, authType: string, callback: AsyncCallback<Array<string>>): 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<Array<string>> 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

appAccountManager.getOAuthList("com.example.accountjsdemo", "getSocialData", (err, data) => {
  console.log('getOAuthList err: ' + JSON.stringify(err));
  console.log('getOAuthList data: ' + JSON.stringify(data));
});

getOAuthList(deprecated)

getOAuthList(name: string, authType: string): Promise<Array<string>>

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<Array<string>> Promise used to return a list of authorized bundles.

Example

appAccountManager.getOAuthList("com.example.accountjsdemo", "getSocialData").then((data) => {
     console.log('getOAuthList data: ' + JSON.stringify(data));
}).catch((err) => {
    console.log("getOAuthList err: "  + JSON.stringify(err));
});

getAuthenticatorCallback(deprecated)

getAuthenticatorCallback(sessionId: string, callback: AsyncCallback<AuthenticatorCallback>): 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<AuthenticatorCallback> 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 UIAbility from '@ohos.app.ability.UIAbility';

export default class EntryAbility extends UIAbility {
  onCreate(want, param) {
    var sessionId = want.parameters[account_appAccount.Constants.KEY_SESSION_ID];
    appAccountManager.getAuthenticatorCallback(sessionId, (err, callback) => {
      if (err.code != account_appAccount.ResultCode.SUCCESS) {
          console.log("getAuthenticatorCallback err: "  + JSON.stringify(err));
          return;
      }
      var result = {[account_appAccount.Constants.KEY_NAME]: "LiSi",
                    [account_appAccount.Constants.KEY_OWNER]: "com.example.accountjsdemo",
                    [account_appAccount.Constants.KEY_AUTH_TYPE]: "getSocialData",
                    [account_appAccount.Constants.KEY_TOKEN]: "xxxxxx"};
      callback.onResult(account_appAccount.ResultCode.SUCCESS, result);
    });
  }
}

getAuthenticatorCallback(deprecated)

getAuthenticatorCallback(sessionId: string): Promise<AuthenticatorCallback>

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<AuthenticatorCallback> Promise used to return the authenticator callback obtained.

Example

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

export default class EntryAbility extends UIAbility {
  onCreate(want, param) {
    var sessionId = want.parameters[account_appAccount.Constants.KEY_SESSION_ID];
    appAccountManager.getAuthenticatorCallback(sessionId).then((callback) => {
      var result = {[account_appAccount.Constants.KEY_NAME]: "LiSi",
                    [account_appAccount.Constants.KEY_OWNER]: "com.example.accountjsdemo",
                    [account_appAccount.Constants.KEY_AUTH_TYPE]: "getSocialData",
                    [account_appAccount.Constants.KEY_TOKEN]: "xxxxxx"};
      callback.onResult(account_appAccount.ResultCode.SUCCESS, result);
    }).catch((err) => {
      console.log("getAuthenticatorCallback err: "  + JSON.stringify(err));
    });
  }
}

getAuthenticatorInfo(deprecated)

getAuthenticatorInfo(owner: string, callback: AsyncCallback<AuthenticatorInfo>): 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<AuthenticatorInfo> 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

appAccountManager.getAuthenticatorInfo("com.example.accountjsdemo", (err, data) => {
    console.log("getAuthenticatorInfo err: "  + JSON.stringify(err));
    console.log('getAuthenticatorInfo data: ' + JSON.stringify(data));
});

getAuthenticatorInfo(deprecated)

getAuthenticatorInfo(owner: string): Promise<AuthenticatorInfo>

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<AuthenticatorInfo> Promise used to return the authenticator information obtained.

Example

appAccountManager.getAuthenticatorInfo("com.example.accountjsdemo").then((data) => { 
     console.log('getAuthenticatorInfo: ' + JSON.stringify(data));
}).catch((err) => {
    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 Account information of the authorization token.

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 Account information of the authorization token.

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 Account information of the authorization token.
tokenInfo AuthTokenInfo No Token information.

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.

CreateAccountImplicitlyOptions9+

Defines the options for implicitly creating an app account.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
requiredLabels Array<string> No Labels required.
authType string No Authentication type.
parameters {[key: string]: Object} No Customized parameters.

SelectAccountsOptions9+

Defines the options for selecting accounts.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
allowedAccounts Array<AppAccountInfo> No Allowed accounts.
allowedOwners Array<string> No Allowed account owners.
requiredLabels Array<string> No Labels required for the authenticator.

VerifyCredentialOptions9+

Represents the options for verifying the user credential.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
credentialType string No Type of the credential to verify.
credential string No Credential value.
parameters {[key: string]: Object} No Customized parameters.

SetPropertiesOptions9+

Represents the options for setting authenticator properties.

System capability: SystemCapability.Account.AppAccount

Name Type Mandatory Description
properties {[key: string]: Object} No Authenticator properties.
parameters {[key: string]: Object} No Customized parameters.

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) => 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.

Example

let appAccountManager = account_appAccount.createAppAccountManager();
var sessionId = "1234";
appAccountManager.getAuthCallback(sessionId).then((callback) => {
    var result = {
        accountInfo: {
          name: "Lisi",
          owner: "com.example.accountjsdemo",
        },
        tokenInfo: {
          token: "xxxxxx",
          authType: "getSocialData"
        }
    };
    callback.onResult(account_appAccount.ResultCode.SUCCESS, result);
}).catch((err) => {
    console.log("getAuthCallback err: "  + JSON.stringify(err));
});

onRequestRedirected9+

onRequestRedirected: (request: Want) => 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, callback) {
        callback.onRequestRedirected({
            bundleName: "com.example.accountjsdemo",
            abilityName: "com.example.accountjsdemo.LoginAbility",
        });
    }

    auth(name, authType, options, callback) {
        var result = {
          accountInfo: {
            name: "Lisi",
            owner: "com.example.accountjsdemo",
          },
          tokenInfo: {
            token: "xxxxxx",
            authType: "getSocialData"
          }
        };
        callback.onResult(account_appAccount.ResultCode.SUCCESS, result);
    }
}

onRequestContinued9+

onRequestContinued?: () => void

Called to continue to process the request.

System capability: SystemCapability.Account.AppAccount

Example

let appAccountManager = account_appAccount.createAppAccountManager();
var sessionId = "1234";
appAccountManager.getAuthCallback(sessionId).then((callback) => {
    callback.onRequestContinued();
}).catch((err) => {
    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}) => 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

let appAccountManager = account_appAccount.createAppAccountManager();
var sessionId = "1234";
appAccountManager.getAuthenticatorCallback(sessionId).then((callback) => {
    var result = {[account_appAccount.Constants.KEY_NAME]: "LiSi",
                  [account_appAccount.Constants.KEY_OWNER]: "com.example.accountjsdemo",
                  [account_appAccount.Constants.KEY_AUTH_TYPE]: "getSocialData",
                  [account_appAccount.Constants.KEY_TOKEN]: "xxxxxx"};
    callback.onResult(account_appAccount.ResultCode.SUCCESS, result);
}).catch((err) => {
    console.log("getAuthenticatorCallback err: "  + JSON.stringify(err));
});

onRequestRedirected8+

onRequestRedirected: (request: Want) => 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, callerBundleName, options, callback) {
        callback.onRequestRedirected({
            bundleName: "com.example.accountjsdemo",
            abilityName: "com.example.accountjsdemo.LoginAbility",
        });
    }

    authenticate(name, authType, callerBundleName, options, callback) {
        var result = {[account_appAccount.Constants.KEY_NAME]: name,
                      [account_appAccount.Constants.KEY_AUTH_TYPE]: authType,
                      [account_appAccount.Constants.KEY_TOKEN]: "xxxxxx"};
        callback.onResult(account_appAccount.ResultCode.SUCCESS, result);
    }
}

onRequestContinued9+

onRequestContinued?: () => void

Called to continue to process the request.

System capability: SystemCapability.Account.AppAccount

Example

let appAccountManager = account_appAccount.createAppAccountManager();
var sessionId = "1234";
appAccountManager.getAuthenticatorCallback(sessionId).then((callback) => {
    callback.onRequestContinued();
}).catch((err) => {
    console.log("getAuthenticatorCallback err: "  + JSON.stringify(err));
});

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<string>, 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<string> 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

class MyAuthenticator extends account_appAccount.Authenticator {
  addAccountImplicitly(authType, callerBundleName, options, callback) {
    callback.onRequestRedirected({
      bundleName: "com.example.accountjsdemo",
      abilityName: "com.example.accountjsdemo.LoginAbility",
    });
  }

  authenticate(name, authType, callerBundleName, options, callback) {
    var result = {[account_appAccount.Constants.KEY_NAME]: name,
                  [account_appAccount.Constants.KEY_AUTH_TYPE]: authType,
                  [account_appAccount.Constants.KEY_TOKEN]: "xxxxxx"};
    callback.onResult(account_appAccount.ResultCode.SUCCESS, result);
  }

  verifyCredential(name, options, callback) {
    callback.onRequestRedirected({
      bundleName: "com.example.accountjsdemo",
      abilityName: "com.example.accountjsdemo.VerifyAbility",
      parameters: {
        name: name
      }
    });
  }

  setProperties(options, callback) {
    callback.onResult(account_appAccount.ResultCode.SUCCESS, {});
  }

  checkAccountLabels(name, labels, callback) {
    var result = {[account_appAccount.Constants.KEY_BOOLEAN_RESULT]: false};
    callback.onResult(account_appAccount.ResultCode.SUCCESS, result);
  }

  checkAccountRemovable(name, callback) {
    var result = {[account_appAccount.Constants.KEY_BOOLEAN_RESULT]: true};
    callback.onResult(account_appAccount.ResultCode.SUCCESS, result);
  }
}
var authenticator = null;
export default {
  onConnect(want) {
    authenticator = new MyAuthenticator();
    return authenticator.getRemoteObject();
  }
}