js-apis-appAccount.md 60.2 KB
Newer Older
A
annie_wangli 已提交
1
#  	App Account Management
A
annie_wangli 已提交
2

A
annie_wangli 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
>
A
annie_wangli 已提交
5 6 7 8 9 10 11 12 13 14
> 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';
```


A
annie_wangli 已提交
15 16 17 18 19
## System Capabilities

SystemCapability.Account.AppAccount


A
annie_wangli 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
## account_appAccount.createAppAccountManager

createAppAccountManager(): AppAccountManager;

Creates an **AppAccountManager** instance.

- Return value
  | Type| Description|
  | -------- | -------- |
  | AppAccountManager | **AppAccountManager** instance created.|

- Example
  ```
  var appAccountManager = account.createAppAccountManager();
  ```

## AppAccountManager

A
annie_wangli 已提交
38
Provides methods to manage app accounts.
A
annie_wangli 已提交
39 40 41 42 43

### addAccount

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

A
annie_wangli 已提交
44
Adds an app account to the account management service. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
45 46 47 48 49 50 51

Required permission: none.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------------------- | ---- | ------------------------------------------ |
A
annie_wangli 已提交
52 53
  | name     | string              | Yes| Name of the app account to add.|
  | callback | AsyncCallback<void> | Yes| Callback invoked when the app account is added.|
A
annie_wangli 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67

- Example

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

### addAccount

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

A
annie_wangli 已提交
68
Adds an app account and its additional information to the account management service. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
69 70 71 72 73 74 75

Required permission: none.

- Parameters

  | Name| Type| Mandatory| Description|
  | --------- | ------------------- | ---- | ------------------------------------------------------------ |
A
annie_wangli 已提交
76 77 78
  | name      | string              | Yes| Name of the app account to add.|
  | extraInfo | string              | Yes| Additional information (for example, token) of the app account to add. The additional information cannot contain sensitive information about the app account.|
  | callback  | AsyncCallback<void> | Yes| Callback invoked when the app account and its additional information are added.|
A
annie_wangli 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

- Example

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



### addAccount

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

A
annie_wangli 已提交
95
Adds an app account and its additional information to the account management service. This method uses a promise to return the result.
A
annie_wangli 已提交
96 97 98 99 100 101 102

Required permission: none.

- Parameters

  | Name| Type| Mandatory| Description|
  | --------- | ------ | ---- | ------------------------------------------------------------ |
A
annie_wangli 已提交
103 104
  | name      | string | Yes| Name of the app account to add.|
  | extraInfo | string | Yes| Additional information of the app account to add. The additional information cannot contain sensitive information about the app account.|
A
annie_wangli 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

- Return value

  | Type| Description|
  | ------------- | ---------------------------------- |
  | Promise<void> | Promise used to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.addAccount("LiSi", "token101").then(()=> { 
      console.log('addAccount Success');
  }).catch((err) => {
      console.log("addAccount err: "  + JSON.stringify(err));
  });
  ```

A
annie_wangli 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
### addAccountImplicitly<sup>8+</sup>

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

Implicitly adds an app account based on the specified account owner, authentication type, and options. This method uses an asynchronous callback to return the result.

Required permission: none.

- Parameters

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

- Example

  ```
  import featureAbility from '@ohos.ability.featureAbility';
  
  function onResultCallback(code, result) {
      console.log("resultCode: "  + code);
      console.log("result: "  + JSON.stringify(result));
  }
  
  function onRequestRedirectedCallback(request) {
      let abilityStartSetting = {want: request};
      featureAbility.startAbility(abilityStartSetting, (err)=>{
          console.log("startAbility err: " + JSON.stringify(err));
      });
  }
  
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.addAccountImplicitly("LiSi", "readAge", {}, {
      onResult: onResultCallback,
      onRequestRedirected: onRequestRedirectedCallback
  });
  ```

A
annie_wangli 已提交
164 165 166 167
### deleteAccount

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

A
annie_wangli 已提交
168
Deletes an app account from the account management service. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
169 170 171 172 173

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------------------- | ---- | ---------------------------------- |
A
annie_wangli 已提交
174 175
  | name     | string              | Yes| Name of the app account to delete.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked when the app account is deleted.|
A
annie_wangli 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189

- Example

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

### deleteAccount

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

A
annie_wangli 已提交
190
Deletes an app account from the account management service. This method uses a promise to return the result.
A
annie_wangli 已提交
191 192 193 194 195

- Parameters

  | Name| Type| Mandatory| Description|
  | ------ | ------ | ---- | ------------------------ |
A
annie_wangli 已提交
196
  | name   | string | Yes| Name of the app account to delete.|
A
annie_wangli 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

- Return value

  | Type| Description|
  | :------------ | :---------------------------------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example

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

### disableAppAccess

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

A
annie_wangli 已提交
219
Disables an app account from accessing an application with the given bundle name. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
220 221 222 223 224

- Parameters

  | Name| Type| Mandatory| Description|
  | ---------- | ------------------- | ---- | ------------------------------------------------------------ |
A
annie_wangli 已提交
225 226 227
  | name       | string              | Yes| App account name.|
  | bundleName | string              | Yes| Bundle name of an app.|
  | callback   | AsyncCallback&lt;void&gt; | Yes| Callback invoked when the app account is disabled from accessing the application with the given bundle name.|
A
annie_wangli 已提交
228 229 230 231 232

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
Z
zengyawen 已提交
233
  appAccountManager.disableAppAccess("ZhangSan", "com.example.ohos.accountjsdemo", (err) => { 
A
annie_wangli 已提交
234 235 236 237 238 239 240 241
      console.log("disableAppAccess err: " + JSON.stringify(err));
  });
  ```

### disableAppAccess

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

A
annie_wangli 已提交
242
Disables an app account from accessing an application with the given bundle name. This method uses a promise to return the result.
A
annie_wangli 已提交
243 244 245 246 247

- Parameters

  | Name| Type| Mandatory| Description|
  | ---------- | ------ | ---- | ---------------------------------- |
A
annie_wangli 已提交
248 249
  | name       | string | Yes| App account name.|
  | bundleName | string | Yes| Bundle name of an app.|
A
annie_wangli 已提交
250 251 252 253 254 255 256 257 258 259 260

- Return value

  | Type| Description|
  | :------------ | :---------------------------------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
Z
zengyawen 已提交
261
  appAccountManager.disableAppAccess("ZhangSan", "com.example.ohos.accountjsdemo").then(() => { 
A
annie_wangli 已提交
262 263 264 265 266 267 268 269 270 271
      console.log('disableAppAccess Success');
  }).catch((err) => {
      console.log("disableAppAccess err: "  + JSON.stringify(err));
  });
  ```

### enableAppAccess

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

A
annie_wangli 已提交
272
Enables an app account to access an application with the given bundle name. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
273 274 275 276 277

- Parameters

  | Name| Type| Mandatory| Description|
  | ---------- | ------------------- | ---- | ------------------------------------------------------------ |
A
annie_wangli 已提交
278 279 280
  | name       | string              | Yes| App account name.|
  | bundleName | string              | Yes| Bundle name of an app.|
  | callback   | AsyncCallback&lt;void&gt; | Yes| Callback invoked when the app account is enabled to access the application with the given bundle name.|
A
annie_wangli 已提交
281 282 283 284 285

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
Z
zengyawen 已提交
286
  appAccountManager.enableAppAccess("ZhangSan", "com.example.ohos.accountjsdemo", (err) => { 
A
annie_wangli 已提交
287 288 289 290 291 292 293 294
      console.log("enableAppAccess: " + JSON.stringify(err));
   });
  ```

### enableAppAccess

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

A
annie_wangli 已提交
295
Enables an app account to access an application with the given bundle name. This method uses a promise to return the result.
A
annie_wangli 已提交
296 297 298 299 300

- Parameters

  | Name| Type| Mandatory| Description|
  | ---------- | ------ | ---- | ------------------ |
A
annie_wangli 已提交
301 302
  | name       | string | Yes| App account name.|
  | bundleName | string | Yes| Bundle name of an app.|
A
annie_wangli 已提交
303 304 305 306 307 308 309 310 311 312

- Return value

  | Type| Description|
  | :------------ | :---------------------------------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example

  ```
Z
zengyawen 已提交
313
  app_account_instance.enableAppAccess("ZhangSan", "com.example.ohos.accountjsdemo").then(() => { 
A
annie_wangli 已提交
314 315 316 317 318 319 320 321 322 323
       console.log('enableAppAccess Success');
  }).catch((err) => {
      console.log("enableAppAccess err: "  + JSON.stringify(err));
  });
  ```

### checkAppAccountSyncEnable

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

A
annie_wangli 已提交
324
Checks whether an app account allows application data synchronization. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
325 326 327 328 329 330 331

The **ohos.permission.DISTRIBUTED_DATASYNC** permission is required. This permission is intended for system applications only.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ---------------------- | ---- | -------------------------------------------- |
A
annie_wangli 已提交
332 333
  | name     | string                 | Yes| App account name.|
  | callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return whether the app account allows application data synchronization.|
A
annie_wangli 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

- Example

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

### checkAppAccountSyncEnable

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

A
annie_wangli 已提交
349
Checks whether an app account allows application data synchronization. This method uses a promise to return the result.
A
annie_wangli 已提交
350 351 352 353 354 355 356

The **ohos.permission.DISTRIBUTED_DATASYNC** permission is required. This permission is intended for system applications only.

- Parameters

  | Name| Type| Mandatory| Description|
  | ------ | ------ | ---- | -------------- |
A
annie_wangli 已提交
357
  | name   | string | Yes| App account name.|
A
annie_wangli 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

- Return value

  | Type| Description|
  | :--------------- | :---------------------------------- |
  | Promise&lt;boolean&gt; | Promise used to return the result.|

- Example

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

### setAccountCredential

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

A
annie_wangli 已提交
380
Sets a credential for an app account. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
381 382 383 384 385

- Parameters

  | Name| Type| Mandatory| Description|
  | -------------- | ------------------- | ---- | ---------------------------- |
A
annie_wangli 已提交
386
  | name           | string              | Yes| App account name.|
A
annie_wangli 已提交
387 388
  | credentialType | string              | Yes| Type of the credential to set.|
  | credential     | string              | Yes| Credential to set.|
A
annie_wangli 已提交
389
  | callback       | AsyncCallback&lt;void&gt; | Yes| Callback invoked when a credential is set for the specified app account.|
A
annie_wangli 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403

- Example

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

### setAccountCredential

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

A
annie_wangli 已提交
404
Sets a credential for an app account. This method uses a promise to return the result asynchronously.
A
annie_wangli 已提交
405 406 407 408 409

- Parameters

  | Name| Type| Mandatory| Description|
  | -------------- | ------ | ---- | -------------------- |
A
annie_wangli 已提交
410
  | name           | string | Yes| App account name.|
A
annie_wangli 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  | credentialType | string | Yes| Type of the credential to set.|
  | credential     | string | Yes| Credential to set.|

- Return value

  | Type| Description|
  | :------------ | :---------------------------------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example

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

### setAccountExtraInfo

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

A
annie_wangli 已提交
435
Sets additional information for an app account. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
436 437 438 439 440

- Parameters

  | Name| Type| Mandatory| Description|
  | --------- | ------------------- | ---- | -------------------------------- |
A
annie_wangli 已提交
441
  | name      | string              | Yes| App account name.|
A
annie_wangli 已提交
442
  | extraInfo | string              | Yes| Additional information to set.|
A
annie_wangli 已提交
443
  | callback  | AsyncCallback&lt;void&gt; | Yes| Callback invoked when additional information is set for the specified app account.|
A
annie_wangli 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457

- Example

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

### setAccountExtraInfo

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

A
annie_wangli 已提交
458
Sets additional information for an app account. This method uses a promise to return the result asynchronously.
A
annie_wangli 已提交
459 460 461 462 463

- Parameters

  | Name| Type| Mandatory| Description|
  | --------- | ------ | ---- | ------------------ |
A
annie_wangli 已提交
464
  | name      | string | Yes| App account name.|
A
annie_wangli 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
  | extraInfo | string | Yes| Additional information to set.|

- Return value

  | Type| Description|
  | :------------ | :---------------------------------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example

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

### setAppAccountSyncEnable

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

A
annie_wangli 已提交
488
Sets whether to enable application data synchronization for an app account. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
489 490 491 492 493 494 495

The **ohos.permission.DISTRIBUTED_DATASYNC** permission is required. This permission is intended for system applications only.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------------------- | ---- | -------------------------------------------------- |
A
annie_wangli 已提交
496 497 498
  | name     | string              | Yes| App account name.|
  | isEnable | boolean             | Yes| Whether to enable app data synchronization.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked when application data synchronization is enabled or disabled for the app account.|
A
annie_wangli 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511 512

- Example

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

### setAppAccountSyncEnable

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

A
annie_wangli 已提交
513
Sets whether to enable application data synchronization for an app account. This method uses a promise to return the result asynchronously.
A
annie_wangli 已提交
514 515 516 517 518 519 520

The **ohos.permission.DISTRIBUTED_DATASYNC** permission is required. This permission is intended for system applications only.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------- | ---- | ---------------------- |
A
annie_wangli 已提交
521 522
  | name     | string  | Yes| App account name.|
  | isEnable | boolean | Yes| Whether to enable app data synchronization.|
A
annie_wangli 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

- Return value

  | Type| Description|
  | :------------ | :---------------------------------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example

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

### setAssociatedData

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

A
annie_wangli 已提交
545
Sets data to be associated with an app account. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
546 547 548 549 550

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------------------- | ---- | ---------------------------------- |
A
annie_wangli 已提交
551
  | name     | string              | Yes| App account name.|
A
annie_wangli 已提交
552 553
  | key      | string              | Yes| Key of the data to set. The private key can be customized.|
  | value    | string              | Yes| Value of the data to be set.|
A
annie_wangli 已提交
554
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked when the data associated with the specified app account is set.|
A
annie_wangli 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567

- Example

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

### setAssociatedData

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

A
annie_wangli 已提交
568
Sets data to be associated with an app account. This method uses a promise to return the result asynchronously.
A
annie_wangli 已提交
569 570 571 572 573

- Parameters

  | Name| Type| Mandatory| Description|
  | ------ | ------ | ---- | ---------------------------------- |
A
annie_wangli 已提交
574
  | name   | string | Yes| App account name.|
A
annie_wangli 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
  | key    | string | Yes| Key of the data to set. The private key can be customized.|
  | value  | string | Yes| Value of the data to be set.|

- Return value

  | Type| Description|
  | :------------ | :---------------------------------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example

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

### getAccountCredential

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

A
annie_wangli 已提交
599
Obtains the credential of an app account. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
600 601 602 603 604

- Parameters

  | Name| Type| Mandatory| Description|
  | -------------- | --------------------- | ---- | ---------------------------- |
A
annie_wangli 已提交
605
  | name           | string                | Yes| App account name.|
A
annie_wangli 已提交
606
  | credentialType | string                | Yes| Type of the credential to obtain.|
A
annie_wangli 已提交
607
  | callback       | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the credential of the specified app account.|
A
annie_wangli 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622

- Example

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

### getAccountCredential

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

A
annie_wangli 已提交
623
Obtains the credential of an app account. This method uses a promise to return the result asynchronously.
A
annie_wangli 已提交
624 625 626 627 628

- Parameters

  | Name| Type| Mandatory| Description|
  | -------------- | ------ | ---- | -------------------- |
A
annie_wangli 已提交
629
  | name           | string | Yes| App account name.|
A
annie_wangli 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
  | credentialType | string | Yes| Type of the credential to obtain.|

- Return value

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

- Example

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

### getAccountExtraInfo

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

A
annie_wangli 已提交
653
Obtains additional information of an app account. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
654 655 656 657 658

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | --------------------- | ---- | -------------------------------- |
A
annie_wangli 已提交
659 660
  | name     | string                | Yes| App account name.|
  | callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the additional information of the specified app account.|
A
annie_wangli 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

- Example

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

### getAccountExtraInfo

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

A
annie_wangli 已提交
676
Obtains additional information of an app account. This method uses a promise to return the result asynchronously.
A
annie_wangli 已提交
677 678 679 680 681

- Parameters

  | Name| Type| Mandatory| Description|
  | ------ | ------ | ---- | -------------- |
A
annie_wangli 已提交
682
  | name   | string | Yes| App account name.|
A
annie_wangli 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704

- Return value

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

- Example

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

### getAssociatedData

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

A
annie_wangli 已提交
705
Obtains data associated with an app account. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
706 707 708 709 710

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | --------------------- | ---- | ---------------------------------- |
A
annie_wangli 已提交
711 712 713
  | name     | string                | Yes| App account name.|
  | key      | string                | Yes| Key of the data to obtain.|
  | callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the data associated with the specified app account.|
A
annie_wangli 已提交
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

- Example

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

### getAssociatedData

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

A
annie_wangli 已提交
729
Obtains data associated with an app account. This method uses a promise to return the result asynchronously.
A
annie_wangli 已提交
730 731 732 733 734

- Parameters

  | Name| Type| Mandatory| Description|
  | ------ | ------ | ---- | ------------------- |
A
annie_wangli 已提交
735
  | name   | string | Yes| App account name.|
A
annie_wangli 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
  | key    | string | Yes| Key of the data to obtain.|

- Return value

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

- Example

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

### getAllAccessibleAccounts

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

A
annie_wangli 已提交
759
Obtains information about all accessible app accounts. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
760 761 762 763 764 765 766

The **ohos.permission.GET_ACCOUNTS_PRIVILEGED** permission is required. This permission is intended for system applications only.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------------------------------------ | ---- | ---------------- |
A
annie_wangli 已提交
767
  | callback | AsyncCallback&lt;Array&lt;AppAccountInfo&gt;&gt; | Yes| Callback invoked to return information about all accessible app accounts.|
A
annie_wangli 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782

- Example

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

### getAllAccessibleAccounts

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

A
annie_wangli 已提交
783
Obtains information about all accessible app accounts. This method uses a promise to return the result.
A
annie_wangli 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

The **ohos.permission.GET_ACCOUNTS_PRIVILEGED** permission is required. This permission is intended for system applications only.

- Parameters

  | Type| Description|
  | ------------------------------ | ----------------------------------- |
  | Promise&lt;Array&lt;AppAccountInfo&gt;&gt; | Promise used to return the result.|

- Example

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

### getAllAccounts

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

A
annie_wangli 已提交
808
Obtains information about all app accounts of the specified app. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
809 810 811 812 813 814 815

The **ohos.permission.GET_ACCOUNTS_PRIVILEGED** permission is required. This permission is intended for system applications only.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------------------------------------ | ---- | ---------------- |
A
annie_wangli 已提交
816 817
  | owner    | string                               | Yes| Bundle name of the app.|
  | callback | AsyncCallback&lt;Array&lt;AppAccountInfo&gt;&gt; | Yes| Callback invoked to return information about all app accounts.|
A
annie_wangli 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

- Example

  ```
  const appAccountManager = account.createAppAccountManager();
  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

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

A
annie_wangli 已提交
834
Obtains information about all app accounts of the specified app. This method uses a promise to return the result.
A
annie_wangli 已提交
835 836 837 838 839 840 841

The **ohos.permission.GET_ACCOUNTS_PRIVILEGED** permission is required. This permission is intended for system applications only.

- Parameters

  | Name| Type| Mandatory| Description|
  | ------ | ------ | ---- | ---------- |
A
annie_wangli 已提交
842
  | owner  | string | Yes| Bundle name of the app.|
A
annie_wangli 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

- Parameters

  | Type| Description|
  | ------------------------------ | ----------------------------------- |
  | Promise&lt;Array&lt;AppAccountInfo&gt;&gt; | Promise used to return the result.|

- Example

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

### on('change')

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

Subscribes to the account change event of the specified account owners. This method uses an asynchronous callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
A
annie_wangli 已提交
872 873
  | type     | 'change'                        | Yes| Type of the event to subscribe to. The subscriber will receive a notification when the account owners update their accounts.|
  | owners   | Array&lt;string&gt;                   | Yes| Owners of the accounts.|
A
annie_wangli 已提交
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
  | callback | Callback&lt;Array&lt;AppAccountInfo&gt;&gt; | Yes| Callback invoked to return the account change.|

- Example

  ```
  const appAccountManager = account.createAppAccountManager();
  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')

off(type: 'change', callback?: Callback&lt;void&gt;): void;

Unsubscribes from the account change event. This method uses an asynchronous callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | -------------------- | ---- | ------------------------ |
A
annie_wangli 已提交
901
  | type     | 'change'             | Yes| Account change event to unsubscribe from.|
A
annie_wangli 已提交
902 903 904 905 906 907 908 909 910 911
  | callback | Callback&lt;void&gt; | No| Callback used to report the account change.|

- Example

  ```
  const appAccountManager = account.createAppAccountManager();
  function changeOnCallback(data){
  	console.debug("receive change data:" + JSON.stringify(data));
  	appAccountManager.off('change', function(){
  		console.debug("off finish");
A
annie_wangli 已提交
912
  	})
A
annie_wangli 已提交
913 914 915 916 917 918 919 920 921
  }
  try{
  	appAccountManager.on('change', ["com.example.actsaccounttest"], changeOnCallback);
  }
  catch(err){
  	console.error("on accountOnOffDemo err:" + JSON.stringify(err));
  }
  ```

A
annie_wangli 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
### authenticate<sup>8+</sup>

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

Authenticates an app account to obtain the Open Authorization (OAuth) access token. This method uses an asynchronous callback to return the result.

- Parameters

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

- Example

  ```
  import featureAbility from '@ohos.ability.featureAbility';
  
  function onResultCallback(code, result) {
      console.log("resultCode: "  + code);
      console.log("result: "  + JSON.stringify(result));
  }
  
  function onRequestRedirectedCallback(request) {
      let abilityStartSetting = {want: request};
      featureAbility.startAbility(abilityStartSetting, (err)=>{
          console.log("startAbility err: " + JSON.stringify(err));
      });
  }
  
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.authenticate("LiSi", "com.example.ohos.accountjsdemo", "readAge", {}, {
    onResult: onResultCallback,
    onRequestRedirected: onRequestRedirectedCallback
  });
  ```

### getOAuthToken<sup>8+</sup>

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

Obtains the OAuth access token of an app account based on the specified authentication type. This method uses an asynchronous callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | --------------------------- | ---- | -------------------- |
  | name     | string                      | Yes| App account name.|
  | owner    | string                      | Yes| Bundle name of the app.|
  | authType | string                      | Yes| Authentication type.|
  | callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.getOAuthToken("LiSi", "com.example.ohos.accountjsdemo", "readAge", (err, data) => {
       console.log('getOAuthToken err: ' + JSON.stringify(err));
       console.log('getOAuthToken token: ' + data);
  });
  ```

### getOAuthToken<sup>8+</sup>

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

Obtains the OAuth access token of an app account based on the specified authentication type. This method uses a promise to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------ | ---- | -------------------- |
  | name     | string | Yes| App account name.|
  | owner    | string | Yes| Bundle name of the app.|
  | authType | string | Yes| Authentication type.|

- Parameters

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

- Example

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

### setOAuthToken<sup>8+</sup>

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

Sets an OAuth access token for an app account. This method uses an asynchronous callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------------------------- | ---- | -------------  |
  | name     | string                    | Yes| App account name.|
  | authType | string                    | Yes| Authentication type.|
  | token    | string                    | Yes| OAuth access token to set.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.setOAuthToken("LiSi", "readAge", "xxxx", (err) => {
      console.log('setOAuthToken err: ' + JSON.stringify(err));
  });
  ```

### setOAuthToken<sup>8+</sup>

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

Sets an OAuth access token for an app account. This method uses a promise to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------ | ---- | -------------  |
  | name     | string | Yes| App account name.|
  | authType | string | Yes| Authentication type.|
  | token    | string | Yes| OAuth access token to set.|

- Parameters

  | Type| Description|
  | ------------------- | -------------------------------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example

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

### deleteOAuthToken<sup>8+</sup>

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

Deletes the specified OAuth access token for an app account. This method uses an asynchronous callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------------------------- | ---- | ------------------  |
  | name     | string                    | Yes| App account name.|
  | owner    | string                    | Yes| Bundle name of the app.|
  | authType | string                    | Yes| Authentication type.|
  | token    | string                    | Yes| OAuth access token to delete.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.deleteOAuthToken("LiSi", "com.example.ohos.accountjsdemo", "readAge", "xxxxx", (err) => {
       console.log('deleteOAuthToken err: ' + JSON.stringify(err));
  });
  ```

### deleteOAuthToken<sup>8+</sup>

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

Deletes the specified OAuth access token for an app account. This method uses a promise to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------ | ---- | ------------------  |
  | name     | string | Yes| App account name.|
  | owner    | string | Yes| Bundle name of the app.|
  | authType | string | Yes| Authentication type.|
  | token    | string | Yes| OAuth access token to delete.|

- Parameters

  | Type| Description|
  | ------------------------------ | --------------------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example

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

### setOAuthTokenVisibility<sup>8+</sup>

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

Sets the visibility of an OAuth access token to the specified app. This method uses an asynchronous callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | ---------- | ------------------------- | ---- | -------------------  |
  | name       | string                    | Yes| App account name.|
  | authType   | string                    | Yes| Authentication type.|
  | bundleName | string                    | Yes| Bundle name of the app.|
  | isVisible  | boolean                   | Yes| Whether the OAuth access token is visible to the app.|
  | callback   | AsyncCallback&lt;void&gt; | Yes| Callback invoked to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.setOAuthTokenVisibility("LiSi", "readAge", "com.example.ohos.accountjsdemo", true, (err) => {
       console.log('setOAuthTokenVisibility err: ' + JSON.stringify(err));
  });
  ```

### setOAuthTokenVisibility<sup>8+</sup>

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

Sets the visibility of an OAuth access token to the specified app. This method uses a promise to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | ---------- | ------------------------- | ---- | -------------------  |
  | name       | string                    | Yes| App account name.|
  | authType   | string                    | Yes| Authentication type.|
  | bundleName | string                    | Yes| Bundle name of the app.|
  | isVisible  | boolean                   | Yes| Whether the OAuth access token is visible to the app.|

- Parameters

  | Type| Description|
  | ------------------------------ | --------------------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example

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

### checkOAuthTokenVisibility<sup>8+</sup>

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

Checks whether an OAuth token is visible to the specified app. This method uses an asynchronous callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | ---------- | ---------------------------- | ---- | ----------------------  |
  | name       | string                       | Yes| App account name.|
  | authType   | string                       | Yes| Authentication type.|
  | bundleName | string                       | Yes| Bundle name of the app.|
  | callback   | AsyncCallback&lt;boolean&gt; | Yes| Callback invoked to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.checkOAuthTokenVisibility("LiSi", "readAge", "com.example.ohos.accountjsdemo", true, (err, data) => {
      console.log('checkOAuthTokenVisibility err: ' + JSON.stringify(err));
      console.log('checkOAuthTokenVisibility isVisible: ' + data);
  });
  ```

### checkOAuthTokenVisibility<sup>8+</sup>

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

Checks whether an OAuth token is visible to the specified app. This method uses a promise to return the result.

- Parameters

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

- Parameters

  | Type| Description|
  | ------------------------------ | ------------------------ |
  | Promise&lt;boolean&gt; | Promise used to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.checkOAuthTokenVisibility("LiSi", "readAge", "com.example.ohos.accountjsdemo", true).then((data) => {
      console.log('checkOAuthTokenVisibility isVisible: ' + data);
  }).catch((err) => {
      console.log('checkOAuthTokenVisibility err: ' + JSON.stringify(err));
  });
  ```

### getAllOAuthTokens<sup>8+</sup>

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

Obtains information about all OAuth access tokens of an app account visible to the specified app. This method uses an asynchronous callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------------------------------------------------ | ---- | -------------------  |
  | name     | string                                           | Yes| App account name.|
  | owner    | string                                           | Yes| Bundle name of the app.|
  | callback | AsyncCallback&lt;Array&lt;OAuthTokenInfo&gt;&gt; | Yes| Callback invoked to return the result.|

- Example

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

### getAllOAuthTokens<sup>8+</sup>

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

Obtains information about all OAuth access tokens of an app account visible to the specified app. This method uses a promise to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------ | ---- | -------------------  |
  | name     | string | Yes| App account name.|
  | owner    | string | Yes| Bundle name of the app.|

- Parameters

  | Type| Description|
  | ------------------------------ | ----------------------------------- |
  | Promise&lt;Array&lt;OAuthTokenInfo&gt;&gt; | Promise used to return the result.|

- Example

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

### getOAuthList<sup>8+</sup>

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

Obtains the authorization list of OAuth access tokens of an app account. This method uses an asynchronous callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ---------------------------------------- | ---- | ------------------  |
  | name     | string                                   | Yes| App account name.|
  | owner    | string                                   | Yes| Bundle name of the app.|
  | callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | Yes| Callback invoked to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.getOAuthList("com.example.ohos.accountjsdemo", "readAge", (err, data) => {
       console.log('getOAuthList err: ' + JSON.stringify(err));
       console.log('getOAuthList data: ' + JSON.stringify(data));
  });
  ```

### getOAuthList<sup>8+</sup>

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

Obtains the authorization list of OAuth access tokens of an app account. This method uses a promise to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | ------ | ---- | -------------------  |
  | name     | string | Yes| App account name.|
  | owner    | string | Yes| Bundle name of the app.|

- Parameters

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

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.getOAuthList("com.example.ohos.accountjsdemo", "readAge").then((data) => {
       console.log('getOAuthList data: ' + JSON.stringify(data));
  }).catch((err) => {
      console.log("getOAuthList err: "  + JSON.stringify(err));
  });
  ```

### getAuthenticatorCallback<sup>8+</sup>

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

Obtains the authenticator callback for a session. This method uses an asynchronous callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | --------- | ------------------------------------------ | ---- | -------------- |
  | sessionId | string                                     | Yes| ID of the session to authenticate.|
  | callback  | AsyncCallback&lt;AuthenticatorCallback&gt; | Yes| Callback invoked to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  featureAbility.getWant((err, want) => {
    var sessionId = want.parameters[Constants.KEY_SESSION_ID];
    appAccountManager.getAuthenticatorCallback(sessionId, (err, callback) => {
        if (err.code != ResultCode.SUCCESS) {
            console.log("getAuthenticatorCallback err: "  + JSON.stringify(err));
            return;
        }
        var result = {Constants.KEY_NAME: "LiSi", Constants.KEY_OWNER: "com.example.ohos.accountjsdemo",
                      Constants.KEY_AUTH_TYPE: "readAge", Constants.KEY_TOKEN: "xxxxxx"};
        callback.OnResult(ResultCode.SUCCESS, result);
    });
  });
  ```

### getAuthenticatorCallback<sup>8+</sup>

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

Obtains the authenticator callback for a session. This method uses a promise to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | ---------- | ------ | ---- | -------------- |
  | sessionId  | string | Yes| ID of the session to authenticate.|

- Parameters

  | Type| Description|
  | ------------------------------------ | -------------------------------- |
  | Promise&lt;AuthenticatorCallback&gt; | Promise used to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  featureAbility.getWant().then((want) => {
      var sessionId = want.parameters[Constants.KEY_SESSION_ID];
      appAccountManager.getAuthenticatorCallback(sessionId).then((callback) => {
          var result = {Constants.KEY_NAME: "LiSi", Constants.KEY_OWNER: "com.example.ohos.accountjsdemo",
                        Constants.KEY_AUTH_TYPE: "readAge", Constants.KEY_TOKEN: "xxxxxx"};
          callback.OnResult(ResultCode.SUCCESS, result);
      }).catch((err) => {
          console.log("getAuthenticatorCallback err: "  + JSON.stringify(err));
      });
  }).catch((err) => {
      console.log("getWant err: "  + JSON.stringify(err));
  });
  ```

### getAuthenticatorInfo<sup>8+</sup>

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

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

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | -------------------------------------- | ---- | ------------------- |
  | owner    | string                                 | Yes| Bundle name of the app.|
  | callback | AsyncCallback&lt;AuthenticatorInfo&gt; | Yes| Callback invoked to return the result.|

- Example

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

### getAuthenticatorInfo<sup>8+</sup>

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

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

- Parameters

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

- Parameters

  | Type| Description|
  | ------------------------------ | ----------------------------------- |
  | Promise&lt;AuthenticatorInfo&gt; | Promise used to return the result.|

- Example

  ```
  const appAccountManager = account_appAccount.createAppAccountManager();
  appAccountManager.getAuthenticatorInfo("com.example.ohos.accountjsdemo").then((data) => { 
       console.log('getAuthenticatorInfo: ' + JSON.stringify(data));
  }).catch((err) => {
      console.log("getAuthenticatorInfo err: "  + JSON.stringify(err));
  });
  ```

A
annie_wangli 已提交
1471 1472
## AppAccountInfo

A
annie_wangli 已提交
1473 1474 1475 1476 1477 1478 1479 1480
Defines app account information.

| Name| Type| Mandatory| Description|
| ----- | ------ | ---- | ---------------- |
| owner | string | Yes| Bundle name of the app.|
| name  | string | Yes| App account name.|

## OAuthTokenInfo<sup>8+</sup>
A
annie_wangli 已提交
1481

A
annie_wangli 已提交
1482
Defines OAuth access token information.
A
annie_wangli 已提交
1483 1484

| Name| Type| Mandatory| Description|
A
annie_wangli 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
| -------- | ------ | ---- | -------------- |
| authType | string | Yes| Authentication type.|
| token    | string | Yes| Value of the access token.|

## AuthenticatorInfo<sup>8+</sup>

Defines OAuth authenticator information.

| Name| Type| Mandatory| Description|
| ------- | ------ | ---- | ------------------ |
| owner   | string | Yes| Bundle name of the authenticator owner.|
| iconId  | string | Yes| ID of the authenticator icon.|
| labelId | string | Yes| ID of the authenticator label.|

## Constants<sup>8+</sup>

Enumerates the constants.

| Name| Default Value| Description|
| ----------------------------- | ---------------------- | ----------------------- |
| ACTION_ADD_ACCOUNT_IMPLICITLY | "addAccountImplicitly" | Operation for implicitly adding an account.|
| ACTION_AUTHENTICATE           | "authenticate"         | Authentication operation.|
| KEY_NAME                      | "name"                 | App account name.|
| KEY_OWNER                     | "owner"                | App account owner.|
| KEY_TOKEN                     | "token"                | OAuth access token.|
| KEY_ACTION                    | "action"               | Action.|
| KEY_AUTH_TYPE                 | "authType"             | Authentication type.|
| KEY_SESSION_ID                | "sessionId"            | Session ID.|
| KEY_CALLER_PID                | "callerPid"            | Caller process ID (PID).|
| KEY_CALLER_UID                | "callerUid"            | Caller user ID (UID).|
| KEY_CALLER_BUNDLE_NAME        | "callerBundleName"     | Caller bundle name.|

## ResultCode<sup>8+</sup>

Enumerates the result codes.

| Name| Default 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 app account 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 OAuth access token does not exist.|
| ERROR_OAUTH_TOKEN_TOO_MANY          | 10015 | The number of OAuth access 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.|

## AuthenticatorCallback<sup>8+</sup>

Provides methods for managing the OAuth authenticator callback.

### onResult<sup>8+</sup>

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

Called back to send the authentication result.

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

- Example

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

### onRequestRedirected<sup>8+</sup>

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

Called back to redirect an authentication request.

- 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.ohos.accountjsdemo",
              abilityName: "com.example.ohos.accountjsdemo.LoginAbility",
          });
      }
  
      authenticate(name, authType, callerBundleName, options, callback) {
          var result = {Constants.KEY_NAME: name, Constants.KEY_AUTH_TYPE: authType, Constants.KEY_TOKEN: "xxxxxx"};
          callback.onResult(ResultCode.SUCCESS, result);
      }
  }
  ```

## Authenticator<sup>8+</sup>

Defines the OAuth authenticator base class.

### addAccountImplicitly<sup>8+</sup>

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

Implicitly adds an app account based on the specified authentication type and options. This method uses an asynchronous callback to return the result.

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

### authenticate<sup>8+</sup>

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

Authenticates an app account to obtain the OAuth access token. This method uses an asynchronous callback to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | ---------------- | --------------------- | ---- | --------------------------  |
  | name             | string                | Yes| App account name.|
  | 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.|

- Example

  ```
  class MyAuthenticator extends account_appAccount.Authenticator {
      addAccountImplicitly(authType, callerBundleName, options, callback) {
          callback.onRequestRedirected({
              bundleName: "com.example.ohos.accountjsdemo",
              abilityName: "com.example.ohos.accountjsdemo.LoginAbility",
          });
      }
  
      authenticate(name, authType, callerBundleName, options, callback) {
          var result = {Constants.KEY_NAME: name, Constants.KEY_AUTH_TYPE: authType, Constants.KEY_TOKEN: "xxxxxx"};
          callback.onResult(ResultCode.SUCCESS, result);
      }
  }
  
  export default {
      onConnect(want) {
          return new MyAuthenticator();
      }
  }
  ```