1# @ohos.account.appAccount (应用账号管理) 2 3本模块提供应用账号信息的添加、删除、修改和查询基础能力,并支持应用间鉴权和分布式数据同步功能。 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9 10## 导入模块 11 12```ts 13import { appAccount } from '@kit.BasicServicesKit'; 14``` 15 16 17## appAccount.createAppAccountManager 18 19createAppAccountManager(): AppAccountManager 20 21创建应用账号管理器对象。 22 23**系统能力:** SystemCapability.Account.AppAccount 24 25**返回值:** 26 27| 类型 | 说明 | 28| ----------------- | ------------ | 29| AppAccountManager | 应用账号管理器对象。 | 30 31**示例:** 32 ```ts 33 let appAccountManager: appAccount.AppAccountManager = appAccount.createAppAccountManager(); 34 ``` 35 36## AppAccountManager 37 38应用账号管理器类。 39 40### createAccount<sup>9+</sup> 41 42createAccount(name: string, callback: AsyncCallback<void>): void 43 44根据账号名创建应用账号。使用callback异步回调。 45 46**系统能力:** SystemCapability.Account.AppAccount 47 48**参数:** 49 50| 参数名 | 类型 | 必填 | 说明 | 51| -------- | ------------------------- | ----- | -------------------- | 52| name | string | 是 | 应用账号的名称。 | 53| callback | AsyncCallback<void> | 是 | 回调函数。当创建成功时,err为null,否则为错误对象。 | 54 55**错误码:** 56 57| 错误码ID | 错误信息 | 58| ------- | ------- | 59| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 60| 12300001 | System service exception. | 61| 12300002 | Invalid name. | 62| 12300004 | Account already exists. | 63| 12300007 | The number of accounts reaches the upper limit. | 64 65**示例:** 66 67 ```ts 68 import { BusinessError } from '@kit.BasicServicesKit'; 69 70 try { 71 appAccountManager.createAccount('WangWu', (err: BusinessError) => { 72 console.log('createAccount err: ' + JSON.stringify(err)); 73 }); 74 } catch (err) { 75 console.log('createAccount err: ' + JSON.stringify(err)); 76 } 77 ``` 78 79### createAccount<sup>9+</sup> 80 81createAccount(name: string, options: CreateAccountOptions, callback: AsyncCallback<void>): void 82 83根据账号名和可选项创建应用账号。使用callback异步回调。 84 85**系统能力:** SystemCapability.Account.AppAccount 86 87**参数:** 88 89| 参数名 | 类型 | 必填 | 说明 | 90| --------- | ------------------------- | ---- | ---------------------------------------- | 91| name | string | 是 | 应用账号的名称。 | 92| options | [CreateAccountOptions](#createaccountoptions9) | 是 | 创建应用账号的选项,可提供自定义数据,但不建议包含敏感数据(如密码、Token等)。 | 93| callback | AsyncCallback<void> | 是 | 回调函数。当创建成功时,err为null,否则为错误对象。 | 94 95**错误码:** 96 97| 错误码ID | 错误信息 | 98| ------- | ------- | 99| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 100| 12300001 | System service exception. | 101| 12300002 | Invalid name or options. | 102| 12300004 | Account already exists. | 103| 12300007 | The number of accounts reaches the upper limit. | 104 105**示例:** 106 107 ```ts 108 import { BusinessError } from '@kit.BasicServicesKit'; 109 110 let options:appAccount.CreateAccountOptions = { 111 customData: { 112 age: '10' 113 } 114 } 115 try { 116 appAccountManager.createAccount('LiSi', options, (err: BusinessError) => { 117 if (err) { 118 console.log('createAccount failed, error: ' + JSON.stringify(err)); 119 } else { 120 console.log('createAccount successfully'); 121 } 122 }); 123 } catch(err) { 124 console.log('createAccount exception: ' + JSON.stringify(err)); 125 } 126 ``` 127 128### createAccount<sup>9+</sup> 129 130createAccount(name: string, options?: CreateAccountOptions): Promise<void> 131 132根据账号名和可选项创建应用账号。使用Promise异步回调。 133 134**系统能力:** SystemCapability.Account.AppAccount 135 136**参数:** 137 138| 参数名 | 类型 | 必填 | 说明 | 139| --------- | ------ | ---- | ---------------------------------------- | 140| name | string | 是 | 应用账号的名称。 | 141| options | [CreateAccountOptions](#createaccountoptions9) | 否 | 创建应用账号的选项,可提供自定义数据,但不建议包含敏感数据(如密码、Token等)。不填无影响,默认为空,表示创建的该账号无额外信息需要添加。 | 142 143**返回值:** 144 145| 类型 | 说明 | 146| ------------------- | --------------------- | 147| Promise<void> | 无返回结果的Promise对象。 | 148 149**错误码:** 150 151| 错误码ID | 错误信息| 152| ------- | -------| 153| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 154| 12300001 | System service exception. | 155| 12300002 | Invalid name or options. | 156| 12300004 | Account already exists. | 157| 12300007 | The number of accounts reaches the upper limit. | 158 159**示例:** 160 161 ```ts 162 import { BusinessError } from '@kit.BasicServicesKit'; 163 164 let options: appAccount.CreateAccountOptions = { 165 customData: { 166 age: '10' 167 } 168 } 169 try { 170 appAccountManager.createAccount('LiSi', options).then(() => { 171 console.log('createAccount successfully'); 172 }).catch((err: BusinessError) => { 173 console.log('createAccount failed, error: ' + JSON.stringify(err)); 174 }); 175 } catch(err) { 176 console.log('createAccount exception: ' + JSON.stringify(err)); 177 } 178 ``` 179 180### createAccountImplicitly<sup>9+</sup> 181 182createAccountImplicitly(owner: string, callback: AuthCallback): void 183 184根据指定的账号所有者隐式地创建应用账号。使用callback异步回调。 185 186**系统能力:** SystemCapability.Account.AppAccount 187 188**参数:** 189 190| 参数名 | 类型 | 必填 | 说明 | 191| -------- | --------------------- | ---- | ----------------------- | 192| owner | string | 是 | 应用账号所有者的包名。 | 193| callback | [AuthCallback](#authcallback9) | 是 | 认证器回调对象,返回创建结果。 | 194 195**错误码:** 196 197| 错误码ID | 错误信息| 198| ------- | -------| 199| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 200| 12300001 | System service exception. | 201| 12300002 | Invalid owner. | 202| 12300007 | The number of accounts reaches the upper limit. | 203| 12300010 | Account service busy. | 204| 12300113 | Authenticator service not found. | 205| 12300114 | Authenticator service exception. | 206 207**示例:** 208 209 ```ts 210 import { BusinessError } from '@kit.BasicServicesKit'; 211 import { Want, common } from '@kit.AbilityKit'; 212 213 @Entry 214 @Component 215 struct Index { 216 context = this.getUIContext().getHostContext() as common.UIAbilityContext; // UIAbilityContext 217 218 onResultCallback(code: number, result?: appAccount.AuthResult): void { 219 console.log('resultCode: ' + code); 220 console.log('result: ' + JSON.stringify(result)); 221 } 222 223 onRequestRedirectedCallback(request: Want): void { 224 let wantInfo: Want = { 225 deviceId: '', 226 bundleName: 'com.example.accountjsdemo', 227 action: 'ohos.want.action.viewData', 228 entities: ['entity.system.default'], 229 } 230 this.context.startAbility(wantInfo).then(() => { 231 console.log('startAbility successfully'); 232 }).catch((err: BusinessError) => { 233 console.log('startAbility err: ' + JSON.stringify(err)); 234 }) 235 } 236 237 aboutToAppear(): void { 238 try { 239 appAccountManager.createAccountImplicitly('com.example.accountjsdemo', { 240 onResult: this.onResultCallback, 241 onRequestRedirected: this.onRequestRedirectedCallback 242 }); 243 } catch (err) { 244 console.log('createAccountImplicitly exception: ' + JSON.stringify(err)); 245 } 246 } 247 build() {} 248 } 249 ``` 250 251### createAccountImplicitly<sup>9+</sup> 252 253createAccountImplicitly(owner: string, options: CreateAccountImplicitlyOptions, callback: AuthCallback): void 254 255根据指定的账号所有者和可选项隐式地创建应用账号。使用callback异步回调。 256 257**系统能力:** SystemCapability.Account.AppAccount 258 259**参数:** 260 261| 参数名 | 类型 | 必填 | 说明 | 262| -------- | --------------------- | ---- | ----------------------- | 263| owner | string | 是 | 应用账号所有者的包名。 | 264| options | [CreateAccountImplicitlyOptions](#createaccountimplicitlyoptions9) | 是 | 隐式创建账号的选项。 | 265| callback | [AuthCallback](#authcallback9) | 是 | 认证器回调对象,返回创建结果。 | 266 267**错误码:** 268 269| 错误码ID | 错误信息 | 270| ------- | ------- | 271| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 272| 12300001 | System service exception. | 273| 12300002 | Invalid owner or options. | 274| 12300007 | The number of accounts reaches the upper limit. | 275| 12300010 | Account service busy. | 276| 12300113 | Authenticator service not found. | 277| 12300114 | Authenticator service exception. | 278 279**示例:** 280 281 ```ts 282 import { BusinessError } from '@kit.BasicServicesKit'; 283 import { Want, common } from '@kit.AbilityKit'; 284 285 @Entry 286 @Component 287 struct Index { 288 context = this.getUIContext().getHostContext() as common.UIAbilityContext; // UIAbilityContext 289 290 onResultCallback(code: number, result?: appAccount.AuthResult): void { 291 console.log('resultCode: ' + code); 292 console.log('result: ' + JSON.stringify(result)); 293 } 294 295 onRequestRedirectedCallback(request: Want): void { 296 let wantInfo: Want = { 297 deviceId: '', 298 bundleName: 'com.example.accountjsdemo', 299 action: 'ohos.want.action.viewData', 300 entities: ['entity.system.default'], 301 } 302 this.context.startAbility(wantInfo).then(() => { 303 console.log('startAbility successfully'); 304 }).catch((err: BusinessError) => { 305 console.log('startAbility err: ' + JSON.stringify(err)); 306 }) 307 } 308 309 aboutToAppear(): void { 310 let options: appAccount.CreateAccountImplicitlyOptions = { 311 authType: 'getSocialData', 312 requiredLabels: [ 'student' ] 313 }; 314 try { 315 appAccountManager.createAccountImplicitly('com.example.accountjsdemo', options, { 316 onResult: this.onResultCallback, 317 onRequestRedirected: this.onRequestRedirectedCallback 318 }); 319 } catch (err) { 320 console.log('createAccountImplicitly exception: ' + JSON.stringify(err)); 321 } 322 } 323 build() {} 324 } 325 ``` 326 327### removeAccount<sup>9+</sup> 328 329removeAccount(name: string, callback: AsyncCallback<void>): void 330 331删除应用账号。使用callback异步回调。 332 333**系统能力:** SystemCapability.Account.AppAccount 334 335**参数:** 336 337| 参数名 | 类型 | 必填 | 说明 | 338| -------- | ------------------------- | ---- | ---------------- | 339| name | string | 是 | 应用账号的名称。 | 340| callback | AsyncCallback<void> | 是 | 回调函数。当删除成功时,err为null,否则为错误对象。 | 341 342**错误码:** 343 344| 错误码ID | 错误信息 | 345| ------- | ------- | 346| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 347| 12300001 | System service exception. | 348| 12300002 | Invalid name. | 349| 12300003 | Account not found. | 350 351**示例:** 352 353 ```ts 354 import { BusinessError } from '@kit.BasicServicesKit'; 355 356 try { 357 appAccountManager.removeAccount('ZhaoLiu', (err: BusinessError) => { 358 if (err) { 359 console.log('removeAccount failed, error: ' + JSON.stringify(err)); 360 } else { 361 console.log('removeAccount successfully'); 362 } 363 }); 364 } catch(err) { 365 console.log('removeAccount exception: ' + JSON.stringify(err)); 366 } 367 ``` 368 369### removeAccount<sup>9+</sup> 370 371removeAccount(name: string): Promise<void> 372 373删除应用账号。使用Promise异步回调。 374 375**系统能力:** SystemCapability.Account.AppAccount 376 377**参数:** 378 379| 参数名 | 类型 | 必填 | 说明 | 380| ---- | ------ | ---- | ----------- | 381| name | string | 是 | 应用账号的名称。 | 382 383**返回值:** 384 385| 类型 | 说明 | 386| :------------------ | :-------------------- | 387| Promise<void> | 无返回结果的Promise对象。 | 388 389**错误码:** 390 391| 错误码ID | 错误信息 | 392| ------- | ------- | 393| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 394| 12300001 | System service exception. | 395| 12300002 | Invalid name. | 396| 12300003 | Account not found. | 397 398**示例:** 399 400 ```ts 401 import { BusinessError } from '@kit.BasicServicesKit'; 402 403 try { 404 appAccountManager.removeAccount('Lisi').then(() => { 405 console.log('removeAccount successfully'); 406 }).catch((err: BusinessError) => { 407 console.log('removeAccount failed, error: ' + JSON.stringify(err)); 408 }); 409 } catch (err) { 410 console.log('removeAccount exception: ' + JSON.stringify(err)); 411 } 412 ``` 413 414### setAppAccess<sup>9+</sup> 415 416setAppAccess(name: string, bundleName: string, isAccessible: boolean, callback: AsyncCallback<void>): void 417 418设置指定应用对特定账号的访问权限。使用callback异步回调。 419 420**系统能力:** SystemCapability.Account.AppAccount 421 422**参数:** 423 424| 参数名 | 类型 | 必填 | 说明 | 425| ------------ | ------------------------- | ---- | --------------------------------- | 426| name | string | 是 | 应用账号的名称。 | 427| bundleName | string | 是 | 第三方应用的包名。 | 428| isAccessible | boolean | 是 | 是否可访问。true表示允许访问,false表示禁止访问。 | 429| callback | AsyncCallback<void> | 是 | 回调函数,如果设置成功,err为null,否则为错误对象。 | 430 431**错误码:** 432 433| 错误码ID | 错误信息| 434| ------- | -------| 435| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 436| 12300001 | System service exception. | 437| 12300002 | Invalid name or bundleName. | 438| 12300003 | Account not found. | 439| 12400005 | The size of authorization list reaches the upper limit. | 440 441**示例:** 442 443 ```ts 444 import { BusinessError } from '@kit.BasicServicesKit'; 445 446 try { 447 appAccountManager.setAppAccess('ZhangSan', 'com.example.accountjsdemo', true, (err: BusinessError) => { 448 if (err) { 449 console.log('setAppAccess failed: ' + JSON.stringify(err)); 450 } else { 451 console.log('setAppAccess successfully'); 452 } 453 }); 454 } catch (err) { 455 console.log('setAppAccess exception: ' + JSON.stringify(err)); 456 } 457 ``` 458 459### setAppAccess<sup>9+</sup> 460 461setAppAccess(name: string, bundleName: string, isAccessible: boolean): Promise<void> 462 463设置指定应用对特定账号的数据访问权限。使用Promise异步回调。 464 465**系统能力:** SystemCapability.Account.AppAccount 466 467**参数:** 468 469| 参数名 | 类型 | 必填 | 说明 | 470| ---------- | ------ | ---- | --------- | 471| name | string | 是 | 应用账号的名称。 | 472| bundleName | string | 是 | 第三方应用的包名。 | 473| isAccessible | boolean | 是 | 是否可访问。true表示允许访问,false表示禁止访问。 | 474 475**返回值:** 476 477| 类型 | 说明 | 478| :------------------ | :-------------------- | 479| Promise<void> | 无返回结果的Promise对象。 | 480 481**错误码:** 482 483| 错误码ID | 错误信息| 484| ------- | -------| 485| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 486| 12300001 | System service exception. | 487| 12300002 | Invalid name or bundleName. | 488| 12300003 | Account not found. | 489| 12400005 | The size of authorization list reaches the upper limit. | 490 491**示例:** 492 493 ```ts 494 import { BusinessError } from '@kit.BasicServicesKit'; 495 496 try { 497 appAccountManager.setAppAccess('ZhangSan', 'com.example.accountjsdemo', true).then(() => { 498 console.log('setAppAccess successfully'); 499 }).catch((err: BusinessError) => { 500 console.log('setAppAccess failed: ' + JSON.stringify(err)); 501 }); 502 } catch (err) { 503 console.log('setAppAccess exception: ' + JSON.stringify(err)); 504 } 505 ``` 506 507### checkAppAccess<sup>9+</sup> 508 509checkAppAccess(name: string, bundleName: string, callback: AsyncCallback<boolean>): void 510 511检查指定应用对特定账号的数据是否可访问。使用callback异步回调。 512 513**系统能力:** SystemCapability.Account.AppAccount 514 515**参数:** 516 517| 参数名 | 类型 | 必填 | 说明 | 518| ---------- | ------------------------- | ---- | --------------------------------- | 519| name | string | 是 | 应用账号的名称。 | 520| bundleName | string | 是 | 第三方应用的包名。 | 521| callback | AsyncCallback<boolean> | 是 | 回调函数。返回true表示指定应用可访问特定账号的数据;返回false表示不可访问。 | 522 523**错误码:** 524 525| 错误码ID | 错误信息 | 526| ------- | ------- | 527| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 528| 12300001 | System service exception. | 529| 12300002 | Invalid name or bundleName. | 530| 12300003 | Account not found. | 531 532**示例:** 533 534 ```ts 535 import { BusinessError } from '@kit.BasicServicesKit'; 536 537 try { 538 appAccountManager.checkAppAccess('ZhangSan', 'com.example.accountjsdemo', 539 (err: BusinessError, isAccessible: boolean) => { 540 if (err) { 541 console.log('checkAppAccess failed, error: ' + JSON.stringify(err)); 542 } else { 543 console.log('checkAppAccess successfully'); 544 } 545 }); 546 } catch (err) { 547 console.log('checkAppAccess exception: ' + JSON.stringify(err)); 548 } 549 ``` 550 551### checkAppAccess<sup>9+</sup> 552 553checkAppAccess(name: string, bundleName: string): Promise<boolean> 554 555检查指定应用对特定账号的数据是否可访问。使用Promise异步回调。 556 557**系统能力:** SystemCapability.Account.AppAccount 558 559**参数:** 560 561| 参数名 | 类型 | 必填 | 说明 | 562| ---------- | ------ | ---- | --------- | 563| name | string | 是 | 应用账号的名称。 | 564| bundleName | string | 是 | 第三方应用的包名。 | 565 566**返回值:** 567 568| 类型 | 说明 | 569| ------------------- | --------------------- | 570| Promise<boolean> | Promise对象。返回true表示指定应用可访问特定账号的数据;返回false表示不可访问。 | 571 572**错误码:** 573 574| 错误码ID | 错误信息| 575| ------- | -------| 576| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 577| 12300001 | System service exception. | 578| 12300002 | Invalid name or bundleName. | 579| 12300003 | Account not found. | 580 581**示例:** 582 583 ```ts 584 import { BusinessError } from '@kit.BasicServicesKit'; 585 586 try { 587 appAccountManager.checkAppAccess('ZhangSan', 'com.example.accountjsdemo').then((isAccessible: boolean) => { 588 console.log('checkAppAccess successfully, isAccessible: ' + isAccessible); 589 }).catch((err: BusinessError) => { 590 console.log('checkAppAccess failed, error: ' + JSON.stringify(err)); 591 }); 592 } catch (err) { 593 console.log('checkAppAccess exception: ' + JSON.stringify(err)); 594 } 595 ``` 596 597### setDataSyncEnabled<sup>9+</sup> 598 599setDataSyncEnabled(name: string, isEnabled: boolean, callback: AsyncCallback<void>): void 600 601开启或禁止指定应用账号的数据同步功能。使用callback异步回调。 602 603**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 604 605**系统能力:** SystemCapability.Account.AppAccount 606 607**参数:** 608 609| 参数名 | 类型 | 必填 | 说明 | 610| -------- | ------------------------- | ---- | ------------------------- | 611| name | string | 是 | 应用账号的名称。 | 612| isEnabled | boolean | 是 | 是否开启数据同步。 | 613| callback | AsyncCallback<void> | 是 | 回调函数。当开启或禁止成功时,err为null,否则为错误对象。 | 614 615**错误码:** 616 617| 错误码ID | 错误信息| 618| ------- | -------| 619| 201 | Permission denied.| 620| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 621| 12300001 | System service exception. | 622| 12300002 | Invalid name. | 623| 12300003 | Account not found. | 624 625**示例:** 626 627 ```ts 628 import { BusinessError } from '@kit.BasicServicesKit'; 629 630 try { 631 appAccountManager.setDataSyncEnabled('ZhangSan', true, (err: BusinessError) => { 632 console.log('setDataSyncEnabled err: ' + JSON.stringify(err)); 633 }); 634 } catch (err) { 635 console.log('setDataSyncEnabled err: ' + JSON.stringify(err)); 636 } 637 ``` 638 639### setDataSyncEnabled<sup>9+</sup> 640 641setDataSyncEnabled(name: string, isEnabled: boolean): Promise<void> 642 643开启或禁止指定应用账号的数据同步功能。使用Promise异步回调。 644 645**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 646 647**系统能力:** SystemCapability.Account.AppAccount 648 649**参数:** 650 651| 参数名 | 类型 | 必填 | 说明 | 652| -------- | ------- | ---- | ----------- | 653| name | string | 是 | 应用账号的名称。 | 654| isEnabled | boolean | 是 | 是否开启数据同步。 | 655 656**返回值:** 657 658| 类型 | 说明 | 659| :------------------ | :-------------------- | 660| Promise<void> | 无返回结果的Promise对象。 | 661 662**错误码:** 663 664| 错误码ID | 错误信息 | 665| ------- | ------- | 666| 201 | Permission denied.| 667| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 668| 12300001 | System service exception. | 669| 12300002 | Invalid name. | 670| 12300003 | Account not found. | 671 672**示例:** 673 674 ```ts 675 import { BusinessError } from '@kit.BasicServicesKit'; 676 677 try { 678 appAccountManager .setDataSyncEnabled('ZhangSan', true).then(() => { 679 console.log('setDataSyncEnabled Success'); 680 }).catch((err: BusinessError) => { 681 console.log('setDataSyncEnabled err: ' + JSON.stringify(err)); 682 }); 683 } catch (err) { 684 console.log('setDataSyncEnabled err: ' + JSON.stringify(err)); 685 } 686 ``` 687 688### checkDataSyncEnabled<sup>9+</sup> 689 690checkDataSyncEnabled(name: string, callback: AsyncCallback<boolean>): void 691 692检查指定应用账号是否开启数据同步功能。使用callback异步回调。 693 694**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 695 696**系统能力:** SystemCapability.Account.AppAccount 697 698**参数:** 699 700| 参数名 | 类型 | 必填 | 说明 | 701| -------- | ---------------------------- | ---- | --------------------- | 702| name | string | 是 | 应用账号的名称。 | 703| callback | AsyncCallback<boolean> | 是 | 回调函数。返回true表示指定应用账号已开启数据同步功能;返回false表示未开启。 | 704 705**错误码:** 706 707| 错误码ID | 错误信息 | 708| ------- | ------- | 709| 201 | Permission denied.| 710| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 711| 12300001 | System service exception. | 712| 12300002 | Invalid name. | 713| 12300003 | Account not found. | 714 715**示例:** 716 717 ```ts 718 import { BusinessError } from '@kit.BasicServicesKit'; 719 720 try { 721 appAccountManager.checkDataSyncEnabled('ZhangSan', (err: BusinessError, isEnabled: boolean) => { 722 if (err) { 723 console.log('checkDataSyncEnabled failed, err: ' + JSON.stringify(err)); 724 } else { 725 console.log('checkDataSyncEnabled successfully, isEnabled: ' + isEnabled); 726 } 727 }); 728 } catch (err) { 729 console.log('checkDataSyncEnabled err: ' + JSON.stringify(err)); 730 } 731 ``` 732 733### checkDataSyncEnabled<sup>9+</sup> 734 735checkDataSyncEnabled(name: string): Promise<boolean> 736 737检查指定应用账号是否开启数据同步功能。使用Promise异步回调。 738 739**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 740 741**系统能力:** SystemCapability.Account.AppAccount 742 743**参数:** 744 745| 参数名 | 类型 | 必填 | 说明 | 746| ---- | ------ | ---- | ------- | 747| name | string | 是 | 应用账号的名称。 | 748 749**返回值:** 750 751| 类型 | 说明 | 752| :--------------------- | :-------------------- | 753| Promise<boolean> | Promise对象。返回true表示指定应用账号已开启数据同步功能;返回false表示未开启。 | 754 755**错误码:** 756 757| 错误码ID | 错误信息| 758| ------- | -------| 759| 201 | Permission denied.| 760| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 761| 12300001 | System service exception. | 762| 12300002 | Invalid name. | 763| 12300003 | Account not found. | 764 765**示例:** 766 767 ```ts 768 import { BusinessError } from '@kit.BasicServicesKit'; 769 770 try { 771 appAccountManager.checkDataSyncEnabled('ZhangSan').then((isEnabled: boolean) => { 772 console.log('checkDataSyncEnabled successfully, isEnabled: ' + isEnabled); 773 }).catch((err: BusinessError) => { 774 console.log('checkDataSyncEnabled failed, err: ' + JSON.stringify(err)); 775 }); 776 } catch (err) { 777 console.log('checkDataSyncEnabled err: ' + JSON.stringify(err)); 778 } 779 ``` 780 781### setCredential<sup>9+</sup> 782 783setCredential(name: string, credentialType: string, credential: string,callback: AsyncCallback<void>): void 784 785设置指定应用账号的凭据。使用callback异步回调。 786 787**系统能力:** SystemCapability.Account.AppAccount 788 789**参数:** 790 791| 参数名 | 类型 | 必填 | 说明 | 792| -------------- | ------------------------- | ---- | ------------- | 793| name | string | 是 | 应用账号的名称。 | 794| credentialType | string | 是 | 凭据类型。 | 795| credential | string | 是 | 凭据取值。 | 796| callback | AsyncCallback<void> | 是 | 回调函数。当凭据设置成功时,err为null,否则为错误对象。 | 797 798**错误码:** 799 800| 错误码ID | 错误信息| 801| ------- | -------| 802| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 803| 12300001 | System service exception. | 804| 12300002 | Invalid name, credentialType or credential. | 805| 12300003 | Account not found. | 806 807**示例:** 808 809 ```ts 810 import { BusinessError } from '@kit.BasicServicesKit'; 811 812 try { 813 appAccountManager.setCredential('ZhangSan', 'PIN_SIX', 'xxxxxx', (err: BusinessError) => { 814 if (err) { 815 console.log('setCredential failed, error: ' + JSON.stringify(err)); 816 } else { 817 console.log('setCredential successfully'); 818 } 819 }); 820 } catch (err) { 821 console.log('setCredential exception: ' + JSON.stringify(err)); 822 } 823 ``` 824 825### setCredential<sup>9+</sup> 826 827setCredential(name: string, credentialType: string, credential: string): Promise<void> 828 829设置指定应用账号的凭据。使用Promise异步回调。 830 831**系统能力:** SystemCapability.Account.AppAccount 832 833**参数:** 834 835| 参数名 | 类型 | 必填 | 说明 | 836| -------------- | ------ | ---- | ---------- | 837| name | string | 是 | 应用账号的名称。 | 838| credentialType | string | 是 | 凭据类型。 | 839| credential | string | 是 | 凭据取值。 | 840 841**返回值:** 842 843| 类型 | 说明 | 844| :------------------ | :-------------------- | 845| Promise<void> | 无返回结果的Promise对象。 | 846 847**错误码:** 848 849| 错误码ID | 错误信息| 850| ------- | -------| 851| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 852| 12300001 | System service exception. | 853| 12300002 | Invalid name, credentialType or credential. | 854| 12300003 | Account not found. | 855 856**示例:** 857 858 ```ts 859 import { BusinessError } from '@kit.BasicServicesKit'; 860 861 try { 862 appAccountManager.setCredential('ZhangSan', 'PIN_SIX', 'xxxxxx').then(() => { 863 console.log('setCredential successfully'); 864 }).catch((err: BusinessError) => { 865 console.log('setCredential failed, error: ' + JSON.stringify(err)); 866 }); 867 } catch (err) { 868 console.log('setCredential exception: ' + JSON.stringify(err)); 869 } 870 ``` 871 872### getCredential<sup>9+</sup> 873 874getCredential(name: string, credentialType: string, callback: AsyncCallback<string>): void 875 876获取指定应用账号的凭据。使用callback异步回调。 877 878**系统能力:** SystemCapability.Account.AppAccount 879 880**参数:** 881 882| 参数名 | 类型 | 必填 | 说明 | 883| -------------- | --------------------------- | ---- | -------------- | 884| name | string | 是 | 应用账号的名称。 | 885| credentialType | string | 是 | 凭据类型。 | 886| callback | AsyncCallback<string> | 是 | 回调函数。当获取凭据成功时,err为null,data为指定应用账号的凭据;否则为错误对象。 | 887 888**错误码:** 889 890| 错误码ID | 错误信息 | 891| ------- | ------- | 892| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 893| 12300001 | System service exception. | 894| 12300002 | Invalid name or credentialType. | 895| 12300003 | Account not found. | 896| 12300102 | Credential not found. | 897 898**示例:** 899 900 ```ts 901 import { BusinessError } from '@kit.BasicServicesKit'; 902 903 try { 904 appAccountManager.getCredential('ZhangSan', 'PIN_SIX', (err: BusinessError, result: string) => { 905 if (err) { 906 console.log('getCredential failed, error: ' + JSON.stringify(err)); 907 } else { 908 console.log('getCredential successfully, result: ' + result); 909 } 910 }); 911 } catch (err) { 912 console.log('getCredential err: ' + JSON.stringify(err)); 913 } 914 ``` 915 916### getCredential<sup>9+</sup> 917 918getCredential(name: string, credentialType: string): Promise<string> 919 920获取指定应用账号的凭据。使用Promise异步回调。 921 922**系统能力:** SystemCapability.Account.AppAccount 923 924**参数:** 925 926| 参数名 | 类型 | 必填 | 说明 | 927| -------------- | ------ | ---- | ---------- | 928| name | string | 是 | 应用账号的名称。 | 929| credentialType | string | 是 | 凭据类型。 | 930 931**返回值:** 932 933| 类型 | 说明 | 934| :-------------------- | :-------------------- | 935| Promise<string> | Promise对象,返回指定应用账号的凭据。 | 936 937**错误码:** 938 939| 错误码ID | 错误信息 | 940| ------- | ------- | 941| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 942| 12300001 | System service exception. | 943| 12300002 | Invalid name or credentialType. | 944| 12300003 | Account not found. | 945| 12300102 | Credential not found. | 946 947**示例:** 948 949 ```ts 950 import { BusinessError } from '@kit.BasicServicesKit'; 951 952 try { 953 appAccountManager.getCredential('ZhangSan', 'PIN_SIX').then((credential: string) => { 954 console.log('getCredential successfully, credential: ' + credential); 955 }).catch((err: BusinessError) => { 956 console.log('getCredential failed, error: ' + JSON.stringify(err)); 957 }); 958 } catch (err) { 959 console.log('getCredential exception: ' + JSON.stringify(err)); 960 } 961 ``` 962 963### setCustomData<sup>9+</sup> 964 965setCustomData(name: string, key: string, value: string, callback: AsyncCallback<void>): void 966 967设置指定应用账号的自定义数据。使用callback异步回调。 968 969**系统能力:** SystemCapability.Account.AppAccount 970 971**参数:** 972 973| 参数名 | 类型 | 必填 | 说明 | 974| -------- | ------------------------- | ---- | ----------------- | 975| name | string | 是 | 应用账号的名称。 | 976| key | string | 是 | 自定义数据的键名。 | 977| value | string | 是 | 自定义数据的取值。 | 978| callback | AsyncCallback<void> | 是 | 回调函数。当设置自定义数据成功时,err为null,否则为错误对象。 | 979 980**错误码:** 981 982| 错误码ID | 错误信息| 983| ------- | -------| 984| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 985| 12300001 | System service exception. | 986| 12300002 | Invalid name, key or value. | 987| 12300003 | Account not found. | 988| 12400003 | The number of custom data reaches the upper limit. | 989 990**示例:** 991 992 ```ts 993 import { BusinessError } from '@kit.BasicServicesKit'; 994 995 try { 996 appAccountManager.setCustomData('ZhangSan', 'age', '12', (err: BusinessError) => { 997 if (err) { 998 console.log('setCustomData failed, error: ' + JSON.stringify(err)); 999 } else { 1000 console.log('setCustomData successfully'); 1001 } 1002 }); 1003 } catch (err) { 1004 console.log('setCustomData exception: ' + JSON.stringify(err)); 1005 } 1006 ``` 1007 1008### setCustomData<sup>9+</sup> 1009 1010setCustomData(name: string, key: string, value: string): Promise<void> 1011 1012设置指定应用账号的自定义数据。使用Promise异步回调。 1013 1014**系统能力:** SystemCapability.Account.AppAccount 1015 1016**参数:** 1017 1018| 参数名 | 类型 | 必填 | 说明 | 1019| ----- | ------ | ---- | ----------------- | 1020| name | string | 是 | 应用账号的名称。 | 1021| key | string | 是 | 自定义数据的键名。 | 1022| value | string | 是 | 自定义数据的取值。 | 1023 1024**返回值:** 1025 1026| 类型 | 说明 | 1027| :------------------ | :-------------------- | 1028| Promise<void> | 无返回结果的Promise对象。 | 1029 1030**错误码:** 1031 1032| 错误码ID | 错误信息| 1033| ------- | -------| 1034| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 1035| 12300001 | System service exception. | 1036| 12300002 | Invalid name, key or value. | 1037| 12300003 | Account not found. | 1038| 12400003 | The number of custom data reaches the upper limit. | 1039 1040**示例:** 1041 1042 ```ts 1043 import { BusinessError } from '@kit.BasicServicesKit'; 1044 1045 try { 1046 appAccountManager.setCustomData('ZhangSan', 'age', '12').then(() => { 1047 console.log('setCustomData successfully'); 1048 }).catch((err: BusinessError) => { 1049 console.log('setCustomData failed, error: ' + JSON.stringify(err)); 1050 }); 1051 } catch (err) { 1052 console.log('setCustomData exception: ' + JSON.stringify(err)); 1053 } 1054 ``` 1055 1056### getCustomData<sup>9+</sup> 1057 1058getCustomData(name: string, key: string, callback: AsyncCallback<string>): void 1059 1060根据指定键名获取特定应用账号的自定义数据。使用callback异步回调。 1061 1062**系统能力:** SystemCapability.Account.AppAccount 1063 1064**参数:** 1065 1066| 参数名 | 类型 | 必填 | 说明 | 1067| -------- | --------------------------- | ----- | ------------------------ | 1068| name | string | 是 | 应用账号的名称。 | 1069| key | string | 是 | 自定义数据的键名。 | 1070| callback | AsyncCallback<string> | 是 | 回调函数。当获取成功时,err为null,data为自定义数据的取值;否则为错误对象。 | 1071 1072**错误码:** 1073 1074| 错误码ID | 错误信息| 1075| ------- | -------| 1076| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 1077| 12300001 | System service exception. | 1078| 12300002 | Invalid name or key. | 1079| 12300003 | Account not found. | 1080| 12400002 | Custom data not found. | 1081 1082**示例:** 1083 1084 ```ts 1085 import { BusinessError } from '@kit.BasicServicesKit'; 1086 1087 try { 1088 appAccountManager.getCustomData('ZhangSan', 'age', (err: BusinessError, data: string) => { 1089 if (err) { 1090 console.log('getCustomData failed, error: ' + err); 1091 } else { 1092 console.log('getCustomData successfully, data: ' + data); 1093 } 1094 }); 1095 } catch (err) { 1096 console.log('getCustomData exception: ' + JSON.stringify(err)); 1097 } 1098 ``` 1099 1100### getCustomData<sup>9+</sup> 1101 1102getCustomData(name: string, key: string): Promise<string> 1103 1104根据指定键名获取特定应用账号的自定义数据。使用Promise异步回调。 1105 1106**系统能力:** SystemCapability.Account.AppAccount 1107 1108**参数:** 1109 1110| 参数名 | 类型 | 必填 | 说明 | 1111| ---- | ------ | ---- | --------- | 1112| name | string | 是 | 应用账号的名称。 | 1113| key | string | 是 | 自定义数据的键名。 | 1114 1115**返回值:** 1116 1117| 类型 | 说明 | 1118| --------------------- | --------------------- | 1119| Promise<string> | Promise对象,返回自定义数据的取值。 | 1120 1121**错误码:** 1122 1123| 错误码ID | 错误信息| 1124| ------- | -------| 1125| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 1126| 12300001 | System service exception. | 1127| 12300002 | Invalid name or key. | 1128| 12300003 | Account not found. | 1129| 12400002 | Custom data not found. | 1130 1131**示例:** 1132 1133 ```ts 1134 import { BusinessError } from '@kit.BasicServicesKit'; 1135 1136 try { 1137 appAccountManager.getCustomData('ZhangSan', 'age').then((data: string) => { 1138 console.log('getCustomData successfully, data: ' + data); 1139 }).catch((err: BusinessError) => { 1140 console.log('getCustomData failed, error: ' + JSON.stringify(err)); 1141 }); 1142 } catch (err) { 1143 console.log('getCustomData exception: ' + JSON.stringify(err)); 1144 } 1145 ``` 1146 1147### getCustomDataSync<sup>9+</sup> 1148 1149getCustomDataSync(name: string, key: string): string; 1150 1151根据指定键名获取特定应用账号的自定义数据。使用同步方式返回结果。 1152 1153**系统能力:** SystemCapability.Account.AppAccount 1154 1155**参数:** 1156 1157| 参数名 | 类型 | 必填 | 说明 | 1158| ---- | ------ | ---- | --------- | 1159| name | string | 是 | 应用账号的名称。 | 1160| key | string | 是 | 自定义数据的键名。 | 1161 1162**返回值:** 1163 1164| 类型 | 说明 | 1165| --------------------- | --------------------- | 1166| string | 自定义数据的取值。 | 1167 1168**错误码:** 1169 1170| 错误码ID | 错误信息| 1171| ------- | -------| 1172| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 1173| 12300001 | System service exception. | 1174| 12300002 | Invalid name or key. | 1175| 12300003 | Account not found. | 1176| 12400002 | Custom data not found. | 1177 1178**示例:** 1179 1180 ```ts 1181 try { 1182 let value = appAccountManager.getCustomDataSync('ZhangSan', 'age'); 1183 console.info('getCustomDataSync successfully, vaue: ' + value); 1184 } catch (err) { 1185 console.error('getCustomDataSync failed, error: ' + JSON.stringify(err)); 1186 } 1187 ``` 1188 1189### getAllAccounts<sup>9+</sup> 1190 1191getAllAccounts(callback: AsyncCallback<Array<AppAccountInfo>>): void 1192 1193获取所有可访问的应用账号信息。使用callback异步回调。 1194 1195**系统能力:** SystemCapability.Account.AppAccount 1196 1197**参数:** 1198 1199| 参数名 | 类型 | 必填 | 说明 | 1200| -------- | ---------------------------------------- | ---- | --------- | 1201| callback | AsyncCallback<Array<[AppAccountInfo](#appaccountinfo)>> | 是 | 回调函数。当查询成功时,err为null,data为获取到的应用账号信息列表;否则为错误对象。 | 1202 1203**错误码:** 1204 1205| 错误码ID | 错误信息| 1206| ------- | -------| 1207| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 1208| 12300001 | System service exception. | 1209 1210**示例:** 1211 1212 ```ts 1213 import { BusinessError } from '@kit.BasicServicesKit'; 1214 1215 try { 1216 appAccountManager.getAllAccounts((err: BusinessError, data: appAccount.AppAccountInfo[]) => { 1217 if (err) { 1218 console.debug('getAllAccounts failed, error: ' + JSON.stringify(err)); 1219 } else { 1220 console.debug('getAllAccounts successfully'); 1221 } 1222 }); 1223 } catch (err) { 1224 console.debug('getAllAccounts exception: ' + JSON.stringify(err)); 1225 } 1226 ``` 1227 1228### getAllAccounts<sup>9+</sup> 1229 1230getAllAccounts(): Promise<Array<AppAccountInfo>> 1231 1232获取所有可访问的应用账号信息。使用Promise异步回调。 1233 1234**系统能力:** SystemCapability.Account.AppAccount 1235 1236**返回值:** 1237 1238| 类型 | 说明 | 1239| ---------------------------------------- | --------------------- | 1240| Promise<Array<[AppAccountInfo](#appaccountinfo)>> | Promise对象,返回全部应用已授权账号信息对象。 | 1241 1242**错误码:** 1243 1244| 错误码ID | 错误信息| 1245| ------- | -------| 1246| 12300001 | System service exception. | 1247 1248**示例:** 1249 1250 ```ts 1251 import { BusinessError } from '@kit.BasicServicesKit'; 1252 1253 try { 1254 appAccountManager.getAllAccounts().then((data: appAccount.AppAccountInfo[]) => { 1255 console.debug('getAllAccounts successfully'); 1256 }).catch((err: BusinessError) => { 1257 console.debug('getAllAccounts failed, error: ' + JSON.stringify(err)); 1258 }); 1259 } catch (err) { 1260 console.debug('getAllAccounts exception: ' + JSON.stringify(err)); 1261 } 1262 ``` 1263 1264### getAccountsByOwner<sup>9+</sup> 1265 1266getAccountsByOwner(owner: string, callback: AsyncCallback<Array<AppAccountInfo>>): void 1267 1268根据应用账号所有者获取调用方可访问的应用账号列表。使用callback异步回调。 1269 1270**系统能力:** SystemCapability.Account.AppAccount 1271 1272**参数:** 1273 1274| 参数名 | 类型 | 必填 | 说明 | 1275| -------- | ---------------------------------------- | ---- | --------- | 1276| owner | string | 是 | 应用账号所有者的包名。 | 1277| callback | AsyncCallback<Array<[AppAccountInfo](#appaccountinfo)>> | 是 | 回调函数。如果获取成功,err为null,data为获取到的应用账号列表;否则为错误对象。 | 1278 1279**错误码:** 1280 1281| 错误码ID | 错误信息| 1282| ------- | -------| 1283| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 1284| 12300001 | System service exception. | 1285| 12300002 | Invalid owner. | 1286 1287**示例:** 1288 1289 ```ts 1290 import { BusinessError } from '@kit.BasicServicesKit'; 1291 1292 try { 1293 appAccountManager.getAccountsByOwner('com.example.accountjsdemo2', 1294 (err: BusinessError, data: appAccount.AppAccountInfo[]) => { 1295 if (err) { 1296 console.debug('getAccountsByOwner failed, error:' + JSON.stringify(err)); 1297 } else { 1298 console.debug('getAccountsByOwner successfully, data:' + JSON.stringify(data)); 1299 } 1300 }); 1301 } catch (err) { 1302 console.debug('getAccountsByOwner exception:' + JSON.stringify(err)); 1303 } 1304 ``` 1305 1306### getAccountsByOwner<sup>9+</sup> 1307 1308getAccountsByOwner(owner: string): Promise<Array<AppAccountInfo>> 1309 1310根据应用账号所有者获取调用方可访问的应用账号列表。使用Promise异步回调。 1311 1312**系统能力:** SystemCapability.Account.AppAccount 1313 1314**参数:** 1315 1316| 参数名 | 类型 | 必填 | 说明 | 1317| ----- | ------ | ---- | ------ | 1318| owner | string | 是 | 应用账号所有者的包名。 | 1319 1320**返回值:** 1321 1322| 类型 | 说明 | 1323| ---------------------------------------- | --------------------- | 1324| Promise<Array<[AppAccountInfo](#appaccountinfo)>> | Promise对象,返回获取到的应用账号列表。 | 1325 1326**错误码:** 1327 1328| 错误码ID | 错误信息| 1329| ------- | -------| 1330| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 1331| 12300001 | System service exception. | 1332| 12300002 | Invalid owner. | 1333 1334**示例:** 1335 1336 ```ts 1337 import { BusinessError } from '@kit.BasicServicesKit'; 1338 1339 try { 1340 appAccountManager.getAccountsByOwner('com.example.accountjsdemo2').then(( 1341 data: appAccount.AppAccountInfo[]) => { 1342 console.debug('getAccountsByOwner successfully, data: ' + JSON.stringify(data)); 1343 }).catch((err: BusinessError) => { 1344 console.debug('getAccountsByOwner failed, error: ' + JSON.stringify(err)); 1345 }); 1346 } catch (err) { 1347 console.debug('getAccountsByOwner exception: ' + JSON.stringify(err)); 1348 } 1349 ``` 1350 1351### on('accountChange')<sup>9+</sup> 1352 1353on(type: 'accountChange', owners: Array<string>, callback: Callback<Array<AppAccountInfo>>): void 1354 1355订阅指定应用的账号信息变更事件。 1356 1357**系统能力:** SystemCapability.Account.AppAccount 1358 1359**参数:** 1360 1361| 参数名 | 类型 | 必填 | 说明 | 1362| -------- | ---------------------------------------- | ---- | ------------------------------ | 1363| type | 'accountChange' | 是 | 事件回调类型,支持的事件为'accountChange',当目标应用更新账号信息时,触发该事件。 | 1364| owners | Array<string> | 是 | 应用账号所有者的包名列表。 | 1365| callback | Callback<Array<[AppAccountInfo](#appaccountinfo)>> | 是 | 需要注册的回调函数,返回信息为发生变更的应用账号列表。 | 1366 1367**错误码:** 1368 1369| 错误码ID | 错误信息 | 1370| ------- | ------- | 1371| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 1372| 12300001 | System service exception. | 1373| 12300002 | Invalid type or owners. | 1374 1375**示例:** 1376 1377 ```ts 1378 function changeOnCallback(data: appAccount.AppAccountInfo[]): void { 1379 console.log('receive change data:' + JSON.stringify(data)); 1380 } 1381 try{ 1382 appAccountManager.on('accountChange', ['com.example.actsaccounttest'], changeOnCallback); 1383 } catch(err) { 1384 console.error('on accountChange failed, error:' + JSON.stringify(err)); 1385 } 1386 ``` 1387 1388### off('accountChange')<sup>9+</sup> 1389 1390off(type: 'accountChange', callback?: Callback<Array<AppAccountInfo>>): void 1391 1392取消订阅账号信息变更事件。 1393 1394**系统能力:** SystemCapability.Account.AppAccount 1395 1396**参数:** 1397 1398| 参数名 | 类型 | 必填 | 说明 | 1399| -------- | -------------------------------- | ---- | ------------ | 1400| type | 'accountChange' | 是 | 事件回调类型,支持的事件为'accountChange',当账号所有者更新账号信息时,触发该事件。 | 1401| callback | Callback<Array<[AppAccountInfo](#appaccountinfo)>> | 否 | 需要注销的回调函数,默认为空,表示取消该类型事件所有的回调。 | 1402 1403**错误码:** 1404 1405| 错误码ID | 错误信息| 1406| ------- | -------| 1407| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.| 1408| 12300001 | System service exception. | 1409| 12300002 | Invalid type. | 1410 1411**示例:** 1412 1413 ```ts 1414 function changeOnCallback(data: appAccount.AppAccountInfo[]): void { 1415 console.log('receive change data:' + JSON.stringify(data)); 1416 } 1417 try{ 1418 appAccountManager.on('accountChange', ['com.example.actsaccounttest'], changeOnCallback); 1419 } catch(err) { 1420 console.error('on accountChange failed, error:' + JSON.stringify(err)); 1421 } 1422 try{ 1423 appAccountManager.off('accountChange', changeOnCallback); 1424 } 1425 catch(err){ 1426 console.error('off accountChange failed, error:' + JSON.stringify(err)); 1427 } 1428 ``` 1429 1430### auth<sup>9+</sup> 1431 1432auth(name: string, owner: string, authType: string, callback: AuthCallback): void 1433 1434对应用账号进行鉴权以获取授权令牌。使用callback异步回调。 1435 1436**系统能力:** SystemCapability.Account.AppAccount 1437 1438**参数:** 1439 1440| 参数名 | 类型 | 必填 | 说明 | 1441| -------- | --------------------- | ---- | --------------- | 1442| name | string | 是 | 应用账号的名称。 | 1443| owner | string | 是 | 应用账号所有者的包名。 | 1444| authType | string | 是 | 鉴权类型。 | 1445| callback | [AuthCallback](#authcallback9) | 是 | 回调对象,返回鉴权结果。 | 1446 1447**错误码:** 1448 1449| 错误码ID | 错误信息| 1450| ------- | -------| 1451| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1452| 12300001 | System service exception. | 1453| 12300002 | Invalid name, owner or authType. | 1454| 12300003 | Account not found. | 1455| 12300010 | Account service busy. | 1456| 12300113 | Authenticator service not found. | 1457| 12300114 | Authenticator service exception. | 1458 1459**示例:** 1460 1461 ```ts 1462 import { BusinessError } from '@kit.BasicServicesKit'; 1463 import { Want, common } from '@kit.AbilityKit'; 1464 1465 @Entry 1466 @Component 1467 struct Index { 1468 context = this.getUIContext().getHostContext() as common.UIAbilityContext; // UIAbilityContext 1469 1470 onResultCallback(code: number, authResult?: appAccount.AuthResult): void { 1471 console.log('resultCode: ' + code); 1472 console.log('authResult: ' + JSON.stringify(authResult)); 1473 } 1474 1475 onRequestRedirectedCallback(request: Want): void { 1476 let wantInfo: Want = { 1477 deviceId: '', 1478 bundleName: 'com.example.accountjsdemo', 1479 action: 'ohos.want.action.viewData', 1480 entities: ['entity.system.default'], 1481 } 1482 this.context.startAbility(wantInfo).then(() => { 1483 console.log('startAbility successfully'); 1484 }).catch((err: BusinessError) => { 1485 console.log('startAbility err: ' + JSON.stringify(err)); 1486 }) 1487 } 1488 1489 aboutToAppear(): void { 1490 try { 1491 appAccountManager.auth('LiSi', 'com.example.accountjsdemo', 'getSocialData', { 1492 onResult: this.onResultCallback, 1493 onRequestRedirected: this.onRequestRedirectedCallback 1494 }); 1495 } catch (err) { 1496 console.log('auth exception: ' + JSON.stringify(err)); 1497 } 1498 } 1499 build() {} 1500 } 1501 ``` 1502 1503### auth<sup>9+</sup> 1504 1505auth(name: string, owner: string, authType: string, options: Record<string, Object>, callback: AuthCallback): void 1506 1507对应用账号进行鉴权以获取授权令牌。使用callback异步回调。 1508 1509**系统能力:** SystemCapability.Account.AppAccount 1510 1511**参数:** 1512 1513| 参数名 | 类型 | 必填 | 说明 | 1514| -------- | --------------------- | ---- | --------------- | 1515| name | string | 是 | 应用账号的名称。 | 1516| owner | string | 是 | 应用账号所有者的包名。 | 1517| authType | string | 是 | 鉴权类型。 | 1518| options | Record<string, Object> | 是 | 鉴权所需的可选项。 | 1519| callback | [AuthCallback](#authcallback9) | 是 | 回调对象,返回鉴权结果。 | 1520 1521**错误码:** 1522 1523| 错误码ID | 错误信息| 1524| ------- | -------| 1525| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1526| 12300001 | System service exception. | 1527| 12300002 | Invalid name, owner, authType or options. | 1528| 12300003 | Account not found. | 1529| 12300010 | Account service busy. | 1530| 12300113 | Authenticator service not found. | 1531| 12300114 | Authenticator service exception. | 1532 1533**示例:** 1534 1535 ```ts 1536 import { BusinessError } from '@kit.BasicServicesKit'; 1537 import { Want, common } from '@kit.AbilityKit'; 1538 1539 @Entry 1540 @Component 1541 struct Index { 1542 context = this.getUIContext().getHostContext() as common.UIAbilityContext; // UIAbilityContext 1543 1544 onResultCallback(code: number, authResult?: appAccount.AuthResult): void { 1545 console.log('resultCode: ' + code); 1546 console.log('authResult: ' + JSON.stringify(authResult)); 1547 } 1548 1549 onRequestRedirectedCallback(request: Want): void { 1550 let wantInfo: Want = { 1551 deviceId: '', 1552 bundleName: 'com.example.accountjsdemo', 1553 action: 'ohos.want.action.viewData', 1554 entities: ['entity.system.default'], 1555 } 1556 this.context.startAbility(wantInfo).then(() => { 1557 console.log('startAbility successfully'); 1558 }).catch((err: BusinessError) => { 1559 console.log('startAbility err: ' + JSON.stringify(err)); 1560 }) 1561 } 1562 1563 aboutToAppear(): void { 1564 let options: Record<string, Object> = { 1565 'password': 'xxxx', 1566 }; 1567 try { 1568 appAccountManager.auth('LiSi', 'com.example.accountjsdemo', 'getSocialData', options, { 1569 onResult: this.onResultCallback, 1570 onRequestRedirected: this.onRequestRedirectedCallback 1571 }); 1572 } catch (err) { 1573 console.log('auth exception: ' + JSON.stringify(err)); 1574 } 1575 } 1576 build() {} 1577 } 1578 ``` 1579 1580### getAuthToken<sup>9+</sup> 1581 1582getAuthToken(name: string, owner: string, authType: string, callback: AsyncCallback<string>): void 1583 1584获取指定应用账号的特定鉴权类型的授权令牌。使用callback异步回调。 1585 1586**系统能力:** SystemCapability.Account.AppAccount 1587 1588**参数:** 1589 1590| 参数名 | 类型 | 必填 | 说明 | 1591| -------- | --------------------------- | ---- | ----------- | 1592| name | string | 是 | 应用账号的名称。 | 1593| owner | string | 是 | 应用账号所有者的包名。 | 1594| authType | string | 是 | 鉴权类型。 | 1595| callback | AsyncCallback<string> | 是 | 回调函数。当获取成功时,err为null,data为授权令牌值;否则为错误对象。 | 1596 1597**错误码:** 1598 1599| 错误码ID | 错误信息| 1600| ------- | -------| 1601| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1602| 12300001 | System service exception. | 1603| 12300002 | Invalid name, owner or authType. | 1604| 12300003 | Account not found. | 1605| 12300107 | AuthType not found. | 1606 1607**示例:** 1608 1609 ```ts 1610 import { BusinessError } from '@kit.BasicServicesKit'; 1611 1612 try { 1613 appAccountManager.getAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData', 1614 (err: BusinessError, token: string) => { 1615 if (err) { 1616 console.log('getAuthToken failed, error: ' + JSON.stringify(err)); 1617 } else { 1618 console.log('getAuthToken successfully, token: ' + token); 1619 } 1620 }); 1621 } catch (err) { 1622 console.log('getAuthToken exception: ' + JSON.stringify(err)); 1623 } 1624 ``` 1625 1626### getAuthToken<sup>9+</sup> 1627 1628getAuthToken(name: string, owner: string, authType: string): Promise<string> 1629 1630获取指定应用账号的特定鉴权类型的授权令牌。使用Promise异步回调。 1631 1632**系统能力:** SystemCapability.Account.AppAccount 1633 1634**参数:** 1635 1636| 参数名 | 类型 | 必填 | 说明 | 1637| -------- | ------ | ---- | ----------- | 1638| name | string | 是 | 应用账号的名称。 | 1639| owner | string | 是 | 应用账号所有者的包名。 | 1640| authType | string | 是 | 鉴权类型。 | 1641 1642**返回值:** 1643 1644| 类型 | 说明 | 1645| --------------------- | --------------------- | 1646| Promise<string> | Promise对象,返回授权令牌。 | 1647 1648**错误码:** 1649 1650| 错误码ID | 错误信息 | 1651| ------- | ------- | 1652| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1653| 12300001 | System service exception. | 1654| 12300002 | Invalid name, owner or authType. | 1655| 12300003 | Account not found. | 1656| 12300107 | AuthType not found. | 1657 1658**示例:** 1659 1660 ```ts 1661 import { BusinessError } from '@kit.BasicServicesKit'; 1662 1663 try { 1664 appAccountManager.getAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData').then((token: string) => { 1665 console.log('getAuthToken successfully, token: ' + token); 1666 }).catch((err: BusinessError) => { 1667 console.log('getAuthToken failed, error: ' + JSON.stringify(err)); 1668 }); 1669 } catch (err) { 1670 console.log('getAuthToken exception: ' + JSON.stringify(err)); 1671 } 1672 ``` 1673 1674### setAuthToken<sup>9+</sup> 1675 1676setAuthToken(name: string, authType: string, token: string, callback: AsyncCallback<void>): void 1677 1678为指定应用账号设置特定鉴权类型的授权令牌。使用callback异步回调。 1679 1680**系统能力:** SystemCapability.Account.AppAccount 1681 1682**参数:** 1683 1684| 参数名 | 类型 | 必填 | 说明 | 1685| -------- | ------------------------- | ---- | -------- | 1686| name | string | 是 | 应用账号的名称。 | 1687| authType | string | 是 | 鉴权类型。 | 1688| token | string | 是 | 授权令牌。 | 1689| callback | AsyncCallback<void> | 是 | 回调函数。当设置成功时,err为null;否则为错误对象。 | 1690 1691**错误码:** 1692 1693| 错误码ID | 错误信息| 1694| ------- | -------| 1695| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1696| 12300001 | System service exception. | 1697| 12300002 | Invalid name, authType or token. | 1698| 12300003 | Account not found. | 1699| 12400004 | The number of tokens reaches the upper limit. | 1700 1701**示例:** 1702 1703 ```ts 1704 import { BusinessError } from '@kit.BasicServicesKit'; 1705 1706 try { 1707 appAccountManager.setAuthToken('LiSi', 'getSocialData', 'xxxx', (err: BusinessError) => { 1708 if (err) { 1709 console.log('setAuthToken failed, error: ' + JSON.stringify(err)); 1710 } else { 1711 console.log('setAuthToken successfully'); 1712 } 1713 }); 1714 } catch (err) { 1715 console.log('setAuthToken exception: ' + JSON.stringify(err)); 1716 } 1717 ``` 1718 1719### setAuthToken<sup>9+</sup> 1720 1721setAuthToken(name: string, authType: string, token: string): Promise<void> 1722 1723为指定应用账号设置特定鉴权类型的授权令牌。使用Promise异步回调。 1724 1725**系统能力:** SystemCapability.Account.AppAccount 1726 1727**参数:** 1728 1729| 参数名 | 类型 | 必填 | 说明 | 1730| -------- | ------ | ---- | -------- | 1731| name | string | 是 | 应用账号的名称。 | 1732| authType | string | 是 | 鉴权类型。 | 1733| token | string | 是 | 授权令牌。 | 1734 1735**返回值:** 1736 1737| 类型 | 说明 | 1738| ------------------- | --------------------- | 1739| Promise<void> | 无返回结果的Promise对象。 | 1740 1741**错误码:** 1742 1743| 错误码ID | 错误信息| 1744| ------- | -------| 1745| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1746| 12300001 | System service exception. | 1747| 12300002 | Invalid name, authType or token. | 1748| 12300003 | Account not found. | 1749| 12400004 | The number of tokens reaches the upper limit. | 1750 1751**示例:** 1752 1753 ```ts 1754 import { BusinessError } from '@kit.BasicServicesKit'; 1755 1756 try { 1757 appAccountManager.setAuthToken('LiSi', 'getSocialData', 'xxxx').then(() => { 1758 console.log('setAuthToken successfully'); 1759 }).catch((err: BusinessError) => { 1760 console.log('setAuthToken failed, error: ' + JSON.stringify(err)); 1761 }); 1762 } catch (err) { 1763 console.log('setAuthToken exception: ' + JSON.stringify(err)); 1764 } 1765 ``` 1766 1767### deleteAuthToken<sup>9+</sup> 1768 1769deleteAuthToken(name: string, owner: string, authType: string, token: string, callback: AsyncCallback<void>): void 1770 1771删除指定应用账号的特定鉴权类型的授权令牌。使用callback异步回调。 1772 1773**系统能力:** SystemCapability.Account.AppAccount 1774 1775**参数:** 1776 1777| 参数名 | 类型 | 必填 | 说明 | 1778| -------- | ------------------------- | ---- | ------------ | 1779| name | string | 是 | 应用账号的名称。 | 1780| owner | string | 是 | 应用账号所有者的包名。 | 1781| authType | string | 是 | 鉴权类型。 | 1782| token | string | 是 | 授权令牌。如果授权令牌不存在,则不执行任何操作。 | 1783| callback | AsyncCallback<void> | 是 | 回调函数。当删除成功时,err为null;否则为错误对象。 | 1784 1785**错误码:** 1786 1787| 错误码ID | 错误信息 | 1788| ------- | ------- | 1789| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1790| 12300001 | System service exception. | 1791| 12300002 | Invalid name, owner, authType or token. | 1792| 12300003 | Account not found. | 1793| 12300107 | AuthType not found. | 1794 1795**示例:** 1796 1797 ```ts 1798 import { BusinessError } from '@kit.BasicServicesKit'; 1799 1800 try { 1801 appAccountManager.deleteAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData', 'xxxxx', 1802 (err: BusinessError) => { 1803 if (err) { 1804 console.log('deleteAuthToken failed, error: ' + JSON.stringify(err)); 1805 } else { 1806 console.log('deleteAuthToken successfully'); 1807 } 1808 }); 1809 } catch (err) { 1810 console.log('deleteAuthToken exception: ' + JSON.stringify(err)); 1811 } 1812 ``` 1813 1814### deleteAuthToken<sup>9+</sup> 1815 1816deleteAuthToken(name: string, owner: string, authType: string, token: string): Promise<void> 1817 1818删除指定应用账号的特定鉴权类型的授权令牌。使用Promise异步回调。 1819 1820**系统能力:** SystemCapability.Account.AppAccount 1821 1822**参数:** 1823 1824| 参数名 | 类型 | 必填 | 说明 | 1825| -------- | ------ | ---- | ------------ | 1826| name | string | 是 | 应用账号的名称。 | 1827| owner | string | 是 | 应用账号所有者的包名。 | 1828| authType | string | 是 | 鉴权类型。 | 1829| token | string | 是 | 授权令牌。如果授权令牌不存在,则不执行任何操作。 | 1830 1831**返回值:** 1832 1833| 类型 | 说明 | 1834| ------------------- | --------------------- | 1835| Promise<void> | 无返回结果的Promise对象。 | 1836 1837**错误码:** 1838 1839| 错误码ID | 错误信息 | 1840| ------- | ------- | 1841| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1842| 12300001 | System service exception. | 1843| 12300002 | Invalid name, owner, authType or token. | 1844| 12300003 | Account not found. | 1845| 12300107 | AuthType not found. | 1846 1847**示例:** 1848 1849 ```ts 1850 import { BusinessError } from '@kit.BasicServicesKit'; 1851 1852 try { 1853 appAccountManager.deleteAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData', 'xxxxx').then(() => { 1854 console.log('deleteAuthToken successfully'); 1855 }).catch((err: BusinessError) => { 1856 console.log('deleteAuthToken failed, error: ' + JSON.stringify(err)); 1857 }); 1858 } catch (err) { 1859 console.log('deleteAuthToken exception: ' + JSON.stringify(err)); 1860 } 1861 ``` 1862 1863### setAuthTokenVisibility<sup>9+</sup> 1864 1865setAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean, callback: AsyncCallback<void>): void 1866 1867设置指定账号的特定鉴权类型的授权令牌对指定应用的可见性。使用callback异步回调。 1868 1869**系统能力:** SystemCapability.Account.AppAccount 1870 1871**参数:** 1872 1873| 参数名 | 类型 | 必填 | 说明 | 1874| ---------- | ------------------------- | ---- | ------------------------- | 1875| name | string | 是 | 应用账号的名称。 | 1876| authType | string | 是 | 鉴权类型。 | 1877| bundleName | string | 是 | 被设置可见性的应用包名。 | 1878| isVisible | boolean | 是 | 是否可见。true表示可见,false表示不可见。 | 1879| callback | AsyncCallback<void> | 是 | 回调函数。当设置成功时,err为null;否则为错误对象。| 1880 1881**错误码:** 1882 1883| 错误码ID | 错误信息| 1884| ------- | -------| 1885| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1886| 12300001 | System service exception. | 1887| 12300002 | Invalid name, authType or bundleName. | 1888| 12300003 | Account not found. | 1889| 12300107 | AuthType not found. | 1890| 12400005 | The size of authorization list reaches the upper limit. | 1891 1892**示例:** 1893 1894 ```ts 1895 import { BusinessError } from '@kit.BasicServicesKit'; 1896 1897 try { 1898 appAccountManager.setAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo', true, 1899 (err: BusinessError) => { 1900 if (err) { 1901 console.log('setAuthTokenVisibility failed, error: ' + JSON.stringify(err)); 1902 } else { 1903 console.log('setAuthTokenVisibility successfully'); 1904 } 1905 }); 1906 } catch (err) { 1907 console.log('setAuthTokenVisibility exception: ' + JSON.stringify(err)); 1908 } 1909 ``` 1910 1911### setAuthTokenVisibility<sup>9+</sup> 1912 1913setAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean): Promise<void> 1914 1915设置指定账号的特定鉴权类型的授权令牌对指定应用的可见性。使用Promise异步回调。 1916 1917**系统能力:** SystemCapability.Account.AppAccount 1918 1919**参数:** 1920 1921| 参数名 | 类型 | 必填 | 说明 | 1922| ---------- | ------------------------- | ---- | ------------------------- | 1923| name | string | 是 | 应用账号的名称。 | 1924| authType | string | 是 | 鉴权类型。 | 1925| bundleName | string | 是 | 被设置可见性的应用包名。 | 1926| isVisible | boolean | 是 | 是否可见。true表示可见,false表示不可见。 | 1927 1928**返回值:** 1929 1930| 类型 | 说明 | 1931| ------------------- | --------------------- | 1932| Promise<void> | 无返回结果的Promise对象。 | 1933 1934**错误码:** 1935 1936| 错误码ID | 错误信息| 1937| ------- | -------| 1938| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1939| 12300001 | System service exception. | 1940| 12300002 | Invalid name, authType or bundleName. | 1941| 12300003 | Account not found. | 1942| 12300107 | AuthType not found. | 1943| 12400005 | The size of authorization list reaches the upper limit. | 1944 1945**示例:** 1946 1947 ```ts 1948 import { BusinessError } from '@kit.BasicServicesKit'; 1949 1950 try { 1951 appAccountManager.setAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo', true).then(() => { 1952 console.log('setAuthTokenVisibility successfully'); 1953 }).catch((err: BusinessError) => { 1954 console.log('setAuthTokenVisibility failed, error: ' + JSON.stringify(err)); 1955 }); 1956 } catch (err) { 1957 console.log('setAuthTokenVisibility exception: ' + JSON.stringify(err)); 1958 } 1959 ``` 1960 1961### checkAuthTokenVisibility<sup>9+</sup> 1962 1963checkAuthTokenVisibility(name: string, authType: string, bundleName: string, callback: AsyncCallback<boolean>): void 1964 1965检查指定应用账号的特定鉴权类型的授权令牌对指定应用的可见性。使用callback异步回调。 1966 1967**系统能力:** SystemCapability.Account.AppAccount 1968 1969**参数:** 1970 1971| 参数名 | 类型 | 必填 | 说明 | 1972| ---------- | ---------------------------- | ---- | ----------- | 1973| name | string | 是 | 应用账号的名称。 | 1974| authType | string | 是 | 鉴权类型。 | 1975| bundleName | string | 是 | 检查可见性的应用包名。 | 1976| callback | AsyncCallback<boolean> | 是 | 回调函数。当检查成功时,err为null,data为true表示可见,data为false表示不可见;否则为错误对象。 | 1977 1978**错误码:** 1979 1980| 错误码ID | 错误信息| 1981| ------- | -------| 1982| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1983| 12300001 | System service exception. | 1984| 12300002 | Invalid name, authType or bundleName. | 1985| 12300003 | Account not found. | 1986| 12300107 | AuthType not found. | 1987 1988**示例:** 1989 1990 ```ts 1991 import { BusinessError } from '@kit.BasicServicesKit'; 1992 1993 try { 1994 appAccountManager.checkAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo', 1995 (err: BusinessError, isVisible: boolean) => { 1996 if (err) { 1997 console.log('checkAuthTokenVisibility failed, error: ' + JSON.stringify(err)); 1998 } else { 1999 console.log('checkAuthTokenVisibility successfully, isVisible: ' + isVisible); 2000 } 2001 }); 2002 } catch (err) { 2003 console.log('checkAuthTokenVisibility exception: ' + JSON.stringify(err)); 2004 } 2005 ``` 2006 2007### checkAuthTokenVisibility<sup>9+</sup> 2008 2009checkAuthTokenVisibility(name: string, authType: string, bundleName: string): Promise<boolean> 2010 2011检查指定应用账号的特定鉴权类型的授权令牌对指定应用的可见性。使用Promise异步回调。 2012 2013**系统能力:** SystemCapability.Account.AppAccount 2014 2015**参数:** 2016 2017| 参数名 | 类型 | 必填 | 说明 | 2018| ---------- | ------ | ---- | ------------- | 2019| name | string | 是 | 应用账号的名称。 | 2020| authType | string | 是 | 鉴权类型。 | 2021| bundleName | string | 是 | 用于检查可见性的应用包名。 | 2022 2023**返回值:** 2024 2025| 类型 | 说明 | 2026| ---------------------- | --------------------- | 2027| Promise<boolean> | Promise对象。返回true表示授权令牌对指定应用的可见,返回false表示不可见。 | 2028 2029**错误码:** 2030 2031| 错误码ID | 错误信息| 2032| ------- | -------| 2033| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2034| 12300001 | System service exception. | 2035| 12300002 | Invalid name, authType or bundleName. | 2036| 12300003 | Account not found. | 2037| 12300107 | AuthType not found. | 2038 2039**示例:** 2040 2041 ```ts 2042 import { BusinessError } from '@kit.BasicServicesKit'; 2043 2044 try { 2045 appAccountManager.checkAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo').then(( 2046 isVisible: boolean) => { 2047 console.log('checkAuthTokenVisibility successfully, isVisible: ' + isVisible); 2048 }).catch((err: BusinessError) => { 2049 console.log('checkAuthTokenVisibility failed, error: ' + JSON.stringify(err)); 2050 }); 2051 } catch (err) { 2052 console.log('checkAuthTokenVisibility exception: ' + JSON.stringify(err)); 2053 } 2054 ``` 2055 2056### getAllAuthTokens<sup>9+</sup> 2057 2058getAllAuthTokens(name: string, owner: string, callback: AsyncCallback<Array<AuthTokenInfo>>): void 2059 2060获取指定账号对调用方可见的所有授权令牌。使用callback异步回调。 2061 2062**系统能力:** SystemCapability.Account.AppAccount 2063 2064**参数:** 2065 2066| 参数名 | 类型 | 必填 | 说明 | 2067| -------- | ---------------------------------------- | ---- | ----------- | 2068| name | string | 是 | 应用账号的名称。 | 2069| owner | string | 是 | 应用账号所有者的包名。 | 2070| callback | AsyncCallback<Array<[AuthTokenInfo](#authtokeninfo9)>> | 是 | 回调函数。当获取成功时,err为null,data为授权令牌数组;否则为错误对象。 | 2071 2072**错误码:** 2073 2074| 错误码ID | 错误信息| 2075| ------- | -------| 2076| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2077| 12300001 | System service exception. | 2078| 12300002 | Invalid name or owner. | 2079| 12300003 | Account not found. | 2080 2081**示例:** 2082 2083 ```ts 2084 import { BusinessError } from '@kit.BasicServicesKit'; 2085 2086 try { 2087 appAccountManager.getAllAuthTokens('LiSi', 'com.example.accountjsdemo', 2088 (err: BusinessError, tokenArr: appAccount.AuthTokenInfo[]) => { 2089 if (err) { 2090 console.log('getAllAuthTokens failed, error: ' + JSON.stringify(err)); 2091 } else { 2092 console.log('getAllAuthTokens successfully, tokenArr: ' + tokenArr); 2093 } 2094 }); 2095 } catch (err) { 2096 console.log('getAllAuthTokens exception: ' + JSON.stringify(err)); 2097 } 2098 ``` 2099 2100### getAllAuthTokens<sup>9+</sup> 2101 2102getAllAuthTokens(name: string, owner: string): Promise<Array<AuthTokenInfo>> 2103 2104获取指定账号对调用方可见的所有授权令牌。使用Promise异步回调。 2105 2106**系统能力:** SystemCapability.Account.AppAccount 2107 2108**参数:** 2109 2110| 参数名 | 类型 | 必填 | 说明 | 2111| ----- | ------ | ---- | ----------- | 2112| name | string | 是 | 应用账号的名称。 | 2113| owner | string | 是 | 应用账号所有者的包名。 | 2114 2115**返回值:** 2116 2117| 类型 | 说明 | 2118| ---------------------------------------- | --------------------- | 2119| Promise<Array<[AuthTokenInfo](#authtokeninfo9)>> | Promise对象,返回授权令牌数组。 | 2120 2121**错误码:** 2122 2123| 错误码ID | 错误信息| 2124| ------- | -------| 2125| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2126| 12300001 | System service exception. | 2127| 12300002 | Invalid name or owner. | 2128| 12300003 | Account not found. | 2129 2130**示例:** 2131 2132 ```ts 2133 import { BusinessError } from '@kit.BasicServicesKit'; 2134 2135 try { 2136 appAccountManager.getAllAuthTokens('LiSi', 'com.example.accountjsdemo').then(( 2137 tokenArr: appAccount.AuthTokenInfo[]) => { 2138 console.log('getAllAuthTokens successfully, tokenArr: ' + JSON.stringify(tokenArr)); 2139 }).catch((err: BusinessError) => { 2140 console.log('getAllAuthTokens failed, error: ' + JSON.stringify(err)); 2141 }); 2142 } catch (err) { 2143 console.log('getAllAuthTokens exception: ' + JSON.stringify(err)); 2144 } 2145 ``` 2146 2147### getAuthList<sup>9+</sup> 2148 2149getAuthList(name: string, authType: string, callback: AsyncCallback<Array<string>>): void 2150 2151获取指定应用账号的特定鉴权类型的授权列表,即被授权的包名数组(令牌的授权列表通过[setAuthTokenVisibility](#setauthtokenvisibility9)来设置)。使用callback异步回调。 2152 2153**系统能力:** SystemCapability.Account.AppAccount 2154 2155**参数:** 2156 2157| 参数名 | 类型 | 必填 | 说明 | 2158| -------- | ---------------------------------------- | ---- | ----------------------- | 2159| name | string | 是 | 应用账号的名称。 | 2160| authType | string | 是 | 鉴权类型。 | 2161| callback | AsyncCallback<Array<string>> | 是 | 回调函数。当获取成功时,err为null,data为被授权的包名数组;否则为错误对象。 | 2162 2163**错误码:** 2164 2165| 错误码ID | 错误信息| 2166| ------- | -------| 2167| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2168| 12300001 | System service exception. | 2169| 12300002 | Invalid name or authType. | 2170| 12300003 | Account not found. | 2171| 12300107 | AuthType not found. | 2172 2173**示例:** 2174 2175 ```ts 2176 import { BusinessError } from '@kit.BasicServicesKit'; 2177 2178 try { 2179 appAccountManager.getAuthList('LiSi', 'getSocialData', (err: BusinessError, authList: string[]) => { 2180 if (err) { 2181 console.log('getAuthList failed, error: ' + JSON.stringify(err)); 2182 } else { 2183 console.log('getAuthList successfully, authList: ' + authList); 2184 } 2185 }); 2186 } catch (err) { 2187 console.log('getAuthList exception: ' + JSON.stringify(err)); 2188 } 2189 ``` 2190 2191### getAuthList<sup>9+</sup> 2192 2193getAuthList(name: string, authType: string): Promise<Array<string>> 2194 2195获取指定应用账号的特定鉴权类型的授权列表,即被授权的包名数组(令牌的授权列表通过[setAuthTokenVisibility](#setauthtokenvisibility9)来设置)。使用Promise异步回调。 2196 2197**系统能力:** SystemCapability.Account.AppAccount 2198 2199**参数:** 2200 2201| 参数名 | 类型 | 必填 | 说明 | 2202| -------- | ------ | ---- | ------------------------------ | 2203| name | string | 是 | 应用账号的名称。 | 2204| authType | string | 是 | 鉴权类型。 | 2205 2206**返回值:** 2207 2208| 类型 | 说明 | 2209| ---------------------------------- | --------------------- | 2210| Promise<Array<string>> | Promise对象,返回被授权的包名数组。 | 2211 2212**错误码:** 2213 2214| 错误码ID | 错误信息| 2215| ------- | -------| 2216| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2217| 12300001 | System service exception. | 2218| 12300002 | Invalid name or authType. | 2219| 12300003 | Account not found. | 2220| 12300107 | AuthType not found. | 2221 2222**示例:** 2223 2224 ```ts 2225 import { BusinessError } from '@kit.BasicServicesKit'; 2226 2227 try { 2228 appAccountManager.getAuthList('LiSi', 'getSocialData').then((authList: string[]) => { 2229 console.log('getAuthList successfully, authList: ' + authList); 2230 }).catch((err: BusinessError) => { 2231 console.log('getAuthList failed, error: ' + JSON.stringify(err)); 2232 }); 2233 } catch (err) { 2234 console.log('getAuthList exception: ' + JSON.stringify(err)); 2235 } 2236 ``` 2237 2238### getAuthCallback<sup>9+</sup> 2239 2240getAuthCallback(sessionId: string, callback: AsyncCallback<AuthCallback>): void 2241 2242获取鉴权会话的认证器回调对象。使用callback异步回调。 2243 2244**系统能力:** SystemCapability.Account.AppAccount 2245 2246**参数:** 2247 2248| 参数名 | 类型 | 必填 | 说明 | 2249| --------- | ---------------------------------------- | ---- | -------- | 2250| sessionId | string | 是 | 鉴权会话的标识。 | 2251| callback | AsyncCallback<[AuthCallback](#authcallback9)> | 是 | 回调函数。当获取成功时,err为null,data为鉴权会话的认证器回调对象;否则为错误对象。 | 2252 2253**错误码:** 2254 2255| 错误码ID | 错误信息 | 2256| ------- | ------- | 2257| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2258| 12300001 | System service exception. | 2259| 12300002 | Invalid sessionId. | 2260| 12300108 | Session not found. | 2261 2262**示例:** 2263 2264 ```ts 2265 import { BusinessError } from '@kit.BasicServicesKit'; 2266 import { Want, UIAbility, AbilityConstant } from '@kit.AbilityKit'; 2267 2268 export default class EntryAbility extends UIAbility { 2269 onCreate(want: Want, param: AbilityConstant.LaunchParam) { // ability 生命周期函数 2270 let sessionId: string = want.parameters![appAccount.Constants.KEY_SESSION_ID] as string; 2271 try { 2272 appAccountManager.getAuthCallback(sessionId, (err: BusinessError, callback: appAccount.AuthCallback) => { 2273 if (err != null) { 2274 console.log('getAuthCallback err: ' + JSON.stringify(err)); 2275 return; 2276 } 2277 let result: appAccount.AuthResult = { 2278 account: { 2279 name: 'Lisi', 2280 owner: 'com.example.accountjsdemo', 2281 }, 2282 tokenInfo: { 2283 token: 'xxxxxx', 2284 authType: 'getSocialData' 2285 } 2286 }; 2287 callback.onResult(0, result); 2288 }); 2289 } catch (err) { 2290 console.log('getAuthCallback exception: ' + JSON.stringify(err)); 2291 } 2292 } 2293 } 2294 ``` 2295 2296### getAuthCallback<sup>9+</sup> 2297 2298getAuthCallback(sessionId: string): Promise<AuthCallback> 2299 2300获取鉴权会话的认证器回调对象。使用Promise异步回调。 2301 2302**系统能力:** SystemCapability.Account.AppAccount 2303 2304**参数:** 2305 2306| 参数名 | 类型 | 必填 | 说明 | 2307| --------- | ------ | ---- | -------- | 2308| sessionId | string | 是 | 鉴权会话的标识。 | 2309 2310**返回值:** 2311 2312| 类型 | 说明 | 2313| ------------------------------------ | --------------------- | 2314| Promise<[AuthCallback](#authcallback9)> | Promise对象,返回鉴权会话的认证器回调对象。 | 2315 2316**错误码:** 2317 2318| 错误码ID | 错误信息 | 2319| ------- | ------- | 2320| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2321| 12300001 | System service exception. | 2322| 12300002 | Invalid sessionId. | 2323| 12300108 | Session not found. | 2324 2325**示例:** 2326 2327 ```ts 2328 import { BusinessError } from '@kit.BasicServicesKit'; 2329 import { Want, UIAbility, AbilityConstant } from '@kit.AbilityKit'; 2330 2331 export default class EntryAbility extends UIAbility { 2332 onCreate(want: Want, param: AbilityConstant.LaunchParam) { // ability 生命周期函数 2333 let sessionId: string = want.parameters![appAccount.Constants.KEY_SESSION_ID] as string; 2334 try { 2335 appAccountManager.getAuthCallback(sessionId).then((callback: appAccount.AuthCallback) => { 2336 let result: appAccount.AuthResult = { 2337 account: { 2338 name: 'Lisi', 2339 owner: 'com.example.accountjsdemo', 2340 }, 2341 tokenInfo: { 2342 token: 'xxxxxx', 2343 authType: 'getSocialData' 2344 } 2345 }; 2346 callback.onResult(0, result); 2347 }).catch((err: BusinessError) => { 2348 console.log('getAuthCallback err: ' + JSON.stringify(err)); 2349 }); 2350 } catch (err) { 2351 console.log('getAuthCallback exception: ' + JSON.stringify(err)); 2352 } 2353 } 2354 } 2355 ``` 2356 2357### queryAuthenticatorInfo<sup>9+</sup> 2358 2359queryAuthenticatorInfo(owner: string, callback: AsyncCallback<AuthenticatorInfo>): void 2360 2361获取指定应用的认证器信息。使用callback异步回调。 2362 2363**系统能力:** SystemCapability.Account.AppAccount 2364 2365**参数:** 2366 2367| 参数名 | 类型 | 必填 | 说明 | 2368| -------- | -------------------------------------- | ---- | ----------- | 2369| owner | string | 是 | 应用账号所有者的包名。 | 2370| callback | AsyncCallback<[AuthenticatorInfo](#authenticatorinfo8)> | 是 | 回调函数。当获取成功时,err为null,data为认证器信息对象;否则为错误对象。 | 2371 2372**错误码:** 2373 2374| 错误码ID | 错误信息| 2375| ------- | -------| 2376| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2377| 12300001 | System service exception. | 2378| 12300002 | Invalid owner. | 2379| 12300113 | Authenticator service not found. | 2380 2381**示例:** 2382 2383 ```ts 2384 import { BusinessError } from '@kit.BasicServicesKit'; 2385 2386 try { 2387 appAccountManager.queryAuthenticatorInfo('com.example.accountjsdemo', 2388 (err: BusinessError, info: appAccount.AuthenticatorInfo) => { 2389 if (err) { 2390 console.log('queryAuthenticatorInfo failed, error: ' + JSON.stringify(err)); 2391 } else { 2392 console.log('queryAuthenticatorInfo successfully, info: ' + JSON.stringify(info)); 2393 } 2394 }); 2395 } catch (err) { 2396 console.log('queryAuthenticatorInfo exception: ' + JSON.stringify(err)); 2397 } 2398 ``` 2399 2400### queryAuthenticatorInfo<sup>9+</sup> 2401 2402queryAuthenticatorInfo(owner: string): Promise<AuthenticatorInfo> 2403 2404获取指定应用的认证器信息。使用Promise异步回调。 2405 2406**系统能力:** SystemCapability.Account.AppAccount 2407 2408**参数:** 2409 2410| 参数名 | 类型 | 必填 | 说明 | 2411| ----- | ------ | ---- | ----------- | 2412| owner | string | 是 | 应用账号所有者的包名。 | 2413 2414**返回值:** 2415 2416| 类型 | 说明 | 2417| -------------------------------- | --------------------- | 2418| Promise<[AuthenticatorInfo](#authenticatorinfo8)> | Promise对象,返回指定应用的认证器信息对象。 | 2419 2420**错误码:** 2421 2422| 错误码ID | 错误信息| 2423| ------- | -------| 2424| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2425| 12300001 | System service exception. | 2426| 12300002 | Invalid owner. | 2427| 12300113 | Authenticator service not found. | 2428 2429**示例:** 2430 2431 ```ts 2432 import { BusinessError } from '@kit.BasicServicesKit'; 2433 2434 try { 2435 appAccountManager.queryAuthenticatorInfo('com.example.accountjsdemo').then(( 2436 info: appAccount.AuthenticatorInfo) => { 2437 console.log('queryAuthenticatorInfo successfully, info: ' + JSON.stringify(info)); 2438 }).catch((err: BusinessError) => { 2439 console.log('queryAuthenticatorInfo failed, error: ' + JSON.stringify(err)); 2440 }); 2441 } catch (err) { 2442 console.log('queryAuthenticatorInfo exception: ' + JSON.stringify(err)); 2443 } 2444 ``` 2445 2446### checkAccountLabels<sup>9+</sup> 2447 2448checkAccountLabels(name: string, owner: string, labels: Array<string>, callback: AsyncCallback<boolean>): void 2449 2450检查指定应用账号是否满足特定的标签集合。使用callback异步回调。该方法依赖目标应用的认证器提供标签检查的能力。 2451 2452**系统能力:** SystemCapability.Account.AppAccount 2453 2454**参数:** 2455 2456| 参数名 | 类型 | 必填 | 说明 | 2457| -------------- | ------------------------- | ----- | --------------- | 2458| name | string | 是 | 应用账号的名称。 | 2459| owner | string | 是 | 应用账号所有者的包名。| 2460| labels | Array<string> | 是 | 标签数组。 | 2461| callback | AsyncCallback<boolean> | 是 | 回调函数。当检查成功时,err为null,data为true表示满足特定的标签集合,data为false表示不满足;否则为错误对象。 | 2462 2463**错误码:** 2464 2465| 错误码ID | 错误信息 | 2466| ------- | ------- | 2467| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2468| 12300001 | System service exception. | 2469| 12300002 | Invalid name, owner or labels. | 2470| 12300003 | Account not found. | 2471| 12300010 | Account service busy. | 2472| 12300113 | Authenticator service not found. | 2473| 12300114 | Authenticator service exception. | 2474 2475**示例:** 2476 2477 ```ts 2478 import { BusinessError } from '@kit.BasicServicesKit'; 2479 2480 let labels = ['student']; 2481 try { 2482 appAccountManager.checkAccountLabels('zhangsan', 'com.example.accountjsdemo', labels, 2483 (err: BusinessError, hasAllLabels: boolean) => { 2484 if (err) { 2485 console.log('checkAccountLabels failed, error: ' + JSON.stringify(err)); 2486 } else { 2487 console.log('checkAccountLabels successfully, hasAllLabels: ' + hasAllLabels); 2488 } 2489 }); 2490 } catch (err) { 2491 console.log('checkAccountLabels exception: ' + JSON.stringify(err)); 2492 } 2493 ``` 2494 2495### checkAccountLabels<sup>9+</sup> 2496 2497checkAccountLabels(name: string, owner: string, labels: Array<string>): Promise<boolean> 2498 2499检查指定应用账号是否满足特定的标签集合。使用Promise异步回调。该方法依赖目标应用的认证器提供标签检查的能力。 2500 2501**系统能力:** SystemCapability.Account.AppAccount 2502 2503**参数:** 2504 2505| 参数名 | 类型 | 必填 | 说明 | 2506| -------------- | ------------------------- | ----- | --------------- | 2507| name | string | 是 | 应用账号的名称。 | 2508| owner | string | 是 | 应用账号所有者的包名。| 2509| labels | Array<string> | 是 | 标签数组。 | 2510 2511**返回值:** 2512 2513| 类型 | 说明 | 2514| ------------------- | -------------------------------- | 2515| Promise<boolean> | Promise对象。返回true表示指定账号满足特定的标签集合,返回false表示不满足。 | 2516 2517**错误码:** 2518 2519| 错误码ID | 错误信息 | 2520| ------- | ------- | 2521| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2522| 12300001 | System service exception. | 2523| 12300002 | Invalid name, owner or labels. | 2524| 12300003 | Account not found. | 2525| 12300010 | Account service busy. | 2526| 12300113 | Authenticator service not found. | 2527| 12300114 | Authenticator service exception. | 2528 2529**示例:** 2530 2531 ```ts 2532 import { BusinessError } from '@kit.BasicServicesKit'; 2533 2534 let labels = ['student']; 2535 try { 2536 appAccountManager.checkAccountLabels('zhangsan', 'com.example.accountjsdemo', labels).then(( 2537 hasAllLabels: boolean) => { 2538 console.log('checkAccountLabels successfully: ' + hasAllLabels); 2539 }).catch((err: BusinessError) => { 2540 console.log('checkAccountLabels failed, error: ' + JSON.stringify(err)); 2541 }); 2542 } catch (err) { 2543 console.log('checkAccountLabels exception: ' + JSON.stringify(err)); 2544 } 2545 ``` 2546 2547### deleteCredential<sup>9+</sup> 2548 2549deleteCredential(name: string, credentialType: string, callback: AsyncCallback<void>): void 2550 2551删除指定应用账号的特定类型的凭据信息。使用callback异步回调。 2552 2553**系统能力:** SystemCapability.Account.AppAccount 2554 2555**参数:** 2556 2557| 参数名 | 类型 | 必填 | 说明 | 2558| -------------- | ------------------------- | ----- | -------------- | 2559| name | string | 是 | 应用账号的名称。 | 2560| credentialType | string | 是 | 凭据类型。 | 2561| callback | AsyncCallback<void> | 是 | 回调函数。当删除成功时,err为null;否则为错误对象。 | 2562 2563**错误码:** 2564 2565| 错误码ID | 错误信息 | 2566| ------- | ------- | 2567| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2568| 12300001 | System service exception. | 2569| 12300002 | Invalid name or credentialType. | 2570| 12300003 | Account not found. | 2571| 12300102 | Credential not found. | 2572 2573**示例:** 2574 2575 ```ts 2576 import { BusinessError } from '@kit.BasicServicesKit'; 2577 2578 try { 2579 appAccountManager.deleteCredential('zhangsan', 'PIN_SIX', (err: BusinessError) => { 2580 if (err) { 2581 console.log('deleteCredential failed, error: ' + JSON.stringify(err)); 2582 } else { 2583 console.log('deleteCredential successfully'); 2584 } 2585 }); 2586 } catch (err) { 2587 console.log('deleteCredential exception: ' + JSON.stringify(err)); 2588 } 2589 ``` 2590 2591### deleteCredential<sup>9+</sup> 2592 2593deleteCredential(name: string, credentialType: string): Promise<void> 2594 2595删除指定应用账号的特定类型的凭据信息。使用Promise异步回调。 2596 2597**系统能力:** SystemCapability.Account.AppAccount 2598 2599**参数:** 2600 2601| 参数名 | 类型 | 必填 | 说明 | 2602| -------------- | ------ | ----- | --------------- | 2603| name | string | 是 | 应用账号的名称。 | 2604| credentialType | string | 是 | 凭据类型。 | 2605 2606**返回值:** 2607 2608| 类型 | 说明 | 2609| ------------------- | -------------------------------- | 2610| Promise<void> | 无返回结果的Promise对象。 | 2611 2612**错误码:** 2613 2614| 错误码ID | 错误信息 | 2615| ------- | ------- | 2616| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2617| 12300001 | System service exception. | 2618| 12300002 | Invalid name or credentialType. | 2619| 12300003 | Account not found. | 2620| 12300102 | Credential not found. | 2621 2622**示例:** 2623 2624 ```ts 2625 import { BusinessError } from '@kit.BasicServicesKit'; 2626 2627 try { 2628 appAccountManager.deleteCredential('zhangsan', 'PIN_SIX').then(() => { 2629 console.log('deleteCredential successfully'); 2630 }).catch((err: BusinessError) => { 2631 console.log('deleteCredential failed, error: ' + JSON.stringify(err)); 2632 }); 2633 } catch (err) { 2634 console.log('deleteCredential exception: ' + JSON.stringify(err)); 2635 } 2636 ``` 2637 2638### selectAccountsByOptions<sup>9+</sup> 2639 2640selectAccountsByOptions(options: SelectAccountsOptions, callback: AsyncCallback<Array<AppAccountInfo>>): void 2641 2642根据选项选择调用方可访问的账号列表。使用callback异步回调。如果选项中包含标签约束,则该方法依赖目标应用的认证器提供标签检查的能力。 2643 2644**系统能力:** SystemCapability.Account.AppAccount 2645 2646**参数:** 2647 2648| 参数名 | 类型 | 必填 | 说明 | 2649| -------------- | ----------------------------------- | ----- | --------------- | 2650| options | [SelectAccountsOptions](#selectaccountsoptions9) | 是 | 选择账号的选项。 | 2651| callback | AsyncCallback<Array<[AppAccountInfo](#appaccountinfo)>> | 是 | 回调函数。当根据选项选择请求方可访问的账号列表时,err为null,data为可访问的账号信息对象;否则为错误对象。 | 2652 2653**错误码:** 2654 2655| 错误码ID | 错误信息 | 2656| ------- | ------- | 2657| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2658| 12300001 | System service exception. | 2659| 12300002 | Invalid options. | 2660| 12300010 | Account service busy. | 2661| 12300114 | Authenticator service exception. | 2662 2663**示例:** 2664 2665 ```ts 2666 import { BusinessError } from '@kit.BasicServicesKit'; 2667 2668 let options: appAccount.SelectAccountsOptions = { 2669 allowedOwners: [ 'com.example.accountjsdemo' ], 2670 requiredLabels: [ 'student' ] 2671 }; 2672 try { 2673 appAccountManager.selectAccountsByOptions(options, 2674 (err: BusinessError, accountArr: appAccount.AppAccountInfo[]) => { 2675 if (err) { 2676 console.log('selectAccountsByOptions failed, error: ' + JSON.stringify(err)); 2677 } else { 2678 console.log('selectAccountsByOptions successfully, accountArr: ' + JSON.stringify(accountArr)); 2679 } 2680 }); 2681 } catch (err) { 2682 console.log('selectAccountsByOptions exception: ' + JSON.stringify(err)); 2683 } 2684 ``` 2685 2686### selectAccountsByOptions<sup>9+</sup> 2687 2688selectAccountsByOptions(options: SelectAccountsOptions): Promise<Array<AppAccountInfo>> 2689 2690根据选项选择调用方可访问的账号列表。使用Promise异步回调。如果选项中包含标签约束,则该方法依赖目标应用的认证器提供标签检查的能力。 2691 2692**系统能力:** SystemCapability.Account.AppAccount 2693 2694**参数:** 2695 2696| 参数名 | 类型 | 必填 | 说明 | 2697| -------------- | ------------------------- | ----- | --------------- | 2698| options | [SelectAccountsOptions](#selectaccountsoptions9) | 是 | 选择账号的选项。 | 2699 2700**返回值:** 2701 2702| 类型 | 说明 | 2703| ------------------- | -------------------------------- | 2704| Promise<[AppAccountInfo](#appaccountinfo)> | Promise对象,返回调用方可访问的账号列表。 | 2705 2706**错误码:** 2707 2708| 错误码ID | 错误信息 | 2709| ------- | ------- | 2710| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2711| 12300001 | System service exception. | 2712| 12300002 | Invalid options. | 2713| 12300010 | Account service busy. | 2714| 12300114 | Authenticator service exception. | 2715 2716**示例:** 2717 2718 ```ts 2719 import { BusinessError } from '@kit.BasicServicesKit'; 2720 2721 let options: appAccount.SelectAccountsOptions = { 2722 allowedOwners: ['com.example.accountjsdemo'] 2723 }; 2724 try { 2725 appAccountManager.selectAccountsByOptions(options).then((accountArr: appAccount.AppAccountInfo[]) => { 2726 console.log('selectAccountsByOptions successfully, accountArr: ' + JSON.stringify(accountArr)); 2727 }).catch((err: BusinessError) => { 2728 console.log('selectAccountsByOptions failed, error: ' + JSON.stringify(err)); 2729 }); 2730 } catch (err) { 2731 console.log('selectAccountsByOptions exception: ' + JSON.stringify(err)); 2732 } 2733 ``` 2734 2735### verifyCredential<sup>9+</sup> 2736 2737verifyCredential(name: string, owner: string, callback: AuthCallback): void 2738 2739验证指定账号的凭据。使用callback异步回调。 2740 2741**系统能力:** SystemCapability.Account.AppAccount 2742 2743**参数:** 2744 2745| 参数名 | 类型 | 必填 | 说明 | 2746| -------- | --------------------- | ----- | ----------------------- | 2747| name | string | 是 | 应用账号的名称。 | 2748| owner | string | 是 | 应用账号所有者的包名。 | 2749| callback | [AuthCallback](#authcallback9) | 是 | 回调函数,返回验证结果。 | 2750 2751**错误码:** 2752 2753| 错误码ID | 错误信息| 2754| ------- | -------| 2755| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2756| 12300001 | System service exception. | 2757| 12300002 | Invalid name or owner. | 2758| 12300003 | Account not found. | 2759| 12300010 | Account service busy. | 2760| 12300113 | Authenticator service not found. | 2761| 12300114 | Authenticator service exception. | 2762 2763**示例:** 2764 2765 ```ts 2766 import { Want } from '@kit.AbilityKit'; 2767 2768 try { 2769 appAccountManager.verifyCredential('zhangsan', 'com.example.accountjsdemo', { 2770 onResult: (resultCode: number, result?: appAccount.AuthResult) => { 2771 console.log('verifyCredential onResult, resultCode: ' + JSON.stringify(resultCode)); 2772 console.log('verifyCredential onResult, result: ' + JSON.stringify(result)); 2773 }, 2774 onRequestRedirected: (request: Want) => { 2775 console.log('verifyCredential onRequestRedirected, request: ' + JSON.stringify(request)); 2776 } 2777 }); 2778 } catch (err) { 2779 console.log('verifyCredential err: ' + JSON.stringify(err)); 2780 } 2781 ``` 2782 2783### verifyCredential<sup>9+</sup> 2784 2785verifyCredential(name: string, owner: string, options: VerifyCredentialOptions, callback: AuthCallback): void 2786 2787验证用户凭据。使用callback异步回调。 2788 2789**系统能力:** SystemCapability.Account.AppAccount 2790 2791**参数:** 2792 2793| 参数名 | 类型 | 必填 | 说明 | 2794| -------- | ----------------------- | ----- | ----------------------- | 2795| name | string | 是 | 应用账号的名称。 | 2796| owner | string | 是 | 应用账号所有者的包名。 | 2797| options | [VerifyCredentialOptions](#verifycredentialoptions9) | 是 | 验证凭据的选项。 | 2798| callback | [AuthCallback](#authcallback9) | 是 | 回调函数,返回验证结果。 | 2799 2800**错误码:** 2801 2802| 错误码ID | 错误信息| 2803| ------- | -------| 2804| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2805| 12300001 | System service exception. | 2806| 12300002 | Invalid name, owner or options. | 2807| 12300003 | Account not found. | 2808| 12300010 | Account service busy. | 2809| 12300113 | Authenticator service not found. | 2810| 12300114 | Authenticator service exception. | 2811 2812**示例:** 2813 2814 ```ts 2815 import { Want } from '@kit.AbilityKit'; 2816 2817 let options: appAccount.VerifyCredentialOptions = { 2818 credentialType: 'pin', 2819 credential: '123456' 2820 }; 2821 try { 2822 appAccountManager.verifyCredential('zhangsan', 'com.example.accountjsdemo', options, { 2823 onResult: (resultCode: number, result?: appAccount.AuthResult) => { 2824 console.log('verifyCredential onResult, resultCode: ' + JSON.stringify(resultCode)); 2825 console.log('verifyCredential onResult, result: ' + JSON.stringify(result)); 2826 }, 2827 onRequestRedirected: (request: Want) => { 2828 console.log('verifyCredential onRequestRedirected, request: ' + JSON.stringify(request)); 2829 } 2830 }); 2831 } catch (err) { 2832 console.log('verifyCredential err: ' + JSON.stringify(err)); 2833 } 2834 ``` 2835 2836### setAuthenticatorProperties<sup>9+</sup> 2837 2838setAuthenticatorProperties(owner: string, callback: AuthCallback): void 2839 2840设置指定应用的认证器属性。使用callback异步回调。 2841 2842**系统能力:** SystemCapability.Account.AppAccount 2843 2844**参数:** 2845 2846| 参数名 | 类型 | 必填 | 说明 | 2847| -------- | --------------------- | ----- | ----------------------- | 2848| owner | string | 是 | 认证器的所有者的包名。 | 2849| callback | [AuthCallback](#authcallback9) | 是 | 回调函数,返回设置属性的结果。 | 2850 2851**错误码:** 2852 2853| 错误码ID | 错误信息 | 2854| ------- | ------- | 2855| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2856| 12300001 | System service exception. | 2857| 12300002 | Invalid owner. | 2858| 12300010 | Account service busy. | 2859| 12300113 | Authenticator service not found. | 2860| 12300114 | Authenticator service exception. | 2861 2862**示例:** 2863 2864 ```ts 2865 import { Want } from '@kit.AbilityKit'; 2866 2867 try { 2868 appAccountManager.setAuthenticatorProperties('com.example.accountjsdemo', { 2869 onResult: (resultCode: number, result?: appAccount.AuthResult) => { 2870 console.log('setAuthenticatorProperties onResult, resultCode: ' + JSON.stringify(resultCode)); 2871 console.log('setAuthenticatorProperties onResult, result: ' + JSON.stringify(result)); 2872 }, 2873 onRequestRedirected: (request: Want) => { 2874 console.log('setAuthenticatorProperties onRequestRedirected, request: ' + JSON.stringify(request)); 2875 } 2876 }); 2877 } catch (err) { 2878 console.log('setAuthenticatorProperties err: ' + JSON.stringify(err)); 2879 } 2880 ``` 2881 2882### setAuthenticatorProperties<sup>9+</sup> 2883 2884setAuthenticatorProperties(owner: string, options: SetPropertiesOptions, callback: AuthCallback): void 2885 2886设置认证器属性。使用callback异步回调。 2887 2888**系统能力:** SystemCapability.Account.AppAccount 2889 2890**参数:** 2891 2892| 参数名 | 类型 | 必填 | 说明 | 2893| -------- | --------------------- | ----- | ----------------------- | 2894| owner | string | 是 | 认证器的所有者的包名。 | 2895| options | [SetPropertiesOptions](#setpropertiesoptions9) | 是 | 设置属性的选项。 | 2896| callback | [AuthCallback](#authcallback9) | 是 | 认证器回调,返回设置属性的结果。 | 2897 2898**错误码:** 2899 2900| 错误码ID | 错误信息 | 2901| ------- | ------- | 2902| 401 |Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 2903| 12300001 | System service exception. | 2904| 12300002 | Invalid owner or options. | 2905| 12300010 | Account service busy. | 2906| 12300113 | Authenticator service not found. | 2907| 12300114 | Authenticator service exception. | 2908 2909**示例:** 2910 2911 ```ts 2912 import { Want } from '@kit.AbilityKit'; 2913 2914 let options: appAccount.SetPropertiesOptions = { 2915 properties: {prop1: 'value1'} 2916 }; 2917 try { 2918 appAccountManager.setAuthenticatorProperties('com.example.accountjsdemo', options, { 2919 onResult: (resultCode: number, result?: appAccount.AuthResult) => { 2920 console.log('setAuthenticatorProperties onResult, resultCode: ' + JSON.stringify(resultCode)); 2921 console.log('setAuthenticatorProperties onResult, result: ' + JSON.stringify(result)); 2922 }, 2923 onRequestRedirected: (request: Want) => { 2924 console.log('setAuthenticatorProperties onRequestRedirected, request: ' + JSON.stringify(request)); 2925 } 2926 }); 2927 } catch (err) { 2928 console.log('setAuthenticatorProperties err: ' + JSON.stringify(err)); 2929 } 2930 2931 ``` 2932 2933### addAccount<sup>(deprecated)</sup> 2934 2935addAccount(name: string, callback: AsyncCallback<void>): void 2936 2937根据账号名添加应用账号。使用callback异步回调。 2938 2939> **说明:** 2940> 2941>从 API version 7开始支持,从API version 9开始废弃。建议使用[createAccount](#createaccount9)替代。 2942 2943 2944**系统能力:** SystemCapability.Account.AppAccount 2945 2946**参数:** 2947 2948| 参数名 | 类型 | 必填 | 说明 | 2949| -------- | ------------------------- | ---- | -------------------- | 2950| name | string | 是 | 应用账号的名称。 | 2951| callback | AsyncCallback<void> | 是 | 回调函数。当创建成功时,err为null,否则为错误对象。 | 2952 2953**示例:** 2954 2955 ```ts 2956 import { BusinessError } from '@kit.BasicServicesKit'; 2957 2958 appAccountManager.addAccount('WangWu', (err: BusinessError) => { 2959 console.log('addAccount err: ' + JSON.stringify(err)); 2960 }); 2961 ``` 2962 2963### addAccount<sup>(deprecated)</sup> 2964 2965addAccount(name: string, extraInfo: string, callback: AsyncCallback<void>): void 2966 2967根据账号名和额外信息添加应用账号。使用callback异步回调。 2968 2969> **说明:** 2970> 从 API version 7开始支持,从API version 9开始废弃。建议使用[createAccount](#createaccount9-1)替代。 2971 2972**系统能力:** SystemCapability.Account.AppAccount 2973 2974**参数:** 2975 2976| 参数名 | 类型 | 必填 | 说明 | 2977| --------- | ------------------------- | ---- | ---------------------------------------- | 2978| name | string | 是 | 应用账号的名称。 | 2979| extraInfo | string | 是 | 额外信息(能转换string类型的其它信息),额外信息不能是应用账号的敏感信息(如应用账号密码、token等)。 | 2980| callback | AsyncCallback<void> | 是 | 回调函数。当创建成功时,err为null,否则为错误对象。 | 2981 2982**示例:** 2983 2984 ```ts 2985 import { BusinessError } from '@kit.BasicServicesKit'; 2986 2987 appAccountManager.addAccount('LiSi', 'token101', (err: BusinessError) => { 2988 console.log('addAccount err: ' + JSON.stringify(err)); 2989 }); 2990 ``` 2991 2992### addAccount<sup>(deprecated)</sup> 2993 2994addAccount(name: string, extraInfo?: string): Promise<void> 2995 2996根据账号名和额外信息添加应用账号。使用callback异步回调。使用Promise异步回调。 2997 2998> **说明:** 2999> 从 API version 7开始支持,从API version 9开始废弃。建议使用[createAccount](#createaccount9-2)替代。 3000 3001**系统能力:** SystemCapability.Account.AppAccount 3002 3003**参数:** 3004 3005| 参数名 | 类型 | 必填 | 说明 | 3006| --------- | ------ | ---- | ---------------------------------------- | 3007| name | string | 是 | 应用账号的名称。 | 3008| extraInfo | string | 否 | 额外信息(能转换string类型的其它信息),额外信息不能是应用账号的敏感信息(如应用账号密码、token等),默认为空,表示创建的该账号无额外信息需要添加。 | 3009 3010**返回值:** 3011 3012| 类型 | 说明 | 3013| ------------------- | --------------------- | 3014| Promise<void> | 无返回结果的Promise对象。 | 3015 3016**示例:** 3017 3018 ```ts 3019 import { BusinessError } from '@kit.BasicServicesKit'; 3020 3021 appAccountManager.addAccount('LiSi', 'token101').then(()=> { 3022 console.log('addAccount Success'); 3023 }).catch((err: BusinessError) => { 3024 console.log('addAccount err: ' + JSON.stringify(err)); 3025 }); 3026 ``` 3027 3028### addAccountImplicitly<sup>(deprecated)</sup> 3029 3030addAccountImplicitly(owner: string, authType: string, options: {[key: string]: any;}, callback: AuthenticatorCallback): void 3031 3032根据指定的账号所有者隐式地添加应用账号。使用callback异步回调。 3033 3034> **说明:** 3035> 3036> 从 API version 8开始支持,从API version 9开始废弃。建议使用[createAccountImplicitly](#createaccountimplicitly9)替代。 3037 3038**系统能力:** SystemCapability.Account.AppAccount 3039 3040**参数:** 3041 3042| 参数名 | 类型 | 必填 | 说明 | 3043| -------- | --------------------- | ---- | ----------------------- | 3044| owner | string | 是 | 应用账号所有者的包名。 | 3045| authType | string | 是 | 鉴权类型。鉴权类型为自定义。 | 3046| options | {[key: string]: any} | 是 | 鉴权所需要的可选项。可选项可根据自己需要设置。 | 3047| callback | [AuthenticatorCallback](#authenticatorcallbackdeprecated) | 是 | 认证器回调对象,返回添加结果。 | 3048 3049**示例:** 3050 3051 ```ts 3052 import { BusinessError } from '@kit.BasicServicesKit'; 3053 import { Want, common } from '@kit.AbilityKit'; 3054 3055 @Entry 3056 @Component 3057 struct Index { 3058 context = this.getUIContext().getHostContext() as common.UIAbilityContext; // UIAbilityContext 3059 3060 onResultCallback(code: number, result: Record<string, Object>): void { 3061 console.log('resultCode: ' + code); 3062 console.log('result: ' + JSON.stringify(result)); 3063 } 3064 3065 onRequestRedirectedCallback(request: Want): void { 3066 let wantInfo: Want = { 3067 deviceId: '', 3068 bundleName: 'com.example.accountjsdemo', 3069 action: 'ohos.want.action.viewData', 3070 entities: ['entity.system.default'], 3071 } 3072 this.context.startAbility(wantInfo).then(() => { 3073 console.log('startAbility successfully'); 3074 }).catch((err: BusinessError) => { 3075 console.log('startAbility err: ' + JSON.stringify(err)); 3076 }) 3077 } 3078 3079 aboutToAppear(): void { 3080 appAccountManager.addAccountImplicitly('com.example.accountjsdemo', 'getSocialData', {}, { 3081 onResult: this.onResultCallback, 3082 onRequestRedirected: this.onRequestRedirectedCallback 3083 }); 3084 } 3085 build() {} 3086 } 3087 ``` 3088 3089### deleteAccount<sup>(deprecated)</sup> 3090 3091deleteAccount(name: string, callback: AsyncCallback<void>): void 3092 3093删除应用账号。使用callback异步回调。 3094 3095> **说明:** 3096> 3097> 从 API version 7开始支持,从API version 9开始废弃。建议使用[removeAccount](#removeaccount9)替代。 3098 3099**系统能力:** SystemCapability.Account.AppAccount 3100 3101**参数:** 3102 3103| 参数名 | 类型 | 必填 | 说明 | 3104| -------- | ------------------------- | ---- | ---------------- | 3105| name | string | 是 | 应用账号的名称。 | 3106| callback | AsyncCallback<void> | 是 | 回调函数。当删除成功时,err为null,否则为错误对象。 | 3107 3108**示例:** 3109 3110 ```ts 3111 import { BusinessError } from '@kit.BasicServicesKit'; 3112 3113 appAccountManager.deleteAccount('ZhaoLiu', (err: BusinessError) => { 3114 console.log('deleteAccount err: ' + JSON.stringify(err)); 3115 }); 3116 ``` 3117 3118### deleteAccount<sup>(deprecated)</sup> 3119 3120deleteAccount(name: string): Promise<void> 3121 3122删除应用账号。使用Promise异步回调。 3123 3124> **说明:** 3125> 3126> 从 API version 7开始支持,从API version 9开始废弃。建议使用[removeAccount](#removeaccount9)替代。 3127 3128**系统能力:** SystemCapability.Account.AppAccount 3129 3130**参数:** 3131 3132| 参数名 | 类型 | 必填 | 说明 | 3133| ---- | ------ | ---- | ----------- | 3134| name | string | 是 | 应用账号的名称。 | 3135 3136**返回值:** 3137 3138| 类型 | 说明 | 3139| :------------------ | :-------------------- | 3140| Promise<void> | 无返回结果的Promise对象。 | 3141 3142**示例:** 3143 3144 ```ts 3145 import { BusinessError } from '@kit.BasicServicesKit'; 3146 3147 appAccountManager.deleteAccount('ZhaoLiu').then(() => { 3148 console.log('deleteAccount Success'); 3149 }).catch((err: BusinessError) => { 3150 console.log('deleteAccount err: ' + JSON.stringify(err)); 3151 }); 3152 ``` 3153### disableAppAccess<sup>(deprecated)</sup> 3154 3155disableAppAccess(name: string, bundleName: string, callback: AsyncCallback<void>): void 3156 3157禁止指定第三方应用账号名称对指定的第三方应用进行访问。使用callback异步回调。 3158 3159> **说明:** 3160> 3161> 从 API version 7开始支持,从API version 9开始废弃。建议使用[setAppAccess](#setappaccess9)替代。 3162 3163**系统能力:** SystemCapability.Account.AppAccount 3164 3165**参数:** 3166 3167| 参数名 | 类型 | 必填 | 说明 | 3168| ---------- | ------------------------- | ---- | --------------------------------- | 3169| name | string | 是 | 应用账号的名称。 | 3170| bundleName | string | 是 | 第三方应用的包名。 | 3171| callback | AsyncCallback<void> | 是 | 回调函数。当禁止指定第三方应用账号名称对指定包名称的第三方应用进行访问设置成功时,err为null,否则为错误对象。 | 3172 3173**示例:** 3174 3175 ```ts 3176 import { BusinessError } from '@kit.BasicServicesKit'; 3177 3178 appAccountManager.disableAppAccess('ZhangSan', 'com.example.accountjsdemo', (err: BusinessError) => { 3179 console.log('disableAppAccess err: ' + JSON.stringify(err)); 3180 }); 3181 ``` 3182 3183### disableAppAccess<sup>(deprecated)</sup> 3184 3185disableAppAccess(name: string, bundleName: string): Promise<void> 3186 3187禁止指定第三方应用账号名称对指定包名称的第三方应用进行访问。使用Promise异步回调。 3188 3189> **说明:** 3190> 3191> 从 API version 7开始支持,从API version 9开始废弃。建议使用[setAppAccess](#setappaccess9-1)替代。 3192 3193**系统能力:** SystemCapability.Account.AppAccount 3194 3195**参数:** 3196 3197| 参数名 | 类型 | 必填 | 说明 | 3198| ---------- | ------ | ---- | ---------------- | 3199| name | string | 是 | 要禁用访问的第三方应用账号的名称。 | 3200| bundleName | string | 是 | 第三方应用的包名。 | 3201 3202**返回值:** 3203 3204| 类型 | 说明 | 3205| :------------------ | :-------------------- | 3206| Promise<void> | 无返回结果的Promise对象。 | 3207 3208**示例:** 3209 3210 ```ts 3211 import { BusinessError } from '@kit.BasicServicesKit'; 3212 3213 appAccountManager.disableAppAccess('ZhangSan', 'com.example.accountjsdemo').then(() => { 3214 console.log('disableAppAccess Success'); 3215 }).catch((err: BusinessError) => { 3216 console.log('disableAppAccess err: ' + JSON.stringify(err)); 3217 }); 3218 ``` 3219 3220### enableAppAccess<sup>(deprecated)</sup> 3221 3222enableAppAccess(name: string, bundleName: string, callback: AsyncCallback<void>): void 3223 3224允许指定第三方应用账号名称对指定包名称的第三方应用进行访问。使用callback异步回调。 3225 3226> **说明:** 3227> 3228> 从 API version 7开始支持,从API version 9开始废弃。建议使用[setAppAccess](#setappaccess9)替代。 3229 3230**系统能力:** SystemCapability.Account.AppAccount 3231 3232**参数:** 3233 3234| 参数名 | 类型 | 必填 | 说明 | 3235| ---------- | ------------------------- | ---- | --------------------------------- | 3236| name | string | 是 | 应用账号的名称。 | 3237| bundleName | string | 是 | 第三方应用的包名。 | 3238| callback | AsyncCallback<void> | 是 | 回调函数。当允许指定第三方应用账号名称对指定包名称的第三方应用进行访问设置成功时,err为null,否则为错误对象。 | 3239 3240**示例:** 3241 3242 ```ts 3243 import { BusinessError } from '@kit.BasicServicesKit'; 3244 3245 appAccountManager.enableAppAccess('ZhangSan', 'com.example.accountjsdemo', (err: BusinessError) => { 3246 console.log('enableAppAccess: ' + JSON.stringify(err)); 3247 }); 3248 ``` 3249 3250### enableAppAccess<sup>(deprecated)</sup> 3251 3252enableAppAccess(name: string, bundleName: string): Promise<void> 3253 3254允许指定第三方应用账号的名称对指定包名称的第三方应用进行访问。使用Promise异步回调。 3255 3256> **说明:** 3257> 3258> 从 API version 7开始支持,从API version 9开始废弃。建议使用[setAppAccess](#setappaccess9-1)替代。 3259 3260**系统能力:** SystemCapability.Account.AppAccount 3261 3262**参数:** 3263 3264| 参数名 | 类型 | 必填 | 说明 | 3265| ---------- | ------ | ---- | --------- | 3266| name | string | 是 | 应用账号的名称。 | 3267| bundleName | string | 是 | 第三方应用的包名。 | 3268 3269**返回值:** 3270 3271| 类型 | 说明 | 3272| :------------------ | :-------------------- | 3273| Promise<void> | 无返回结果的Promise对象。 | 3274 3275**示例:** 3276 3277 ```ts 3278 import { BusinessError } from '@kit.BasicServicesKit'; 3279 3280 appAccountManager.enableAppAccess('ZhangSan', 'com.example.accountjsdemo').then(() => { 3281 console.log('enableAppAccess Success'); 3282 }).catch((err: BusinessError) => { 3283 console.log('enableAppAccess err: ' + JSON.stringify(err)); 3284 }); 3285 ``` 3286 3287### checkAppAccountSyncEnable<sup>(deprecated)</sup> 3288 3289checkAppAccountSyncEnable(name: string, callback: AsyncCallback<boolean>): void 3290 3291检查指定应用账号是否开启数据同步功能。使用callback异步回调。 3292 3293> **说明:** 3294> 3295> 从 API version 7开始支持,从API version 9开始废弃。建议使用[checkDataSyncEnabled](#checkdatasyncenabled9)替代。 3296 3297**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3298 3299**系统能力:** SystemCapability.Account.AppAccount 3300 3301**参数:** 3302 3303| 参数名 | 类型 | 必填 | 说明 | 3304| -------- | ---------------------------- | ---- | --------------------- | 3305| name | string | 是 | 应用账号的名称。 | 3306| callback | AsyncCallback<boolean> | 是 | 回调函数。返回true表示指定应用账号已开启数据同步功能;返回false表示未开启。 | 3307 3308**示例:** 3309 3310 ```ts 3311 import { BusinessError } from '@kit.BasicServicesKit'; 3312 3313 appAccountManager.checkAppAccountSyncEnable('ZhangSan', (err: BusinessError, result: boolean) => { 3314 console.log('checkAppAccountSyncEnable err: ' + JSON.stringify(err)); 3315 console.log('checkAppAccountSyncEnable result: ' + result); 3316 }); 3317 ``` 3318 3319### checkAppAccountSyncEnable<sup>(deprecated)</sup> 3320 3321checkAppAccountSyncEnable(name: string): Promise<boolean> 3322 3323检查指定应用账号是否开启数据同步功能。使用Promise异步回调。 3324 3325> **说明:** 3326> 3327> 从 API version 7开始支持,从API version 9开始废弃。建议使用[checkDataSyncEnabled](#checkdatasyncenabled9-1)替代。 3328 3329**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3330 3331**系统能力:** SystemCapability.Account.AppAccount 3332 3333**参数:** 3334 3335| 参数名 | 类型 | 必填 | 说明 | 3336| ---- | ------ | ---- | ------- | 3337| name | string | 是 | 应用账号的名称。 | 3338 3339**返回值:** 3340 3341| 类型 | 说明 | 3342| ---------------------- | --------------------- | 3343| Promise<boolean> | Promise对象。返回true表示指定应用账号已开启数据同步功能;返回false表示未开启。 | 3344 3345**示例:** 3346 3347 ```ts 3348 import { BusinessError } from '@kit.BasicServicesKit'; 3349 3350 appAccountManager.checkAppAccountSyncEnable('ZhangSan').then((data: boolean) => { 3351 console.log('checkAppAccountSyncEnable, result: ' + data); 3352 }).catch((err: BusinessError) => { 3353 console.log('checkAppAccountSyncEnable err: ' + JSON.stringify(err)); 3354 }); 3355 ``` 3356 3357### setAccountCredential<sup>(deprecated)</sup> 3358 3359setAccountCredential(name: string, credentialType: string, credential: string,callback: AsyncCallback<void>): void 3360 3361设置指定应用账号的凭据。使用callback异步回调。 3362 3363> **说明:** 3364> 3365> 从 API version 7开始支持,从API version 9开始废弃,建议使用[setCredential](#setcredential9)替代。 3366 3367**系统能力:** SystemCapability.Account.AppAccount 3368 3369**参数:** 3370 3371| 参数名 | 类型 | 必填 | 说明 | 3372| -------------- | ------------------------- | ---- | ------------- | 3373| name | string | 是 | 应用账号的名称。 | 3374| credentialType | string | 是 | 凭据类型。 | 3375| credential | string | 是 | 凭据取值。 | 3376| callback | AsyncCallback<void> | 是 | 回调函数。当设置此应用程序账号的凭据成功时,err为null,否则为错误对象。 | 3377 3378**示例:** 3379 3380 ```ts 3381 import { BusinessError } from '@kit.BasicServicesKit'; 3382 3383 appAccountManager.setAccountCredential('ZhangSan', 'credentialType001', 'credential001', (err: BusinessError) => { 3384 console.log('setAccountCredential err: ' + JSON.stringify(err)); 3385 }); 3386 ``` 3387 3388### setAccountCredential<sup>(deprecated)</sup> 3389 3390setAccountCredential(name: string, credentialType: string, credential: string): Promise<void> 3391 3392设置指定应用账号的凭据。使用Promise异步回调。 3393 3394> **说明:** 3395> 3396> 从 API version 7开始支持,从API version 9开始废弃,建议使用[setCredential](#setcredential9-1)替代。 3397 3398**系统能力:** SystemCapability.Account.AppAccount 3399 3400**参数:** 3401 3402| 参数名 | 类型 | 必填 | 说明 | 3403| -------------- | ------ | ---- | ---------- | 3404| name | string | 是 | 应用账号的名称。 | 3405| credentialType | string | 是 | 凭据类型。 | 3406| credential | string | 是 | 凭据取值。 | 3407 3408**返回值:** 3409 3410| 类型 | 说明 | 3411| :------------------ | :-------------------- | 3412| Promise<void> | 无返回结果的Promise对象。 | 3413 3414**示例:** 3415 3416 ```ts 3417 import { BusinessError } from '@kit.BasicServicesKit'; 3418 3419 appAccountManager.setAccountCredential('ZhangSan', 'credentialType001', 'credential001').then(() => { 3420 console.log('setAccountCredential Success'); 3421 }).catch((err: BusinessError) => { 3422 console.log('setAccountCredential err: ' + JSON.stringify(err)); 3423 }); 3424 ``` 3425 3426### setAccountExtraInfo<sup>(deprecated)</sup> 3427 3428setAccountExtraInfo(name: string, extraInfo: string, callback: AsyncCallback<void>): void 3429 3430设置指定应用账号的额外信息。使用callback异步回调。 3431 3432> **说明:** 3433> 3434> 从 API version 7开始支持,从API version 9开始废弃。建议使用[setCustomData](#setcustomdata9)替代。 3435 3436 3437**系统能力:** SystemCapability.Account.AppAccount 3438 3439**参数:** 3440 3441| 参数名 | 类型 | 必填 | 说明 | 3442| --------- | ------------------------- | ---- | --------------- | 3443| name | string | 是 | 应用账号的名称。 | 3444| extraInfo | string | 是 | 额外信息(能转换string类型的其它信息),额外信息不能是应用账号的敏感信息(如应用账号密码、token等)。 | 3445| callback | AsyncCallback<void> | 是 | 回调函数。当设置成功时,err为null,否则为错误对象。 | 3446 3447**示例:** 3448 3449 ```ts 3450 import { BusinessError } from '@kit.BasicServicesKit'; 3451 3452 appAccountManager.setAccountExtraInfo('ZhangSan', 'Tk002', (err: BusinessError) => { 3453 console.log('setAccountExtraInfo err: ' + JSON.stringify(err)); 3454 }); 3455 ``` 3456 3457### setAccountExtraInfo<sup>(deprecated)</sup> 3458 3459setAccountExtraInfo(name: string, extraInfo: string): Promise<void> 3460 3461设置此应用程序账号的额外信息。使用Promise异步回调。 3462 3463> **说明:** 3464> 3465> 从 API version 7开始支持,从API version 9开始废弃。建议使用[setCustomData](#setcustomdata9-1)替代。 3466 3467 3468**系统能力:** SystemCapability.Account.AppAccount 3469 3470**参数:** 3471 3472| 参数名 | 类型 | 必填 | 说明 | 3473| --------- | ------ | ---- | --------- | 3474| name | string | 是 | 应用账号的名称。 | 3475| extraInfo | string | 是 | 额外信息(能转换string类型的其它信息),额外信息不能是应用账号的敏感信息(如应用账号密码、token等)。 | 3476 3477**返回值:** 3478 3479| 类型 | 说明 | 3480| :------------------ | :-------------------- | 3481| Promise<void> | 无返回结果的Promise对象。 | 3482 3483**示例:** 3484 3485 ```ts 3486 import { BusinessError } from '@kit.BasicServicesKit'; 3487 3488 appAccountManager.setAccountExtraInfo('ZhangSan', 'Tk002').then(() => { 3489 console.log('setAccountExtraInfo Success'); 3490 }).catch((err: BusinessError) => { 3491 console.log('setAccountExtraInfo err: ' + JSON.stringify(err)); 3492 }); 3493 ``` 3494 3495### setAppAccountSyncEnable<sup>(deprecated)</sup> 3496 3497setAppAccountSyncEnable(name: string, isEnable: boolean, callback: AsyncCallback<void>): void 3498 3499开启或禁止指定应用账号的数据同步功能。使用callback异步回调。 3500 3501> **说明:** 3502> 3503> 从 API version 7开始支持,从API version 9开始废弃。建议使用[setDataSyncEnabled](#setdatasyncenabled9)替代。 3504 3505**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3506 3507**系统能力:** SystemCapability.Account.AppAccount 3508 3509**参数:** 3510 3511| 参数名 | 类型 | 必填 | 说明 | 3512| -------- | ------------------------- | ---- | ------------------------- | 3513| name | string | 是 | 应用账号的名称。 | 3514| isEnable | boolean | 是 | 是否开启数据同步。 | 3515| callback | AsyncCallback<void> | 是 | 回调函数。当开启或禁止成功时,err为null,否则为错误对象。 | 3516 3517**示例:** 3518 3519 ```ts 3520 import { BusinessError } from '@kit.BasicServicesKit'; 3521 3522 appAccountManager.setAppAccountSyncEnable('ZhangSan', true, (err: BusinessError) => { 3523 console.log('setAppAccountSyncEnable err: ' + JSON.stringify(err)); 3524 }); 3525 ``` 3526 3527### setAppAccountSyncEnable<sup>(deprecated)</sup> 3528 3529setAppAccountSyncEnable(name: string, isEnable: boolean): Promise<void> 3530 3531开启或禁止指定应用账号的数据同步功能。使用Promise异步回调。 3532 3533> **说明:** 3534> 3535> 从 API version 7开始支持,从API version 9开始废弃。建议使用[setDataSyncEnabled](#setdatasyncenabled9-1)替代。 3536 3537**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3538 3539**系统能力:** SystemCapability.Account.AppAccount 3540 3541**参数:** 3542 3543| 参数名 | 类型 | 必填 | 说明 | 3544| -------- | ------- | ---- | ----------- | 3545| name | string | 是 | 应用账号的名称。 | 3546| isEnable | boolean | 是 | 是否开启数据同步。 | 3547 3548**返回值:** 3549 3550| 类型 | 说明 | 3551| :------------------ | :-------------------- | 3552| Promise<void> | 无返回结果的Promise对象。 | 3553 3554**示例:** 3555 3556 ```ts 3557 import { BusinessError } from '@kit.BasicServicesKit'; 3558 3559 appAccountManager .setAppAccountSyncEnable('ZhangSan', true).then(() => { 3560 console.log('setAppAccountSyncEnable Success'); 3561 }).catch((err: BusinessError) => { 3562 console.log('setAppAccountSyncEnable err: ' + JSON.stringify(err)); 3563 }); 3564 ``` 3565 3566### setAssociatedData<sup>(deprecated)</sup> 3567 3568setAssociatedData(name: string, key: string, value: string, callback: AsyncCallback<void>): void 3569 3570设置指定应用账号的关联数据。使用callback异步回调。 3571 3572> **说明:** 3573> 3574> 从 API version 7开始支持,从API version 9开始废弃。建议使用[setCustomData](#setcustomdata9)替代。 3575 3576 3577**系统能力:** SystemCapability.Account.AppAccount 3578 3579**参数:** 3580 3581| 参数名 | 类型 | 必填 | 说明 | 3582| -------- | ------------------------- | ---- | ----------------- | 3583| name | string | 是 | 应用账号的名称。 | 3584| key | string | 是 | 关联数据的键名。 | 3585| value | string | 是 | 关联数据的取值。 | 3586| callback | AsyncCallback<void> | 是 | 回调函数。当设置与此应用账号关联的数据成功时,err为null,否则为错误对象。 | 3587 3588**示例:** 3589 3590 ```ts 3591 import { BusinessError } from '@kit.BasicServicesKit'; 3592 3593 appAccountManager.setAssociatedData('ZhangSan', 'k001', 'v001', (err: BusinessError) => { 3594 console.log('setAssociatedData err: ' + JSON.stringify(err)); 3595 }); 3596 ``` 3597 3598### setAssociatedData<sup>(deprecated)</sup> 3599 3600setAssociatedData(name: string, key: string, value: string): Promise<void> 3601 3602设置指定应用账号的关联数据。使用Promise异步回调。 3603 3604> **说明:** 3605> 3606> 从 API version 7开始支持,从API version 9开始废弃。建议使用[setCustomData](#setcustomdata9-1)替代。 3607 3608 3609**系统能力:** SystemCapability.Account.AppAccount 3610 3611**参数:** 3612 3613| 参数名 | 类型 | 必填 | 说明 | 3614| ----- | ------ | ---- | ----------------- | 3615| name | string | 是 | 应用账号的名称。 | 3616| key | string | 是 | 关联数据的键名。 | 3617| value | string | 是 | 关联数据的取值。 | 3618 3619**返回值:** 3620 3621| 类型 | 说明 | 3622| :------------------ | :-------------------- | 3623| Promise<void> | 无返回结果的Promise对象。 | 3624 3625**示例:** 3626 3627 ```ts 3628 import { BusinessError } from '@kit.BasicServicesKit'; 3629 3630 appAccountManager.setAssociatedData('ZhangSan', 'k001', 'v001').then(() => { 3631 console.log('setAssociatedData Success'); 3632 }).catch((err: BusinessError) => { 3633 console.log('setAssociatedData err: ' + JSON.stringify(err)); 3634 }); 3635 ``` 3636 3637### getAllAccessibleAccounts<sup>(deprecated)</sup> 3638 3639getAllAccessibleAccounts(callback: AsyncCallback<Array<AppAccountInfo>>): void 3640 3641获取所有可访问的应用账号信息。使用callback异步回调。 3642 3643> **说明:** 3644> 3645> 从 API version 7开始支持,从API version 9开始废弃。建议使用[getAllAccounts](#getallaccounts9)替代。 3646 3647**需要权限:** ohos.permission.GET_ALL_APP_ACCOUNTS,该权限仅系统应用可申请。 3648 3649**系统能力:** SystemCapability.Account.AppAccount 3650 3651**参数:** 3652 3653| 参数名 | 类型 | 必填 | 说明 | 3654| -------- | ---------------------------------------- | ---- | --------- | 3655| callback | AsyncCallback<Array<[AppAccountInfo](#appaccountinfo)>> | 是 | 回调函数。当查询成功时,err为null,data为获取到的应用账号信息列表;否则为错误对象。 | 3656 3657**示例:** 3658 3659 ```ts 3660 import { BusinessError } from '@kit.BasicServicesKit'; 3661 3662 appAccountManager.getAllAccessibleAccounts((err: BusinessError, data: appAccount.AppAccountInfo[])=>{ 3663 console.debug('getAllAccessibleAccounts err: ' + JSON.stringify(err)); 3664 console.debug('getAllAccessibleAccounts data: ' + JSON.stringify(data)); 3665 }); 3666 ``` 3667 3668### getAllAccessibleAccounts<sup>(deprecated)</sup> 3669 3670getAllAccessibleAccounts(): Promise<Array<AppAccountInfo>> 3671 3672获取所有可访问的应用账号信息。使用Promise异步回调。 3673 3674> **说明:** 3675> 3676> 从 API version 7开始支持,从API version 9开始废弃。建议使用[getAllAccounts](#getallaccounts9-1)替代。 3677 3678**需要权限:** ohos.permission.GET_ALL_APP_ACCOUNTS,该权限仅系统应用可申请。 3679 3680**系统能力:** SystemCapability.Account.AppAccount 3681 3682**返回值:** 3683 3684| 类型 | 说明 | 3685| ---------------------------------------- | --------------------- | 3686| Promise<Array<[AppAccountInfo](#appaccountinfo)>> | Promise对象,返回全部应用已授权账号信息对象。 | 3687 3688**示例:** 3689 3690 ```ts 3691 import { BusinessError } from '@kit.BasicServicesKit'; 3692 3693 appAccountManager.getAllAccessibleAccounts().then((data: appAccount.AppAccountInfo[]) => { 3694 console.log('getAllAccessibleAccounts: ' + data); 3695 }).catch((err: BusinessError) => { 3696 console.log('getAllAccessibleAccounts err: ' + JSON.stringify(err)); 3697 }); 3698 ``` 3699 3700### getAllAccounts<sup>(deprecated)</sup> 3701 3702getAllAccounts(owner: string, callback: AsyncCallback<Array<AppAccountInfo>>): void 3703 3704根据应用账号所有者获取调用方可访问的应用账号列表。使用callback异步回调。 3705 3706> **说明:** 3707> 3708> 从 API version 7开始支持,从API version 9开始废弃。建议使用[getAccountsByOwner](#getaccountsbyowner9)替代。 3709 3710**需要权限:** ohos.permission.GET_ALL_APP_ACCOUNTS,该权限仅系统应用可申请。 3711 3712**系统能力:** SystemCapability.Account.AppAccount 3713 3714**参数:** 3715 3716| 参数名 | 类型 | 必填 | 说明 | 3717| -------- | ---------------------------------------- | ---- | --------- | 3718| owner | string | 是 | 应用账号所有者的包名。 | 3719| callback | AsyncCallback<Array<[AppAccountInfo](#appaccountinfo)>> | 是 | 应用账号信息列表。 | 3720 3721**示例:** 3722 3723 ```ts 3724 import { BusinessError } from '@kit.BasicServicesKit'; 3725 3726 const selfBundle = 'com.example.actsgetallaaccounts'; 3727 appAccountManager.getAllAccounts(selfBundle, (err: BusinessError, data: appAccount.AppAccountInfo[])=>{ 3728 console.debug('getAllAccounts err: ' + JSON.stringify(err)); 3729 console.debug('getAllAccounts data:' + JSON.stringify(data)); 3730 }); 3731 ``` 3732 3733### getAllAccounts<sup>(deprecated)</sup> 3734 3735getAllAccounts(owner: string): Promise<Array<AppAccountInfo>> 3736 3737根据应用账号所有者获取调用方可访问的应用账号列表。使用Promise异步回调。 3738 3739> **说明:** 3740> 3741> 从 API version 7开始支持,从API version 9开始废弃。建议使用[getAccountsByOwner](#getaccountsbyowner9-1)替代。 3742 3743**需要权限:** ohos.permission.GET_ALL_APP_ACCOUNTS,该权限仅系统应用可申请。 3744 3745**系统能力:** SystemCapability.Account.AppAccount 3746 3747**参数:** 3748 3749| 参数名 | 类型 | 必填 | 说明 | 3750| ----- | ------ | ---- | ------ | 3751| owner | string | 是 | 应用账号所有者的包名。 | 3752 3753**返回值:** 3754 3755| 类型 | 说明 | 3756| ---------------------------------------- | --------------------- | 3757| Promise<Array<[AppAccountInfo](#appaccountinfo)>> | Promise对象,返回指定应用全部账号信息对象。 | 3758 3759**示例:** 3760 3761 ```ts 3762 import { BusinessError } from '@kit.BasicServicesKit'; 3763 3764 const selfBundle = 'com.example.actsgetallaaccounts'; 3765 appAccountManager.getAllAccounts(selfBundle).then((data: appAccount.AppAccountInfo[]) => { 3766 console.log('getAllAccounts: ' + data); 3767 }).catch((err: BusinessError) => { 3768 console.log('getAllAccounts err: ' + JSON.stringify(err)); 3769 }); 3770 ``` 3771 3772### getAccountCredential<sup>(deprecated)</sup> 3773 3774getAccountCredential(name: string, credentialType: string, callback: AsyncCallback<string>): void 3775 3776获取指定应用账号的凭据。使用callback异步回调。 3777 3778> **说明:** 3779> 3780> 从 API version 7开始支持,从API version 9开始废弃。建议使用[getCredential](#getcredential9)替代。 3781 3782**系统能力:** SystemCapability.Account.AppAccount 3783 3784**参数:** 3785 3786| 参数名 | 类型 | 必填 | 说明 | 3787| -------------- | --------------------------- | ---- | -------------- | 3788| name | string | 是 | 应用账号的名称。 | 3789| credentialType | string | 是 | 凭据类型。 | 3790| callback | AsyncCallback<string> | 是 | 回调函数。当获取凭据成功时,err为null,data为指定应用账号的凭据;否则为错误对象。 | 3791 3792**示例:** 3793 3794 ```ts 3795 import { BusinessError } from '@kit.BasicServicesKit'; 3796 3797 appAccountManager.getAccountCredential('ZhangSan', 'credentialType001', (err: BusinessError, result: string) => { 3798 console.log('getAccountCredential err: ' + JSON.stringify(err)); 3799 console.log('getAccountCredential result: ' + result); 3800 }); 3801 ``` 3802 3803### getAccountCredential<sup>(deprecated)</sup> 3804 3805getAccountCredential(name: string, credentialType: string): Promise<string> 3806 3807获取指定应用账号的凭据。使用Promise异步回调。 3808 3809> **说明:** 3810> 3811> 从 API version 7开始支持,从API version 9开始废弃。建议使用[getCredential](#getcredential9-1)替代。 3812 3813**系统能力:** SystemCapability.Account.AppAccount 3814 3815**参数:** 3816 3817| 参数名 | 类型 | 必填 | 说明 | 3818| -------------- | ------ | ---- | ---------- | 3819| name | string | 是 | 应用账号的名称。 | 3820| credentialType | string | 是 | 凭据类型。 | 3821 3822**返回值:** 3823 3824| 类型 | 说明 | 3825| :-------------------- | :-------------------- | 3826| Promise<string> | Promise对象,返回指定应用账号的凭据。 | 3827 3828**示例:** 3829 3830 ```ts 3831 import { BusinessError } from '@kit.BasicServicesKit'; 3832 3833 appAccountManager.getAccountCredential('ZhangSan', 'credentialType001').then((data: string) => { 3834 console.log('getAccountCredential, result: ' + data); 3835 }).catch((err: BusinessError) => { 3836 console.log('getAccountCredential err: ' + JSON.stringify(err)); 3837 }); 3838 ``` 3839 3840### getAccountExtraInfo<sup>(deprecated)</sup> 3841 3842getAccountExtraInfo(name: string, callback: AsyncCallback<string>): void 3843 3844获取指定应用账号的额外信息(能转换成string类型的其它信息)。使用callback异步回调。 3845 3846> **说明:** 3847> 3848> 从 API version 7开始支持,从API version 9开始废弃。建议使用[getCustomData](#getcustomdata9)替代。 3849 3850**系统能力:** SystemCapability.Account.AppAccount 3851 3852**参数:** 3853 3854| 参数名 | 类型 | 必填 | 说明 | 3855| -------- | --------------------------- | ---- | --------------- | 3856| name | string | 是 | 应用账号的名称。 | 3857| callback | AsyncCallback<string> | 是 | 回调函数。当获取此应用账号的额外信息成功时,err为null,data返回此应用账号的额外信息对象;否则为错误对象。 | 3858 3859**示例:** 3860 3861 ```ts 3862 import { BusinessError } from '@kit.BasicServicesKit'; 3863 3864 appAccountManager.getAccountExtraInfo('ZhangSan', (err: BusinessError, result: string) => { 3865 console.log('getAccountExtraInfo err: ' + JSON.stringify(err)); 3866 console.log('getAccountExtraInfo result: ' + result); 3867 }); 3868 ``` 3869 3870### getAccountExtraInfo<sup>(deprecated)</sup> 3871 3872getAccountExtraInfo(name: string): Promise<string> 3873 3874获取指定应用账号的额外信息(能转换成string类型的其它信息)。使用Promise异步回调。 3875 3876> **说明:** 3877> 3878> 从 API version 7开始支持,从API version 9开始废弃。建议使用[getCustomData](#getcustomdata9-1)替代。 3879 3880**系统能力:** SystemCapability.Account.AppAccount 3881 3882**参数:** 3883 3884| 参数名 | 类型 | 必填 | 说明 | 3885| ---- | ------ | ---- | ------- | 3886| name | string | 是 | 应用账号的名称。 | 3887 3888**返回值:** 3889 3890| 类型 | 说明 | 3891| :-------------------- | :-------------------- | 3892| Promise<string> | Promise对象,返回此应用程序账号的额外信息对象。 | 3893 3894**示例:** 3895 3896 ```ts 3897 import { BusinessError } from '@kit.BasicServicesKit'; 3898 3899 appAccountManager.getAccountExtraInfo('ZhangSan').then((data: string) => { 3900 console.log('getAccountExtraInfo, result: ' + data); 3901 }).catch((err: BusinessError) => { 3902 console.log('getAccountExtraInfo err: ' + JSON.stringify(err)); 3903 }); 3904 ``` 3905 3906### getAssociatedData<sup>(deprecated)</sup> 3907 3908getAssociatedData(name: string, key: string, callback: AsyncCallback<string>): void 3909 3910根据指定键名获取特定应用账号的关联数据。使用callback异步回调。 3911 3912> **说明:** 3913> 3914> 从 API version 7开始支持,从API version 9开始废弃。建议使用[getCustomData](#getcustomdata9)替代。 3915 3916**系统能力:** SystemCapability.Account.AppAccount 3917 3918**参数:** 3919 3920| 参数名 | 类型 | 必填 | 说明 | 3921| -------- | --------------------------- | ---- | ----------------- | 3922| name | string | 是 | 应用账号的名称。 | 3923| key | string | 是 | 关联数据的键名。 | 3924| callback | AsyncCallback<string> | 是 | 回调函数。当获取成功时,err为null,data为关联数据的取值;否则为错误对象。 | 3925 3926**示例:** 3927 3928 ```ts 3929 import { BusinessError } from '@kit.BasicServicesKit'; 3930 3931 appAccountManager.getAssociatedData('ZhangSan', 'k001', (err: BusinessError, result: string) => { 3932 console.log('getAssociatedData err: ' + JSON.stringify(err)); 3933 console.log('getAssociatedData result: ' + result); 3934 }); 3935 ``` 3936 3937### getAssociatedData<sup>(deprecated)</sup> 3938 3939getAssociatedData(name: string, key: string): Promise<string> 3940 3941获取与此应用程序账号关联的数据。使用Promise异步回调。 3942 3943> **说明:** 3944> 3945> 从 API version 7开始支持,从API version 9开始废弃。建议使用[getCustomData](#getcustomdata9-1)替代。 3946 3947**系统能力:** SystemCapability.Account.AppAccount 3948 3949**参数:** 3950 3951| 参数名 | 类型 | 必填 | 说明 | 3952| ---- | ------ | ---- | --------- | 3953| name | string | 是 | 应用账号的名称。 | 3954| key | string | 是 | 关联数据的键名。 | 3955 3956**返回值:** 3957 3958| 类型 | 说明 | 3959| :-------------------- | :-------------------- | 3960| Promise<string> | Promise对象,返回关联数据的取值。 | 3961 3962**示例:** 3963 3964 ```ts 3965 import { BusinessError } from '@kit.BasicServicesKit'; 3966 3967 appAccountManager.getAssociatedData('ZhangSan', 'k001').then((data: string) => { 3968 console.log('getAssociatedData: ' + data); 3969 }).catch((err: BusinessError) => { 3970 console.log('getAssociatedData err: ' + JSON.stringify(err)); 3971 }); 3972 ``` 3973 3974### on('change')<sup>(deprecated)</sup> 3975 3976on(type: 'change', owners: Array<string>, callback: Callback<Array<AppAccountInfo>>): void 3977 3978订阅指定应用的账号信息变更事件。 3979 3980> **说明:** 3981> 3982> 从 API version 7开始支持,从API version 9开始废弃。建议使用[on('accountChange')](#onaccountchange9)替代。 3983 3984**系统能力:** SystemCapability.Account.AppAccount 3985 3986**参数:** 3987 3988| 参数名 | 类型 | 必填 | 说明 | 3989| -------- | ---------------------------------------- | ---- | ------------------------------ | 3990| type | 'change' | 是 | 事件回调类型,支持的事件为'change',当账号所有者更新账号信息时,触发该事件。 | 3991| owners | Array<string> | 是 | 应用账号所有者的包名列表。 | 3992| callback | Callback<Array<[AppAccountInfo](#appaccountinfo)>> | 是 | 需要注册的回调函数,返回信息发生变更的应用账号列表。 | 3993 3994**示例:** 3995 3996 ```ts 3997 function changeOnCallback(data: appAccount.AppAccountInfo[]): void { 3998 console.debug('receive change data:' + JSON.stringify(data)); 3999 } 4000 try{ 4001 appAccountManager.on('change', ['com.example.actsaccounttest'], changeOnCallback); 4002 } 4003 catch(err){ 4004 console.error('on accountOnOffDemo err:' + JSON.stringify(err)); 4005 } 4006 ``` 4007 4008### off('change')<sup>(deprecated)</sup> 4009 4010off(type: 'change', callback?: Callback<Array<AppAccountInfo>>): void 4011 4012取消订阅账号信息变更事件。 4013 4014> **说明:** 4015> 4016> 从 API version 7开始支持,从API version 9开始废弃。建议使用[off('accountChange')](#offaccountchange9)替代。 4017 4018**系统能力:** SystemCapability.Account.AppAccount 4019 4020**参数:** 4021 4022| 参数名 | 类型 | 必填 | 说明 | 4023| -------- | -------------------------------- | ---- | ------------ | 4024| type | 'change' | 是 | 事件回调类型,支持的事件为'change',当账号所有者更新账号信息时,触发该事件。 | 4025| callback | Callback<Array<[AppAccountInfo](#appaccountinfo)>> | 否 | 需要注销的回调函数,默认为空,表示取消该类型事件的所有回调。 | 4026 4027**示例:** 4028 4029 ```ts 4030 function changeOnCallback(data: appAccount.AppAccountInfo[]): void { 4031 console.debug('receive change data: ' + JSON.stringify(data)); 4032 appAccountManager.off('change', () => { 4033 console.debug('off finish'); 4034 }) 4035 } 4036 try{ 4037 appAccountManager.on('change', ['com.example.actsaccounttest'], changeOnCallback); 4038 } 4039 catch(err){ 4040 console.error('on accountOnOffDemo err: ' + JSON.stringify(err)); 4041 } 4042 ``` 4043 4044### authenticate<sup>(deprecated)</sup> 4045 4046authenticate(name: string, owner: string, authType: string, options: {[key: string]: any;}, callback: AuthenticatorCallback): void 4047 4048对应用账号进行鉴权以获取授权令牌。使用callback异步回调。 4049 4050> **说明:** 4051> 4052> 从 API version 8开始支持,从API version 9开始废弃。建议使用[auth](#auth9)替代。 4053 4054**系统能力:** SystemCapability.Account.AppAccount 4055 4056**参数:** 4057 4058| 参数名 | 类型 | 必填 | 说明 | 4059| -------- | --------------------- | ---- | --------------- | 4060| name | string | 是 | 应用账号的名称。 | 4061| owner | string | 是 | 应用账号所有者的包名。 | 4062| authType | string | 是 | 鉴权类型。 | 4063| options | {[key: string]: any} | 是 | 鉴权所需的可选项。 | 4064| callback | [AuthenticatorCallback](#authenticatorcallbackdeprecated) | 是 | 回调对象,返回鉴权结果。 | 4065 4066**示例:** 4067 4068 ```ts 4069 import { BusinessError } from '@kit.BasicServicesKit'; 4070 import { Want, common } from '@kit.AbilityKit'; 4071 4072 @Entry 4073 @Component 4074 struct Index { 4075 context = this.getUIContext().getHostContext() as common.UIAbilityContext; // UIAbilityContext 4076 4077 onResultCallback(code: number, result: Record<string, Object>): void { 4078 console.log('resultCode: ' + code); 4079 console.log('result: ' + JSON.stringify(result)); 4080 } 4081 4082 onRequestRedirectedCallback(request: Want): void { 4083 let wantInfo: Want = { 4084 deviceId: '', 4085 bundleName: 'com.example.accountjsdemo', 4086 action: 'ohos.want.action.viewData', 4087 entities: ['entity.system.default'], 4088 } 4089 this.context.startAbility(wantInfo).then(() => { 4090 console.log('startAbility successfully'); 4091 }).catch((err: BusinessError) => { 4092 console.log('startAbility err: ' + JSON.stringify(err)); 4093 }) 4094 } 4095 4096 aboutToAppear(): void { 4097 appAccountManager.authenticate('LiSi', 'com.example.accountjsdemo', 'getSocialData', {}, { 4098 onResult: this.onResultCallback, 4099 onRequestRedirected: this.onRequestRedirectedCallback 4100 }); 4101 } 4102 build() {} 4103 } 4104 ``` 4105 4106### getOAuthToken<sup>(deprecated)</sup> 4107 4108getOAuthToken(name: string, owner: string, authType: string, callback: AsyncCallback<string>): void 4109 4110获取指定应用账号的特定鉴权类型的授权令牌。使用callback异步回调。 4111 4112> **说明:** 4113> 4114> 从 API version 8开始支持,从API version 9开始废弃。建议使用[getAuthToken](#getauthtoken9)替代。 4115 4116**系统能力:** SystemCapability.Account.AppAccount 4117 4118**参数:** 4119 4120| 参数名 | 类型 | 必填 | 说明 | 4121| -------- | --------------------------- | ---- | ----------- | 4122| name | string | 是 | 应用账号的名称。 | 4123| owner | string | 是 | 应用账号所有者的包名。 | 4124| authType | string | 是 | 鉴权类型。 | 4125| callback | AsyncCallback<string> | 是 | 回调函数。当获取成功时,err为null,data为授权令牌值;否则为错误对象。 | 4126 4127**示例:** 4128 4129 ```ts 4130 import { BusinessError } from '@kit.BasicServicesKit'; 4131 4132 appAccountManager.getOAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData', 4133 (err: BusinessError, data: string) => { 4134 console.log('getOAuthToken err: ' + JSON.stringify(err)); 4135 console.log('getOAuthToken token: ' + data); 4136 }); 4137 ``` 4138 4139### getOAuthToken<sup>(deprecated)</sup> 4140 4141getOAuthToken(name: string, owner: string, authType: string): Promise<string> 4142 4143获取指定应用账号的特定鉴权类型的授权令牌。使用Promise异步回调。 4144 4145> **说明:** 4146> 4147> 从 API version 8开始支持,从API version 9开始废弃。建议使用[getAuthToken](#getauthtoken9-1)替代。 4148 4149**系统能力:** SystemCapability.Account.AppAccount 4150 4151**参数:** 4152 4153| 参数名 | 类型 | 必填 | 说明 | 4154| -------- | ------ | ---- | ----------- | 4155| name | string | 是 | 应用账号的名称。 | 4156| owner | string | 是 | 应用账号所有者的包名。 | 4157| authType | string | 是 | 鉴权类型。 | 4158 4159**返回值:** 4160 4161| 类型 | 说明 | 4162| --------------------- | --------------------- | 4163| Promise<string> | Promise对象,返回授权令牌。 | 4164 4165**示例:** 4166 4167 ```ts 4168 import { BusinessError } from '@kit.BasicServicesKit'; 4169 4170 appAccountManager.getOAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData').then((data: string) => { 4171 console.log('getOAuthToken token: ' + data); 4172 }).catch((err: BusinessError) => { 4173 console.log('getOAuthToken err: ' + JSON.stringify(err)); 4174 }); 4175 ``` 4176 4177### setOAuthToken<sup>(deprecated)</sup> 4178 4179setOAuthToken(name: string, authType: string, token: string, callback: AsyncCallback<void>): void 4180 4181为指定应用账号设置特定鉴权类型的授权令牌。使用callback异步回调。 4182 4183> **说明:** 4184> 4185> 从 API version 8开始支持,从API version 9开始废弃。建议使用[setAuthToken](#setauthtoken9)替代。 4186 4187**系统能力:** SystemCapability.Account.AppAccount 4188 4189**参数:** 4190 4191| 参数名 | 类型 | 必填 | 说明 | 4192| -------- | ------------------------- | ---- | -------- | 4193| name | string | 是 | 应用账号的名称。 | 4194| authType | string | 是 | 鉴权类型。 | 4195| token | string | 是 | 授权令牌。 | 4196| callback | AsyncCallback<void> | 是 | 回调函数。当设置成功时,err为null;否则为错误对象。 | 4197 4198**示例:** 4199 4200 ```ts 4201 import { BusinessError } from '@kit.BasicServicesKit'; 4202 4203 appAccountManager.setOAuthToken('LiSi', 'getSocialData', 'xxxx', (err: BusinessError) => { 4204 console.log('setOAuthToken err: ' + JSON.stringify(err)); 4205 }); 4206 ``` 4207 4208### setOAuthToken<sup>(deprecated)</sup> 4209 4210setOAuthToken(name: string, authType: string, token: string): Promise<void> 4211 4212为指定应用账号设置特定鉴权类型的授权令牌。使用Promise异步回调。 4213 4214> **说明:** 4215> 4216> 从 API version 8开始支持,从API version 9开始废弃。建议使用[setAuthToken](#setauthtoken9-1)替代。 4217 4218**系统能力:** SystemCapability.Account.AppAccount 4219 4220**参数:** 4221 4222| 参数名 | 类型 | 必填 | 说明 | 4223| -------- | ------ | ---- | -------- | 4224| name | string | 是 | 应用账号的名称。 | 4225| authType | string | 是 | 鉴权类型。 | 4226| token | string | 是 | 授权令牌。 | 4227 4228**返回值:** 4229 4230| 类型 | 说明 | 4231| ------------------- | --------------------- | 4232| Promise<void> | 无返回结果的Promise对象。 | 4233 4234**示例:** 4235 4236 ```ts 4237 import { BusinessError } from '@kit.BasicServicesKit'; 4238 4239 appAccountManager.setOAuthToken('LiSi', 'getSocialData', 'xxxx').then(() => { 4240 console.log('setOAuthToken successfully'); 4241 }).catch((err: BusinessError) => { 4242 console.log('setOAuthToken err: ' + JSON.stringify(err)); 4243 }); 4244 ``` 4245 4246### deleteOAuthToken<sup>(deprecated)</sup> 4247 4248deleteOAuthToken(name: string, owner: string, authType: string, token: string, callback: AsyncCallback<void>): void 4249 4250删除指定应用账号的特定鉴权类型的授权令牌。使用callback异步回调。 4251 4252> **说明:** 4253> 4254> 从 API version 8开始支持,从API version 9开始废弃。建议使用[deleteAuthToken](#deleteauthtoken9)替代。 4255 4256**系统能力:** SystemCapability.Account.AppAccount 4257 4258**参数:** 4259 4260| 参数名 | 类型 | 必填 | 说明 | 4261| -------- | ------------------------- | ---- | ------------ | 4262| name | string | 是 | 应用账号的名称。 | 4263| owner | string | 是 | 应用账号所有者的包名。 | 4264| authType | string | 是 | 鉴权类型。 | 4265| token | string | 是 | 授权令牌。 | 4266| callback | AsyncCallback<void> | 是 | 回调函数。当删除成功时,err为null;否则为错误对象。 | 4267 4268**示例:** 4269 4270 ```ts 4271 import { BusinessError } from '@kit.BasicServicesKit'; 4272 4273 appAccountManager.deleteOAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData', 'xxxxx', 4274 (err: BusinessError) => { 4275 console.log('deleteOAuthToken err: ' + JSON.stringify(err)); 4276 }); 4277 ``` 4278 4279### deleteOAuthToken<sup>(deprecated)</sup> 4280 4281deleteOAuthToken(name: string, owner: string, authType: string, token: string): Promise<void> 4282 4283删除指定应用账号的特定鉴权类型的授权令牌。使用Promise异步回调。 4284 4285> **说明:** 4286> 4287> 从 API version 8开始支持,从API version 9开始废弃。建议使用[deleteAuthToken](#deleteauthtoken9-1)替代。 4288 4289**系统能力:** SystemCapability.Account.AppAccount 4290 4291**参数:** 4292 4293| 参数名 | 类型 | 必填 | 说明 | 4294| -------- | ------ | ---- | ------------ | 4295| name | string | 是 | 应用账号的名称。 | 4296| owner | string | 是 | 应用账号所有者的包名。 | 4297| authType | string | 是 | 鉴权类型。 | 4298| token | string | 是 | 授权令牌。 | 4299 4300**返回值:** 4301 4302| 类型 | 说明 | 4303| ------------------- | --------------------- | 4304| Promise<void> | 无返回结果的Promise对象。 | 4305 4306**示例:** 4307 4308 ```ts 4309 import { BusinessError } from '@kit.BasicServicesKit'; 4310 4311 appAccountManager.deleteOAuthToken('LiSi', 'com.example.accountjsdemo', 'getSocialData', 'xxxxx').then(() => { 4312 console.log('deleteOAuthToken successfully'); 4313 }).catch((err: BusinessError) => { 4314 console.log('deleteOAuthToken err: ' + JSON.stringify(err)); 4315 }); 4316 ``` 4317 4318### setOAuthTokenVisibility<sup>(deprecated)</sup> 4319 4320setOAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean, callback: AsyncCallback<void>): void 4321 4322设置指定账号的特定鉴权类型的授权令牌对指定应用的可见性。使用callback异步回调。 4323 4324> **说明:** 4325> 4326> 从 API version 8开始支持,从API version 9开始废弃。建议使用[setAuthTokenVisibility](#setauthtokenvisibility9)替代。 4327 4328**系统能力:** SystemCapability.Account.AppAccount 4329 4330**参数:** 4331 4332| 参数名 | 类型 | 必填 | 说明 | 4333| ---------- | ------------------------- | ---- | ------------------------- | 4334| name | string | 是 | 应用账号的名称。 | 4335| authType | string | 是 | 鉴权类型。 | 4336| bundleName | string | 是 | 被设置可见性的应用包名。 | 4337| isVisible | boolean | 是 | 是否可见。true表示可见,false表示不可见。 | 4338| callback | AsyncCallback<void> | 是 | 回调函数。当设置成功时,err为null;否则为错误对象。 | 4339 4340**示例:** 4341 4342 ```ts 4343 import { BusinessError } from '@kit.BasicServicesKit'; 4344 4345 appAccountManager.setOAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo', true, 4346 (err: BusinessError) => { 4347 console.log('setOAuthTokenVisibility err: ' + JSON.stringify(err)); 4348 }); 4349 ``` 4350 4351### setOAuthTokenVisibility<sup>(deprecated)</sup> 4352 4353setOAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean): Promise<void> 4354 4355设置指定账号的特定鉴权类型的授权令牌对指定应用的可见性。使用Promise异步回调。 4356 4357> **说明:** 4358> 4359> 从 API version 8开始支持,从API version 9开始废弃。建议使用[setAuthTokenVisibility](#setauthtokenvisibility9-1)替代。 4360 4361**系统能力:** SystemCapability.Account.AppAccount 4362 4363**参数:** 4364 4365| 参数名 | 类型 | 必填 | 说明 | 4366| ---------- | ------- | ---- | ------------ | 4367| name | string | 是 | 应用账号的名称。 | 4368| authType | string | 是 | 鉴权类型。 | 4369| bundleName | string | 是 | 被设置可见性的应用包名。 | 4370| isVisible | boolean | 是 | 是否可见。true表示可见,false表示不可见。 | 4371 4372**返回值:** 4373 4374| 类型 | 说明 | 4375| ------------------- | --------------------- | 4376| Promise<void> | 无返回结果的Promise对象。 | 4377 4378**示例:** 4379 4380 ```ts 4381 import { BusinessError } from '@kit.BasicServicesKit'; 4382 4383 appAccountManager.setOAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo', true).then(() => { 4384 console.log('setOAuthTokenVisibility successfully'); 4385 }).catch((err: BusinessError) => { 4386 console.log('setOAuthTokenVisibility err: ' + JSON.stringify(err)); 4387 }); 4388 ``` 4389 4390### checkOAuthTokenVisibility<sup>(deprecated)</sup> 4391 4392checkOAuthTokenVisibility(name: string, authType: string, bundleName: string, callback: AsyncCallback<boolean>): void 4393 4394检查指定应用账号的特定鉴权类型的授权令牌对指定应用的可见性。使用callback异步回调。 4395 4396> **说明:** 4397> 4398> 从 API version 8开始支持,从API version 9开始废弃。建议使用[checkAuthTokenVisibility](#checkauthtokenvisibility9)替代。 4399 4400**系统能力:** SystemCapability.Account.AppAccount 4401 4402**参数:** 4403 4404| 参数名 | 类型 | 必填 | 说明 | 4405| ---------- | ---------------------------- | ---- | ----------- | 4406| name | string | 是 | 应用账号的名称。 | 4407| authType | string | 是 | 鉴权类型。 | 4408| bundleName | string | 是 | 检查可见性的应用包名。 | 4409| callback | AsyncCallback<boolean> | 是 | 回调函数。当检查成功时,err为null,data为true表示可见,data为false表示不可见;否则为错误对象。 | 4410 4411**示例:** 4412 4413 ```ts 4414 import { BusinessError } from '@kit.BasicServicesKit'; 4415 4416 appAccountManager.checkOAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo', 4417 (err: BusinessError, data: boolean) => { 4418 console.log('checkOAuthTokenVisibility err: ' + JSON.stringify(err)); 4419 console.log('checkOAuthTokenVisibility isVisible: ' + data); 4420 }); 4421 ``` 4422 4423### checkOAuthTokenVisibility<sup>(deprecated)</sup> 4424 4425checkOAuthTokenVisibility(name: string, authType: string, bundleName: string): Promise<boolean> 4426 4427检查指定应用账号的特定鉴权类型的授权令牌对指定应用的可见性。使用Promise异步回调。 4428 4429> **说明:** 4430> 4431> 从 API version 8开始支持,从API version 9开始废弃。建议使用[checkAuthTokenVisibility](#checkauthtokenvisibility9-1)替代。 4432 4433**系统能力:** SystemCapability.Account.AppAccount 4434 4435**参数:** 4436 4437| 参数名 | 类型 | 必填 | 说明 | 4438| ---------- | ------ | ---- | ------------- | 4439| name | string | 是 | 应用账号的名称。 | 4440| authType | string | 是 | 鉴权类型。 | 4441| bundleName | string | 是 | 用于检查可见性的应用包名。 | 4442 4443**返回值:** 4444 4445| 类型 | 说明 | 4446| ---------------------- | --------------------- | 4447| Promise<boolean> | Promise对象。返回true表示指定鉴权类型的OAuth令牌对特定应用的可见,返回false表示不可见。 | 4448 4449**示例:** 4450 4451 ```ts 4452 import { BusinessError } from '@kit.BasicServicesKit'; 4453 4454 appAccountManager.checkOAuthTokenVisibility('LiSi', 'getSocialData', 'com.example.accountjsdemo').then(( 4455 data: boolean) => { 4456 console.log('checkOAuthTokenVisibility isVisible: ' + data); 4457 }).catch((err: BusinessError) => { 4458 console.log('checkOAuthTokenVisibility err: ' + JSON.stringify(err)); 4459 }); 4460 ``` 4461 4462### getAllOAuthTokens<sup>(deprecated)</sup> 4463 4464getAllOAuthTokens(name: string, owner: string, callback: AsyncCallback<Array<OAuthTokenInfo>>): void 4465 4466获取指定账号对调用方可见的所有授权令牌。使用callback异步回调。 4467 4468> **说明:** 4469> 4470> 从 API version 8开始支持,从API version 9开始废弃。建议使用[getAllAuthTokens](#getallauthtokens9)替代。 4471 4472**系统能力:** SystemCapability.Account.AppAccount 4473 4474**参数:** 4475 4476| 参数名 | 类型 | 必填 | 说明 | 4477| -------- | ---------------------------------------- | ---- | ----------- | 4478| name | string | 是 | 应用账号的名称。 | 4479| owner | string | 是 | 应用账号所有者的包名。 | 4480| callback | AsyncCallback<Array<[OAuthTokenInfo](#oauthtokeninfodeprecated)>> | 是 | 回调函数。当获取成功时,err为null,data为授权令牌数组;否则为错误对象。 | 4481 4482**示例:** 4483 4484 ```ts 4485 import { BusinessError } from '@kit.BasicServicesKit'; 4486 4487 appAccountManager.getAllOAuthTokens('LiSi', 'com.example.accountjsdemo', 4488 (err: BusinessError, data: appAccount.OAuthTokenInfo[]) => { 4489 console.log('getAllOAuthTokens err: ' + JSON.stringify(err)); 4490 console.log('getAllOAuthTokens data: ' + JSON.stringify(data)); 4491 }); 4492 ``` 4493 4494### getAllOAuthTokens<sup>(deprecated)</sup> 4495 4496getAllOAuthTokens(name: string, owner: string): Promise<Array<OAuthTokenInfo>> 4497 4498获取指定账号对调用方可见的所有授权令牌。使用Promise异步回调。 4499 4500> **说明:** 4501> 4502> 从 API version 8开始支持,从API version 9开始废弃。建议使用[getAllAuthTokens](#getallauthtokens9-1)替代。 4503 4504**系统能力:** SystemCapability.Account.AppAccount 4505 4506**参数:** 4507 4508| 参数名 | 类型 | 必填 | 说明 | 4509| ----- | ------ | ---- | ----------- | 4510| name | string | 是 | 应用账号的名称。 | 4511| owner | string | 是 | 应用账号所有者的包名。 | 4512 4513**返回值:** 4514 4515| 类型 | 说明 | 4516| ---------------------------------------- | --------------------- | 4517| Promise<Array< [OAuthTokenInfo](#oauthtokeninfodeprecated)>> | Promise对象,返回授权令牌数组。 | 4518 4519**示例:** 4520 4521 ```ts 4522 import { BusinessError } from '@kit.BasicServicesKit'; 4523 4524 appAccountManager.getAllOAuthTokens('LiSi', 'com.example.accountjsdemo').then(( 4525 data: appAccount.OAuthTokenInfo[]) => { 4526 console.log('getAllOAuthTokens data: ' + JSON.stringify(data)); 4527 }).catch((err: BusinessError) => { 4528 console.log('getAllOAuthTokens err: ' + JSON.stringify(err)); 4529 }); 4530 ``` 4531 4532### getOAuthList<sup>(deprecated)</sup> 4533 4534getOAuthList(name: string, authType: string, callback: AsyncCallback<Array<string>>): void 4535 4536获取指定应用账号的特定鉴权类型的授权列表,即被授权的包名数组(令牌的授权列表通过setOAuthTokenVisibility(#setoauthtokenvisibilitydeprecated)来设置)。使用callback异步回调。 4537 4538> **说明:** 4539> 4540> 从 API version 8开始支持,从API version 9开始废弃。建议使用[getAuthList](#getauthlist9)替代。 4541 4542**系统能力:** SystemCapability.Account.AppAccount 4543 4544**参数:** 4545 4546| 参数名 | 类型 | 必填 | 说明 | 4547| -------- | ---------------------------------------- | ---- | ----------------------- | 4548| name | string | 是 | 应用账号的名称。 | 4549| authType | string | 是 | 鉴权类型。 | 4550| callback | AsyncCallback<Array<string>> | 是 | 回调函数。当获取成功时,err为null,data为被授权的包名数组;否则为错误对象。 | 4551 4552**示例:** 4553 4554 ```ts 4555 import { BusinessError } from '@kit.BasicServicesKit'; 4556 4557 appAccountManager.getOAuthList('LiSi', 'getSocialData', (err: BusinessError, data: string[]) => { 4558 console.log('getOAuthList err: ' + JSON.stringify(err)); 4559 console.log('getOAuthList data: ' + JSON.stringify(data)); 4560 }); 4561 ``` 4562 4563### getOAuthList<sup>(deprecated)</sup> 4564 4565getOAuthList(name: string, authType: string): Promise<Array<string>> 4566 4567获取指定应用账号的特定鉴权类型的授权列表,即被授权的包名数组(令牌的授权列表通过setOAuthTokenVisibility(#setoauthtokenvisibilitydeprecated)来设置)。使用Promise异步回调。 4568 4569> **说明:** 4570> 4571> 从 API version 8开始支持,从API version 9开始废弃。建议使用[getAuthList](#getauthlist9-1)替代。 4572 4573**系统能力:** SystemCapability.Account.AppAccount 4574 4575**参数:** 4576 4577| 参数名 | 类型 | 必填 | 说明 | 4578| -------- | ------ | ---- | ----------------------- | 4579| name | string | 是 | 应用账号的名称。 | 4580| authType | string | 是 | 鉴权类型。 | 4581 4582**返回值:** 4583 4584| 类型 | 说明 | 4585| ---------------------------------- | --------------------- | 4586| Promise<Array<string>> | Promise对象,返回被授权的包名数组。 | 4587 4588**示例:** 4589 4590 ```ts 4591 import { BusinessError } from '@kit.BasicServicesKit'; 4592 4593 appAccountManager.getOAuthList('LiSi', 'getSocialData').then((data: string[]) => { 4594 console.log('getOAuthList data: ' + JSON.stringify(data)); 4595 }).catch((err: BusinessError) => { 4596 console.log('getOAuthList err: ' + JSON.stringify(err)); 4597 }); 4598 ``` 4599 4600### getAuthenticatorCallback<sup>(deprecated)</sup> 4601 4602getAuthenticatorCallback(sessionId: string, callback: AsyncCallback<AuthenticatorCallback>): void 4603 4604获取鉴权会话的认证器回调。使用callback异步回调。 4605 4606> **说明:** 4607> 4608> 从 API version 8开始支持,从API version 9开始废弃。建议使用[getAuthCallback](#getauthcallback9)替代。 4609 4610**系统能力:** SystemCapability.Account.AppAccount 4611 4612**参数:** 4613 4614| 参数名 | 类型 | 必填 | 说明 | 4615| --------- | ---------------------------------------- | ---- | -------- | 4616| sessionId | string | 是 | 鉴权会话的标识。 | 4617| callback | AsyncCallback<[AuthenticatorCallback](#authenticatorcallbackdeprecated)> | 是 | 回调函数。当获取鉴权会话的认证器回调函数成功时,err为null,data为认证器回调函数;否则为错误对象。 | 4618 4619**示例:** 4620 4621 ```ts 4622 import { BusinessError } from '@kit.BasicServicesKit'; 4623 import { Want, UIAbility, AbilityConstant } from '@kit.AbilityKit'; 4624 4625 export default class EntryAbility extends UIAbility { 4626 onCreate(want: Want, param: AbilityConstant.LaunchParam) { // ability 生命周期函数 4627 let sessionId: string = want.parameters![appAccount.Constants.KEY_SESSION_ID] as string; 4628 appAccountManager.getAuthenticatorCallback(sessionId, 4629 (err: BusinessError, callback: appAccount.AuthenticatorCallback) => { 4630 if (err.code != appAccount.ResultCode.SUCCESS) { 4631 console.log('getAuthenticatorCallback err: ' + JSON.stringify(err)); 4632 return; 4633 } 4634 callback.onResult(appAccount.ResultCode.SUCCESS, { 4635 name: 'LiSi', 4636 owner: 'com.example.accountjsdemo', 4637 authType: 'getSocialData', 4638 token: 'xxxxxx'} 4639 ); 4640 }); 4641 } 4642 } 4643 ``` 4644 4645### getAuthenticatorCallback<sup>(deprecated)</sup> 4646 4647getAuthenticatorCallback(sessionId: string): Promise<AuthenticatorCallback> 4648 4649获取鉴权会话的认证器回调。使用Promise异步回调。 4650 4651> **说明:** 4652> 4653> 从 API version 8开始支持,从API version 9开始废弃。建议使用[getAuthCallback](#getauthcallback9-1)替代。 4654 4655**系统能力:** SystemCapability.Account.AppAccount 4656 4657**参数:** 4658 4659| 参数名 | 类型 | 必填 | 说明 | 4660| --------- | ------ | ---- | -------- | 4661| sessionId | string | 是 | 鉴权会话的标识。 | 4662 4663**返回值:** 4664 4665| 类型 | 说明 | 4666| ------------------------------------ | --------------------- | 4667| Promise<[AuthenticatorCallback](#authenticatorcallbackdeprecated)> | Promise对象,返回鉴权会话的认证器回调对象。 | 4668 4669**示例:** 4670 4671 ```ts 4672 import { BusinessError } from '@kit.BasicServicesKit'; 4673 import { Want, UIAbility, AbilityConstant } from '@kit.AbilityKit'; 4674 4675 export default class EntryAbility extends UIAbility { 4676 onCreate(want: Want, param: AbilityConstant.LaunchParam) { // ability 生命周期函数 4677 let sessionId: string = want.parameters![appAccount.Constants.KEY_SESSION_ID] as string; 4678 appAccountManager.getAuthenticatorCallback(sessionId).then(( 4679 callback: appAccount.AuthenticatorCallback) => { 4680 callback.onResult(appAccount.ResultCode.SUCCESS, { 4681 name: 'LiSi', 4682 owner: 'com.example.accountjsdemo', 4683 authType: 'getSocialData', 4684 token: 'xxxxxx'} 4685 ); 4686 }).catch((err: BusinessError) => { 4687 console.log('getAuthenticatorCallback err: ' + JSON.stringify(err)); 4688 }); 4689 } 4690 } 4691 ``` 4692 4693### getAuthenticatorInfo<sup>(deprecated)</sup> 4694 4695getAuthenticatorInfo(owner: string, callback: AsyncCallback<AuthenticatorInfo>): void 4696 4697获取指定应用的认证器信息。使用callback异步回调。 4698 4699> **说明:** 4700> 4701> 从 API version 8开始支持,从API version 9开始废弃。建议使用[queryAuthenticatorInfo](#queryauthenticatorinfo9)替代。 4702 4703**系统能力:** SystemCapability.Account.AppAccount 4704 4705**参数:** 4706 4707| 参数名 | 类型 | 必填 | 说明 | 4708| -------- | -------------------------------------- | ---- | ----------- | 4709| owner | string | 是 | 应用账号所有者的包名。 | 4710| callback | AsyncCallback<[AuthenticatorInfo](#authenticatorinfo8)> | 是 | 回调函数。当获取成功时,err为null,data为认证器信息对象;否则为错误对象。 | 4711 4712**示例:** 4713 4714 ```ts 4715 import { BusinessError } from '@kit.BasicServicesKit'; 4716 4717 appAccountManager.getAuthenticatorInfo('com.example.accountjsdemo', 4718 (err: BusinessError, data: appAccount.AuthenticatorInfo) => { 4719 console.log('getAuthenticatorInfo err: ' + JSON.stringify(err)); 4720 console.log('getAuthenticatorInfo data: ' + JSON.stringify(data)); 4721 }); 4722 ``` 4723 4724### getAuthenticatorInfo<sup>(deprecated)</sup> 4725 4726getAuthenticatorInfo(owner: string): Promise<AuthenticatorInfo> 4727 4728获取指定应用的认证器信息。使用Promise异步回调。 4729 4730> **说明:** 4731> 4732> 从 API version 8开始支持,从API version 9开始废弃。建议使用[queryAuthenticatorInfo](#queryauthenticatorinfo9-1)替代。 4733 4734**系统能力:** SystemCapability.Account.AppAccount 4735 4736**参数:** 4737 4738| 参数名 | 类型 | 必填 | 说明 | 4739| ----- | ------ | ---- | ----------- | 4740| owner | string | 是 | 应用账号所有者的包名。 | 4741 4742**返回值:** 4743 4744| 类型 | 说明 | 4745| -------------------------------- | --------------------- | 4746| Promise<[AuthenticatorInfo](#authenticatorinfo8)> | Promise对象,返回指定应用的认证器信息对象。 | 4747 4748**示例:** 4749 4750 ```ts 4751 import { BusinessError } from '@kit.BasicServicesKit'; 4752 4753 appAccountManager.getAuthenticatorInfo('com.example.accountjsdemo').then(( 4754 data: appAccount.AuthenticatorInfo) => { 4755 console.log('getAuthenticatorInfo: ' + JSON.stringify(data)); 4756 }).catch((err: BusinessError) => { 4757 console.log('getAuthenticatorInfo err: ' + JSON.stringify(err)); 4758 }); 4759 ``` 4760 4761## AppAccountInfo 4762 4763表示应用账号信息。 4764 4765**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4766 4767| 名称 | 类型 | 必填 | 说明 | 4768| ----- | ------ | ---- | ----------- | 4769| owner | string | 是 | 应用账号所有者的包名。 | 4770| name | string | 是 | 应用账号的名称。 | 4771 4772## AuthTokenInfo<sup>9+</sup> 4773 4774表示Auth令牌信息。 4775 4776**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4777 4778| 名称 | 类型 | 必填 | 说明 | 4779| -------------------- | -------------- | ----- | ---------------- | 4780| authType<sup>9+</sup> | string | 是 | 令牌的鉴权类型。 | 4781| token<sup>9+</sup> | string | 是 | 令牌的取值。 | 4782| account<sup>9+</sup> | [AppAccountInfo](#appaccountinfo) | 否 | 令牌所属的账号信息,默认为空。| 4783 4784## OAuthTokenInfo<sup>(deprecated)</sup> 4785 4786表示OAuth令牌信息。 4787 4788> **说明:** 4789> 4790> 从 API version 8开始支持,从API version 9开始废弃。建议使用[AuthTokenInfo](#authtokeninfo9)替代。 4791 4792**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4793 4794| 名称 | 类型 | 必填 | 说明 | 4795| -------------------- | -------------- | ----- | ---------------- | 4796| authType | string | 是 | 令牌的鉴权类型。 | 4797| token | string | 是 | 令牌的取值。 | 4798 4799## AuthenticatorInfo<sup>8+</sup> 4800 4801表示OAuth认证器信息。 4802 4803**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4804 4805| 名称 | 类型 | 必填 | 说明 | 4806| ------- | ------ | ---- | ---------- | 4807| owner | string | 是 | 认证器的所有者的包名。 | 4808| iconId | number | 是 | 认证器的图标标识。 | 4809| labelId | number | 是 | 认证器的标签标识。 | 4810 4811## AuthResult<sup>9+</sup> 4812 4813表示认证结果信息。 4814 4815**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4816 4817| 名称 | 类型 | 必填 | 说明 | 4818| ------- | ------ | ---- | ---------- | 4819| account | [AppAccountInfo](#appaccountinfo) | 否 | 令牌所属的账号信息,默认为空。 | 4820| tokenInfo | [AuthTokenInfo](#authtokeninfo9) | 否 | 令牌信息,默认为空。 | 4821 4822## CreateAccountOptions<sup>9+</sup> 4823 4824表示创建账号的选项。 4825 4826**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4827 4828| 名称 | 类型 | 必填 | 说明 | 4829| ------- | ------ | ---- | ---------- | 4830| customData | Record<string, string> | 否 | 自定义数据,默认为空。 | 4831 4832## CreateAccountImplicitlyOptions<sup>9+</sup> 4833 4834表示隐式创建账号的选项。 4835 4836**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4837 4838| 名称 | 类型 | 必填 | 说明 | 4839| ------- | ------ | ---- | ---------- | 4840| requiredLabels | Array<string> | 否 | 所需的标签,默认为空。 | 4841| authType | string | 否 | 鉴权类型,默认为空。 | 4842| parameters | Record<string, Object> | 否 | 自定义参数对象,默认为空。 | 4843## SelectAccountsOptions<sup>9+</sup> 4844 4845表示用于选择账号的选项。 4846 4847**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4848 4849| 名称 | 类型 | 必填 | 说明 | 4850| --------------- | --------------------------- | ----- | ------------------- | 4851| allowedAccounts | Array<[AppAccountInfo](#appaccountinfo)> | 否 | 允许的账号数组,默认为空。 | 4852| allowedOwners | Array<string> | 否 | 允许的账号所有者数组,默认为空。 | 4853| requiredLabels | Array<string> | 否 | 认证器的标签标识,默认为空。 | 4854 4855## VerifyCredentialOptions<sup>9+</sup> 4856 4857表示用于验证凭据的选项。 4858 4859**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4860 4861| 名称 | 类型 | 必填 | 说明 | 4862| -------------- | ---------------------- | ----- | -------------- | 4863| credentialType | string | 否 | 凭据类型,默认为空。 | 4864| credential | string | 否 | 凭据取值,默认为空。 | 4865| parameters | Record<string, Object> | 否 | 自定义参数对象,默认为空。 | 4866 4867 4868## SetPropertiesOptions<sup>9+</sup> 4869 4870表示用于设置属性的选项。 4871 4872**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4873 4874| 名称 | 类型 | 必填 | 说明 | 4875| ---------- | ---------------------- | ----- | -------------- | 4876| properties | Record<string, Object> | 否 | 属性对象,默认为空。 | 4877| parameters | Record<string, Object> | 否 | 自定义参数对象,默认为空。 | 4878 4879## Constants<sup>8+</sup> 4880 4881表示常量的枚举。 4882 4883**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4884 4885| 名称 | 值 | 说明 | 4886| -------------------------------- | ---------------------- | ----------------------- | 4887| ACTION_ADD_ACCOUNT_IMPLICITLY<sup>(deprecated)</sup> | 'addAccountImplicitly' | 表示操作,隐式添加账号。 | 4888| ACTION_AUTHENTICATE<sup>(deprecated)</sup> | 'authenticate' | 表示操作,鉴权。 | 4889| ACTION_CREATE_ACCOUNT_IMPLICITLY<sup>9+</sup> | 'createAccountImplicitly' | 表示操作,隐式创建账号。 | 4890| ACTION_AUTH<sup>9+</sup> | 'auth' | 表示操作,鉴权。 | 4891| ACTION_VERIFY_CREDENTIAL<sup>9+</sup> | 'verifyCredential' | 表示操作,验证凭据。 | 4892| ACTION_SET_AUTHENTICATOR_PROPERTIES<sup>9+</sup> | 'setAuthenticatorProperties' | 表示操作,设置认证器属性。 | 4893| KEY_NAME | 'name' | 表示键名,应用账号的名称。 | 4894| KEY_OWNER | 'owner' | 表示键名,应用账号所有者的包名。| 4895| KEY_TOKEN | 'token' | 表示键名,令牌。 | 4896| KEY_ACTION | 'action' | 表示键名,操作。 | 4897| KEY_AUTH_TYPE | 'authType' | 表示键名,鉴权类型。 | 4898| KEY_SESSION_ID | 'sessionId' | 表示键名,会话标识。 | 4899| KEY_CALLER_PID | 'callerPid' | 表示键名,调用方PID。 | 4900| KEY_CALLER_UID | 'callerUid' | 表示键名,调用方UID。 | 4901| KEY_CALLER_BUNDLE_NAME | 'callerBundleName' | 表示键名,调用方包名。 | 4902| KEY_REQUIRED_LABELS<sup>9+</sup> | 'requiredLabels' | 表示键名,必需的标签。 | 4903| KEY_BOOLEAN_RESULT<sup>9+</sup> | 'booleanResult' | 表示键名,布尔返回值。 | 4904 4905## ResultCode<sup>(deprecated)</sup> 4906 4907表示返回码的枚举。 4908 4909> **说明:**<br/> 4910> 从API version 8开始支持,从API version 9开始废弃。相关信息建议查看[错误码文档](errorcode-account.md)替代。 4911 4912**系统能力:** 以下各项对应的系统能力均为SystemCapability.Account.AppAccount。 4913 4914| 名称 | 值 | 说明 | 4915| ----------------------------------- | ----- | ------------ | 4916| SUCCESS | 0 | 表示操作成功。 | 4917| ERROR_ACCOUNT_NOT_EXIST | 10001 | 表示应用账号不存在。 | 4918| ERROR_APP_ACCOUNT_SERVICE_EXCEPTION | 10002 | 表示应用账号服务异常。 | 4919| ERROR_INVALID_PASSWORD | 10003 | 表示密码无效。 | 4920| ERROR_INVALID_REQUEST | 10004 | 表示请求无效。 | 4921| ERROR_INVALID_RESPONSE | 10005 | 表示响应无效。 | 4922| ERROR_NETWORK_EXCEPTION | 10006 | 表示网络异常。 | 4923| ERROR_OAUTH_AUTHENTICATOR_NOT_EXIST | 10007 | 表示认证器不存在。 | 4924| ERROR_OAUTH_CANCELED | 10008 | 表示鉴权取消。 | 4925| ERROR_OAUTH_LIST_TOO_LARGE | 10009 | 表示开放授权列表过大。 | 4926| ERROR_OAUTH_SERVICE_BUSY | 10010 | 表示开放授权服务忙碌。 | 4927| ERROR_OAUTH_SERVICE_EXCEPTION | 10011 | 表示开放授权服务异常。 | 4928| ERROR_OAUTH_SESSION_NOT_EXIST | 10012 | 表示鉴权会话不存在。 | 4929| ERROR_OAUTH_TIMEOUT | 10013 | 表示鉴权超时。 | 4930| ERROR_OAUTH_TOKEN_NOT_EXIST | 10014 | 表示开放授权令牌不存在。 | 4931| ERROR_OAUTH_TOKEN_TOO_MANY | 10015 | 表示开放授权令牌过多。 | 4932| ERROR_OAUTH_UNSUPPORT_ACTION | 10016 | 表示不支持的鉴权操作。 | 4933| ERROR_OAUTH_UNSUPPORT_AUTH_TYPE | 10017 | 表示不支持的鉴权类型。 | 4934| ERROR_PERMISSION_DENIED | 10018 | 表示权限不足。 | 4935 4936## AuthCallback<sup>9+</sup> 4937 4938认证器回调类。 4939 4940### onResult<sup>9+</sup> 4941 4942onResult: (code: number, result?: AuthResult) => void 4943 4944通知请求结果。 4945 4946**系统能力:** SystemCapability.Account.AppAccount 4947 4948**参数:** 4949 4950| 参数名 | 类型 | 必填 | 说明 | 4951| ------ | -------------------- | ---- | ------ | 4952| code | number | 是 | 鉴权结果码。 | 4953| result | [AuthResult](#authresult9) | 否 | 鉴权结果,默认为空,表示不接收认证结果信息。 | 4954 4955**示例:** 4956 4957 ```ts 4958 import { BusinessError } from '@kit.BasicServicesKit'; 4959 4960 let appAccountManager: appAccount.AppAccountManager = appAccount.createAppAccountManager(); 4961 let sessionId = '1234'; 4962 appAccountManager.getAuthCallback(sessionId).then((callback: appAccount.AuthCallback) => { 4963 let result: appAccount.AuthResult = { 4964 account: { 4965 name: 'Lisi', 4966 owner: 'com.example.accountjsdemo', 4967 }, 4968 tokenInfo: { 4969 token: 'xxxxxx', 4970 authType: 'getSocialData' 4971 } 4972 }; 4973 callback.onResult(appAccount.ResultCode.SUCCESS, result); 4974 }).catch((err: BusinessError) => { 4975 console.log('getAuthCallback err: ' + JSON.stringify(err)); 4976 }); 4977 ``` 4978 4979### onRequestRedirected<sup>9+</sup> 4980 4981onRequestRedirected: (request: Want) => void 4982 4983通知请求被跳转。 4984 4985**系统能力:** SystemCapability.Account.AppAccount 4986 4987**参数:** 4988 4989| 参数名 | 类型 | 必填 | 说明 | 4990| ------- | ---- | ---- | ---------- | 4991| request | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 用于跳转的请求信息。 | 4992 4993**示例:** 4994 4995 ```ts 4996 import { Want } from '@kit.AbilityKit'; 4997 4998 class MyAuthenticator extends appAccount.Authenticator { 4999 createAccountImplicitly( 5000 options: appAccount.CreateAccountImplicitlyOptions, callback: appAccount.AuthCallback) { 5001 let want: Want = { 5002 bundleName: 'com.example.accountjsdemo', 5003 abilityName: 'com.example.accountjsdemo.LoginAbility', 5004 }; 5005 callback.onRequestRedirected(want); 5006 } 5007 5008 auth(name: string, authType: string, 5009 options: Record<string, Object>, callback: appAccount.AuthCallback) { 5010 let result: appAccount.AuthResult = { 5011 account: { 5012 name: 'Lisi', 5013 owner: 'com.example.accountjsdemo', 5014 }, 5015 tokenInfo: { 5016 token: 'xxxxxx', 5017 authType: 'getSocialData' 5018 } 5019 }; 5020 callback.onResult(appAccount.ResultCode.SUCCESS, result); 5021 } 5022 } 5023 ``` 5024 5025### onRequestContinued<sup>9+</sup> 5026 5027onRequestContinued?: () => void 5028 5029通知请求被继续处理。 5030 5031**系统能力:** SystemCapability.Account.AppAccount 5032 5033**示例:** 5034 5035 ```ts 5036 import { BusinessError } from '@kit.BasicServicesKit'; 5037 5038 let appAccountManager: appAccount.AppAccountManager = appAccount.createAppAccountManager(); 5039 let sessionId = '1234'; 5040 appAccountManager.getAuthCallback(sessionId).then((callback: appAccount.AuthCallback) => { 5041 if (callback.onRequestContinued != undefined) { 5042 callback.onRequestContinued(); 5043 } 5044 }).catch((err: BusinessError) => { 5045 console.log('getAuthCallback err: ' + JSON.stringify(err)); 5046 }); 5047 ``` 5048 5049## AuthenticatorCallback<sup>(deprecated)</sup> 5050 5051OAuth认证器回调接口。 5052 5053> **说明:** 5054> 5055> 从 API version 8开始支持,从API version 9开始废弃。建议使用[AuthCallback](#authcallback9)替代。 5056 5057### onResult<sup>8+</sup> 5058 5059onResult: (code: number, result: {[key: string]: any;}) => void 5060 5061通知请求结果。 5062 5063**系统能力:** SystemCapability.Account.AppAccount 5064 5065**参数:** 5066 5067| 参数名 | 类型 | 必填 | 说明 | 5068| ------ | -------------------- | ---- | ------ | 5069| code | number | 是 | 鉴权结果码。 | 5070| result | {[key: string]: any} | 是 | 鉴权结果。 | 5071 5072**示例:** 5073 5074 ```ts 5075 import { BusinessError } from '@kit.BasicServicesKit'; 5076 5077 let appAccountManager: appAccount.AppAccountManager = appAccount.createAppAccountManager(); 5078 let sessionId = '1234'; 5079 appAccountManager.getAuthenticatorCallback(sessionId).then((callback: appAccount.AuthenticatorCallback) => { 5080 callback.onResult(appAccount.ResultCode.SUCCESS, { 5081 name: 'LiSi', 5082 owner: 'com.example.accountjsdemo', 5083 authType: 'getSocialData', 5084 token: 'xxxxxx'} 5085 ); 5086 }).catch((err: BusinessError) => { 5087 console.log('getAuthenticatorCallback err: ' + JSON.stringify(err)); 5088 }); 5089 ``` 5090 5091### onRequestRedirected<sup>8+</sup> 5092 5093onRequestRedirected: (request: Want) => void 5094 5095通知请求被跳转。 5096 5097**系统能力:** SystemCapability.Account.AppAccount 5098 5099**参数:** 5100 5101| 参数名 | 类型 | 必填 | 说明 | 5102| ------- | ---- | ---- | ---------- | 5103| request | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 用于跳转的请求信息。 | 5104 5105**示例:** 5106 5107 ```ts 5108 import { Want } from '@kit.AbilityKit'; 5109 5110 class MyAuthenticator extends appAccount.Authenticator { 5111 addAccountImplicitly(authType: string, callerBundleName: string, 5112 options: Record<string, Object>, callback: appAccount.AuthenticatorCallback) { 5113 let want: Want = { 5114 bundleName: 'com.example.accountjsdemo', 5115 abilityName: 'com.example.accountjsdemo.LoginAbility', 5116 }; 5117 callback.onRequestRedirected(want); 5118 } 5119 5120 authenticate(name: string, authType: string, callerBundleName: string, 5121 options: Record<string, Object>, callback: appAccount.AuthenticatorCallback) { 5122 callback.onResult(appAccount.ResultCode.SUCCESS, { 5123 name: name, 5124 authType: authType, 5125 token: 'xxxxxx'} 5126 ); 5127 } 5128 } 5129 ``` 5130 5131## Authenticator<sup>8+</sup> 5132 5133认证器基类。 5134 5135### createAccountImplicitly<sup>9+</sup> 5136 5137createAccountImplicitly(options: CreateAccountImplicitlyOptions, callback: AuthCallback): void 5138 5139根据指定的账号所有者隐式地创建应用账号,并使用callback异步回调返回结果。 5140 5141**系统能力:** SystemCapability.Account.AppAccount 5142 5143**参数:** 5144 5145| 参数名 | 类型 | 必填 | 说明 | 5146| ---------------- | --------------------- | ---- | --------------- | 5147| options | [CreateAccountImplicitlyOptions](#createaccountimplicitlyoptions9) | 是 | 隐式创建账号的选项。 | 5148| callback | [AuthCallback](#authcallback9) | 是 | 认证器回调对象,用于返回创建结果。 | 5149 5150### addAccountImplicitly<sup>(deprecated)</sup> 5151 5152addAccountImplicitly(authType: string, callerBundleName: string, options: {[key: string]: any;}, callback: AuthenticatorCallback): void 5153 5154根据指定的鉴权类型和可选项,隐式地添加应用账号,并使用callback异步回调返回结果。 5155 5156> **说明:** 5157> 5158> 从 API version 8开始支持, 从API version 9开始废弃。建议使用[createAccountImplicitly](#createaccountimplicitly9-2)替代。 5159 5160**系统能力:** SystemCapability.Account.AppAccount 5161 5162**参数:** 5163 5164| 参数名 | 类型 | 必填 | 说明 | 5165| ---------------- | --------------------- | ---- | --------------- | 5166| authType | string | 是 | 应用账号的鉴权类型。 | 5167| callerBundleName | string | 是 | 鉴权请求方的包名。 | 5168| options | {[key: string]: any} | 是 | 鉴权所需要的可选项。 | 5169| callback | [AuthenticatorCallback](#authenticatorcallbackdeprecated) | 是 | 认证器回调,用于返回鉴权结果。 | 5170 5171### auth<sup>9+</sup> 5172 5173auth(name: string, authType: string, options: Record<string, Object>, callback: AuthCallback): void 5174 5175对应用账号进行鉴权以获取授权令牌,并使用callback异步回调返回结果。 5176 5177**系统能力:** SystemCapability.Account.AppAccount 5178 5179**参数:** 5180 5181| 参数名 | 类型 | 必填 | 说明 | 5182| ---------------- | --------------------- | ---- | --------------- | 5183| name | string | 是 | 应用账号的名称。 | 5184| authType | string | 是 | 应用账号的鉴权类型。 | 5185| options | Record<string, Object> | 是 | 鉴权所需要的可选项。 | 5186| callback | [AuthCallback](#authcallback9) | 是 | 回调对象,用于返回鉴权结果。 | 5187 5188### authenticate<sup>(deprecated)</sup> 5189 5190authenticate(name: string, authType: string, callerBundleName: string, options: {[key: string]: any;}, callback: AuthenticatorCallback): void 5191 5192对应用账号进行鉴权,获取OAuth令牌,并使用callback异步回调返回结果。 5193 5194> **说明:** 5195> 5196> 从 API version 8开始支持, 从API version 9开始废弃。建议使用[auth](#auth9-2)替代。 5197 5198**系统能力:** SystemCapability.Account.AppAccount 5199 5200**参数:** 5201 5202| 参数名 | 类型 | 必填 | 说明 | 5203| ---------------- | --------------------- | ---- | --------------- | 5204| name | string | 是 | 应用账号的名称。 | 5205| authType | string | 是 | 应用账号的鉴权类型。 | 5206| callerBundleName | string | 是 | 鉴权请求方的包名。 | 5207| options | {[key: string]: any} | 是 | 鉴权所需要的可选项。 | 5208| callback | [AuthenticatorCallback](#authenticatorcallbackdeprecated) | 是 | 认证器回调,用于返回鉴权结果。 | 5209 5210### verifyCredential<sup>9+</sup> 5211 5212verifyCredential(name: string, options: VerifyCredentialOptions, callback: AuthCallback): void 5213 5214验证应用账号的凭据,并使用callback异步回调返回结果。 5215 5216**系统能力:** SystemCapability.Account.AppAccount 5217 5218**参数:** 5219 5220| 参数名 | 类型 | 必填 | 说明 | 5221| ---------------- | --------------------- | ---- | --------------- | 5222| name | string | 是 | 应用账号的名称。 | 5223| options | [VerifyCredentialOptions](#verifycredentialoptions9) | 是 | 验证凭据的可选项。 | 5224| callback | [AuthCallback](#authcallback9) | 是 | 认证器回调,用于返回验证结果。 | 5225 5226### setProperties<sup>9+</sup> 5227 5228setProperties(options: SetPropertiesOptions, callback: AuthCallback): void 5229 5230设置认证器属性,并使用callback异步回调返回结果。 5231 5232**系统能力:** SystemCapability.Account.AppAccount 5233 5234**参数:** 5235 5236| 参数名 | 类型 | 必填 | 说明 | 5237| ---------------- | --------------------- | ---- | --------------- | 5238| options | [SetPropertiesOptions](#setpropertiesoptions9) | 是 | 设置属性的可选项。 | 5239| callback | [AuthCallback](#authcallback9) | 是 | 认证器回调,用于返回设置结果。 | 5240 5241### checkAccountLabels<sup>9+</sup> 5242 5243checkAccountLabels(name: string, labels: Array<string>, callback: AuthCallback): void 5244 5245检查账号标签,并使用callback异步回调返回结果。 5246 5247**系统能力:** SystemCapability.Account.AppAccount 5248 5249**参数:** 5250 5251| 参数名 | 类型 | 必填 | 说明 | 5252| ---------------- | --------------------- | ---- | --------------- | 5253| name | string | 是 | 应用账号的名称。 | 5254| labels | Array<string> | 是 | 标签数组。 | 5255| callback | [AuthCallback](#authcallback9) | 是 | 认证器回调,用于返回检查结果。 | 5256 5257### checkAccountRemovable<sup>9+</sup> 5258 5259checkAccountRemovable(name: string, callback: AuthCallback): void 5260 5261判断账号是否可以删除,并使用callback异步回调返回结果。 5262 5263**系统能力:** SystemCapability.Account.AppAccount 5264 5265**参数:** 5266 5267| 参数名 | 类型 | 必填 | 说明 | 5268| ---------------- | --------------------- | ---- | --------------- | 5269| name | string | 是 | 应用账号的名称。 | 5270| callback | [AuthCallback](#authcallback9) | 是 | 认证器回调,用于返回判断结果。 | 5271 5272### getRemoteObject<sup>9+</sup> 5273 5274getRemoteObject(): rpc.RemoteObject; 5275 5276获取认证器的远程对象,不可以重载实现。 5277 5278**系统能力:** SystemCapability.Account.AppAccount 5279 5280**示例:** 5281 5282 <!--code_no_check--> 5283 ```ts 5284 import { rpc } from '@kit.IPCKit'; 5285 import { Want } from '@kit.AbilityKit'; 5286 5287 class MyAuthenticator extends appAccount.Authenticator { 5288 verifyCredential(name: string, 5289 options: appAccount.VerifyCredentialOptions, callback: appAccount.AuthCallback) { 5290 let want: Want = { 5291 bundleName: 'com.example.accountjsdemo', 5292 abilityName: 'com.example.accountjsdemo.VerifyAbility', 5293 parameters: { 5294 name: name 5295 } 5296 }; 5297 callback.onRequestRedirected(want); 5298 } 5299 5300 setProperties(options: appAccount.SetPropertiesOptions, callback: appAccount.AuthCallback) { 5301 let want: Want = { 5302 bundleName: 'com.example.accountjsdemo', 5303 abilityName: 'com.example.accountjsdemo.SetPropertiesAbility', 5304 parameters: { 5305 options: options 5306 } 5307 }; 5308 callback.onRequestRedirected(want); 5309 } 5310 5311 checkAccountLabels(name: string, labels: string[], callback: appAccount.AuthCallback) { 5312 callback.onResult(0); 5313 } 5314 5315 checkAccountRemovable(name: string, callback: appAccount.AuthCallback) { 5316 callback.onResult(0); 5317 } 5318 } 5319 5320 export default { 5321 onConnect(want: Want): rpc.RemoteObject { // serviceAbility 生命周期函数, 需要放在serviceAbility中 5322 let authenticator = new MyAuthenticator(); 5323 return authenticator.getRemoteObject(); 5324 } 5325 } 5326 ``` 5327