diff --git a/zh-cn/application-dev/reference/apis/js-apis-appAccount.md b/zh-cn/application-dev/reference/apis/js-apis-appAccount.md index 537dc263d18ec8b7359b849a063d4f133774a305..38e22506315711bdc156e56523ad76a3998d0ac8 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-appAccount.md +++ b/zh-cn/application-dev/reference/apis/js-apis-appAccount.md @@ -11,6 +11,11 @@ import account_appAccount from '@ohos.account.appAccount'; ``` +## 系统能力 + +SystemCapability.Account.AppAccount + + ## account_appAccount.createAppAccountManager createAppAccountManager(): AppAccountManager; @@ -114,6 +119,47 @@ addAccount(name: string, extraInfo?: string): Promise<void>; }); ``` +### addAccountImplicitly8+ + +addAccountImplicitly(owner: string, authType: string, options: {[key: string]: any}, callback: AuthenticatorCallback): void; + +根据指定的帐号所有者、鉴权类型和可选项,隐式地添加应用帐号,并使用callback回调异步返回结果。 + +需要权限:无。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | --------------------- | --- | -------------------------- | + | owner | string | 是 | 要添加的应用帐户的所有者包名。 | + | authType | string | 是 | 要添加的应用帐户的鉴权类型。 | + | options | {[key: string]: any} | 是 | 鉴权所需要的可选项。 | + | callback | AuthenticatorCallback | 是 | 认证器回调,用于返回鉴权结果。 | + +- 示例: + + ``` + import featureAbility from '@ohos.ability.featureAbility'; + + function onResultCallback(code, result) { + console.log("resultCode: " + code); + console.log("result: " + JSON.stringify(result)); + } + + function onRequestRedirectedCallback(request) { + let abilityStartSetting = {want: request}; + featureAbility.startAbility(abilityStartSetting, (err)=>{ + console.log("startAbility err: " + JSON.stringify(err)); + }); + } + + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.addAccountImplicitly("LiSi", "readAge", {}, { + onResult: onResultCallback, + onRequestRedirected: onRequestRedirectedCallback + }); + ``` + ### deleteAccount deleteAccount(name: string, callback: AsyncCallback<void>): void; @@ -872,12 +918,739 @@ off(type: 'change', callback?: Callback<void>): void; } ``` +### authenticate8+ + +authenticate(name: string, owner: string, authType: string, options: {[key: string]: any}, callback: AuthenticatorCallback): void; + +鉴权应用帐户以获取OAuth令牌,使用callback回调异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | --------------------- | ---- | --------------------------- | + | name | string | 是 | 要鉴权的应用帐户的名称。 | + | owner | string | 是 | 要鉴权的应用帐户的所有者包名。 | + | authType | string | 是 | 鉴权类型。 | + | options | {[key: string]: any} | 是 | 鉴权所需的可选项。 | + | callback | AuthenticatorCallback | 是 | 认证器回调,用于返回鉴权结果。 | + +- 示例: + + ``` + import featureAbility from '@ohos.ability.featureAbility'; + + function onResultCallback(code, result) { + console.log("resultCode: " + code); + console.log("result: " + JSON.stringify(result)); + } + + function onRequestRedirectedCallback(request) { + let abilityStartSetting = {want: request}; + featureAbility.startAbility(abilityStartSetting, (err)=>{ + console.log("startAbility err: " + JSON.stringify(err)); + }); + } + + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.authenticate("LiSi", "com.example.ohos.accountjsdemo", "readAge", {}, { + onResult: onResultCallback, + onRequestRedirected: onRequestRedirectedCallback + }); + ``` + +### getOAuthToken8+ + +getOAuthToken(name: string, owner: string, authType: string, callback: AsyncCallback<string>): void; + +获取指定应用帐户和鉴权类型的OAuth令牌,使用callback回调异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | --------------------------- | ---- | -------------------- | + | name | string | 是 | 应用帐户的名称。 | + | owner | string | 是 | 应用帐户的所有者包名。 | + | authType | string | 是 | 鉴权类型。 | + | callback | AsyncCallback<string> | 是 | 查询结果的回调。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.getOAuthToken("LiSi", "com.example.ohos.accountjsdemo", "readAge", (err, data) => { + console.log('getOAuthToken err: ' + JSON.stringify(err)); + console.log('getOAuthToken token: ' + data); + }); + ``` + +### getOAuthToken8+ + +getOAuthToken(name: string, owner: string, authType: string): Promise<string>; + +获取指定应用帐户和鉴权类型的OAuth令牌,使用Promise方式异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------ | ---- | -------------------- | + | name | string | 是 | 应用帐户的名称。 | + | owner | string | 是 | 应用帐户的所有者包名。 | + | authType | string | 是 | 鉴权类型。 | + +- 参数: + + | 类型 | 说明 | + | --------------------- | -------------------------------- | + | Promise<string> | Promise实例,用于获取异步返回结果。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.getOAuthToken("LiSi", "com.example.ohos.accountjsdemo", "readAge").then((data) => { + console.log('getOAuthToken token: ' + data); + }).catch((err) => { + console.log("getOAuthToken err: " + JSON.stringify(err)); + }); + ``` + +### setOAuthToken8+ + +setOAuthToken(name: string, authType: string, token: string, callback: AsyncCallback<void>): void; + +设置指定应用帐户和鉴权类型的OAuth令牌,使用callback回调异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------------------------- | ---- | ------------- | + | name | string | 是 | 应用帐户的名称。 | + | authType | string | 是 | 鉴权类型。 | + | token | string | 是 | OAuth令牌。 | + | callback | AsyncCallback<void> | 是 | 设置结果的回调。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.setOAuthToken("LiSi", "readAge", "xxxx", (err) => { + console.log('setOAuthToken err: ' + JSON.stringify(err)); + }); + ``` + +### setOAuthToken8+ + +setOAuthToken(name: string, authType: string, token: string): Promise<void>; + +设置指定应用帐户和鉴权类型的OAuth令牌,使用Promise方式异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------ | ---- | ------------- | + | name | string | 是 | 应用帐户的名称。 | + | authType | string | 是 | 鉴权类型。 | + | token | string | 是 | OAuth令牌。 | + +- 参数: + + | 类型 | 说明 | + | ------------------- | -------------------------------- | + | Promise<void> | Promise实例,用于获取异步返回结果。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.setOAuthToken("LiSi", "readAge", "xxxx").then(() => { + console.log('setOAuthToken successfully'); + }).catch((err) => { + console.log('setOAuthToken err: ' + JSON.stringify(err)); + }); + ``` + +### deleteOAuthToken8+ + +deleteOAuthToken(name: string, owner: string, authType: string, token: string, callback: AsyncCallback<void>): void; + +删除指定应用帐户和鉴权类型的OAuth令牌,使用callback回调异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------------------------- | ---- | ------------------ | + | name | string | 是 | 应用帐户的名称。 | + | owner | string | 是 | 应用帐户的所有者包名。 | + | authType | string | 是 | 鉴权类型。 | + | token | string | 是 | 要删除的OAuth令牌。 | + | callback | AsyncCallback<void> | 是 | 删除结果的回调。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.deleteOAuthToken("LiSi", "com.example.ohos.accountjsdemo", "readAge", "xxxxx", (err) => { + console.log('deleteOAuthToken err: ' + JSON.stringify(err)); + }); + ``` + +### deleteOAuthToken8+ + +deleteOAuthToken(name: string, owner: string, authType: string, token: string): Promise<void>; + +删除指定应用帐户和鉴权类型的OAuth令牌,使用Promise方式异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------ | ---- | ------------------ | + | name | string | 是 | 应用帐户的名称。 | + | owner | string | 是 | 应用帐户的所有者包名。 | + | authType | string | 是 | 鉴权类型。 | + | token | string | 是 | 要删除的OAuth令牌。 | + +- 参数: + + | 类型 | 说明 | + | ------------------------------ | --------------------- | + | Promise<void> | Promise实例,用于获取异步返回结果。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.deleteOAuthToken("LiSi", "com.example.ohos.accountjsdemo", "readAge", "xxxxx").then(() => { + console.log('deleteOAuthToken successfully'); + }).catch((err) => { + console.log("deleteOAuthToken err: " + JSON.stringify(err)); + }); + ``` + +### setOAuthTokenVisibility8+ + +setOAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean, callback: AsyncCallback<void>): void; + +设置指定鉴权类型的OAuth令牌对特定应用的可见性,使用callback回调异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | ---------- | ------------------------- | ---- | ------------------- | + | name | string | 是 | 应用帐户的名称。 | + | authType | string | 是 | 鉴权类型。 | + | bundleName | string | 是 | 被设置可见性的应用包名。| + | isVisible | boolean | 是 | 是否可见。 | + | callback | AsyncCallback<void> | 是 | 设置结果的回调。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.setOAuthTokenVisibility("LiSi", "readAge", "com.example.ohos.accountjsdemo", true, (err) => { + console.log('setOAuthTokenVisibility err: ' + JSON.stringify(err)); + }); + ``` + +### setOAuthTokenVisibility8+ + +setOAuthTokenVisibility(name: string, authType: string, bundleName: string, isVisible: boolean): Promise<void>; + +设置指定鉴权类型的OAuth令牌对特定应用的可见性,使用Promise方式异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | ---------- | ------------------------- | ---- | ------------------- | + | name | string | 是 | 应用帐户的名称。 | + | authType | string | 是 | 鉴权类型。 | + | bundleName | string | 是 | 被设置可见性的应用包名。| + | isVisible | boolean | 是 | 是否可见。 | + +- 参数: + + | 类型 | 说明 | + | ------------------------------ | --------------------- | + | Promise<void> | Promise实例,用于获取异步返回结果。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.setOAuthTokenVisibility("LiSi", "readAge", "com.example.ohos.accountjsdemo", true).then(() => { + console.log('setOAuthTokenVisibility successfully'); + }).catch((err) => { + console.log('setOAuthTokenVisibility err: ' + JSON.stringify(err)); + }); + ``` + +### checkOAuthTokenVisibility8+ + +checkOAuthTokenVisibility(name: string, authType: string, bundleName: string, callback: AsyncCallback<boolean>): void; + +检查指定鉴权类型的OAuth令牌对特定应用的可见性,使用callback回调异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | ---------- | ---------------------------- | ---- | ---------------------- | + | name | string | 是 | 应用帐户的名称。 | + | authType | string | 是 | 鉴权类型。 | + | bundleName | string | 是 | 用于检查可见性的应用包名。 | + | callback | AsyncCallback<boolean> | 是 | 检查结果的回调。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.checkOAuthTokenVisibility("LiSi", "readAge", "com.example.ohos.accountjsdemo", true, (err, data) => { + console.log('checkOAuthTokenVisibility err: ' + JSON.stringify(err)); + console.log('checkOAuthTokenVisibility isVisible: ' + data); + }); + ``` + +### checkOAuthTokenVisibility8+ + +checkOAuthTokenVisibility(name: string, authType: string, bundleName: string): Promise<boolean>; + +检查指定鉴权类型的OAuth令牌对特定应用的可见性,使用Promise方式异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | ---------- | ------------------------- | ---- | ---------------------- | + | name | string | 是 | 应用帐户的名称。 | + | authType | string | 是 | 鉴权类型。 | + | bundleName | string | 是 | 用于检查可见性的应用包名。 | + +- 参数: + + | 类型 | 说明 | + | ------------------------------ | ------------------------ | + | Promise<boolean> | Promise实例,用于获取异步返回结果。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.checkOAuthTokenVisibility("LiSi", "readAge", "com.example.ohos.accountjsdemo", true).then((data) => { + console.log('checkOAuthTokenVisibility isVisible: ' + data); + }).catch((err) => { + console.log('checkOAuthTokenVisibility err: ' + JSON.stringify(err)); + }); + ``` + +### getAllOAuthTokens8+ + +getAllOAuthTokens(name: string, owner: string, callback: AsyncCallback<Array<OAuthTokenInfo>>): void; + +获取指定应用对调用方全部可见的OAuth令牌,使用callback回调异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------------------------------------------------ | ---- | ------------------- | + | name | string | 是 | 应用帐户的名称。 | + | owner | string | 是 | 应用帐户的所有者包名。 | + | callback | AsyncCallback<Array<OAuthTokenInfo>> | 是 | 查询结果的回调。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.getAllOAuthTokens("LiSi", "com.example.ohos.accountjsdemo", (err, data) => { + console.log("getAllOAuthTokens err: " + JSON.stringify(err)); + console.log('getAllOAuthTokens data: ' + JSON.stringify(data)); + }); + ``` + +### getAllOAuthTokens8+ + +getAllOAuthTokens(name: string, owner: string): Promise<Array<OAuthTokenInfo>>; + +获取指定应用帐户对调用方可见的全部OAuth令牌,使用Promise方式异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------ | ---- | ------------------- | + | name | string | 是 | 应用帐户的名称。 | + | owner | string | 是 | 应用帐户的所有者包名。 | + +- 参数: + + | 类型 | 说明 | + | ------------------------------ | ----------------------------------- | + | Promise<Array<OAuthTokenInfo>> | Promise实例,用于获取异步返回结果。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.getAllOAuthTokens("LiSi", "com.example.ohos.accountjsdemo").then((data) => { + console.log('getAllOAuthTokens data: ' + JSON.stringify(data)); + }).catch((err) => { + console.log("getAllOAuthTokens err: " + JSON.stringify(err)); + }); + ``` + +### getOAuthList8+ + +getOAuthList(name: string, authType: string, callback: AsyncCallback<Array<string>>): void; + +获取指定应用帐户和鉴权类型的OAuth令牌的授权列表,使用callback回调异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ---------------------------------------- | ---- | ------------------ | + | name | string | 是 | 应用帐户的名称。 | + | owner | string | 是 | 应用帐户的所有者包名。 | + | callback | AsyncCallback<Array<string>> | 是 | 查询结果的回调。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.getOAuthList("com.example.ohos.accountjsdemo", "readAge", (err, data) => { + console.log('getOAuthList err: ' + JSON.stringify(err)); + console.log('getOAuthList data: ' + JSON.stringify(data)); + }); + ``` + +### getOAuthList8+ + +getOAuthList(name: string, authType: string): Promise<Array<string>>; + +获取指定应用帐户和鉴权类型的OAuth令牌的授权列表,使用Promise方式异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | ------ | ---- | ------------------- | + | name | string | 是 | 应用帐户的名称。 | + | owner | string | 是 | 应用帐户的所有者包名。 | + +- 参数: + + | 类型 | 说明 | + | ------------------------------ | ------------------------------------ | + | Promise<Array<string>> | Promise实例,用于获取异步返回结果。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.getOAuthList("com.example.ohos.accountjsdemo", "readAge").then((data) => { + console.log('getOAuthList data: ' + JSON.stringify(data)); + }).catch((err) => { + console.log("getOAuthList err: " + JSON.stringify(err)); + }); + ``` + +### getAuthenticatorCallback8+ + +getAuthenticatorCallback(sessionId: string, callback: AsyncCallback<AuthenticatorCallback>): void; + +获取鉴权会话的认证器回调,使用callback回调异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | --------- | ------------------------------------------ | ---- | -------------- | + | sessionId | string | 是 | 鉴权会话的标识。 | + | callback | AsyncCallback<AuthenticatorCallback> | 是 | 查询结果的回调。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + featureAbility.getWant((err, want) => { + var sessionId = want.parameters[Constants.KEY_SESSION_ID]; + appAccountManager.getAuthenticatorCallback(sessionId, (err, callback) => { + if (err.code != ResultCode.SUCCESS) { + console.log("getAuthenticatorCallback err: " + JSON.stringify(err)); + return; + } + var result = {Constants.KEY_NAME: "LiSi", Constants.KEY_OWNER: "com.example.ohos.accountjsdemo", + Constants.KEY_AUTH_TYPE: "readAge", Constants.KEY_TOKEN: "xxxxxx"}; + callback.OnResult(ResultCode.SUCCESS, result); + }); + }); + ``` + +### getAuthenticatorCallback8+ + +getAuthenticatorCallback(sessionId: string): Promise<AuthenticatorCallback>; + +获取鉴权会话的认证器回调,使用Promise方式异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | ---------- | ------ | ---- | -------------- | + | sessionId | string | 是 | 鉴权会话的标识。 | + +- 参数: + + | 类型 | 说明 | + | ------------------------------------ | -------------------------------- | + | Promise<AuthenticatorCallback> | Promise实例,用于获取异步返回结果。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + featureAbility.getWant().then((want) => { + var sessionId = want.parameters[Constants.KEY_SESSION_ID]; + appAccountManager.getAuthenticatorCallback(sessionId).then((callback) => { + var result = {Constants.KEY_NAME: "LiSi", Constants.KEY_OWNER: "com.example.ohos.accountjsdemo", + Constants.KEY_AUTH_TYPE: "readAge", Constants.KEY_TOKEN: "xxxxxx"}; + callback.OnResult(ResultCode.SUCCESS, result); + }).catch((err) => { + console.log("getAuthenticatorCallback err: " + JSON.stringify(err)); + }); + }).catch((err) => { + console.log("getWant err: " + JSON.stringify(err)); + }); + ``` + +### getAuthenticatorInfo8+ + +getAuthenticatorInfo(owner: string, callback: AsyncCallback<AuthenticatorInfo>): void; + +获取指定应用帐户的认证器信息,使用callback回调异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------------------------------------- | ---- | ------------------- | + | owner | string | 是 | 应用帐户的所有者包名。 | + | callback | AsyncCallback<AuthenticatorInfo> | 是 | 查询结果的回调。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.getAuthenticatorInfo("com.example.ohos.accountjsdemo", (err, data) => { + console.log("getAuthenticatorInfo err: " + JSON.stringify(err)); + console.log('getAuthenticatorInfo data: ' + JSON.stringify(data)); + }); + ``` + +### getAuthenticatorInfo8+ + +getAuthenticatorInfo(owner: string): Promise<AuthenticatorInfo>; + +获取指定应用帐户的认证器信息,使用Promise方式异步返回结果。 + +- 参数: + + | 参数名 | 类型 | 必填 | 说明 | + | ----- | ------ | ---- | -------------------- | + | owner | string | 是 | 应用帐户的所有者包名。 | + +- 参数: + + | 类型 | 说明 | + | ------------------------------ | ----------------------------------- | + | Promise<AuthenticatorInfo> | Promise实例,用于获取异步返回结果。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + appAccountManager.getAuthenticatorInfo("com.example.ohos.accountjsdemo").then((data) => { + console.log('getAuthenticatorInfo: ' + JSON.stringify(data)); + }).catch((err) => { + console.log("getAuthenticatorInfo err: " + JSON.stringify(err)); + }); + ``` + ## AppAccountInfo +表示应用帐号信息。 + +| 参数名 | 类型 | 必填 | 说明 | +| ----- | ------ | ---- | ---------------- | +| owner | string | 是 | 应用帐户的所有者包名。 | +| name | string | 是 | 应用帐户的名称。 | + +## OAuthTokenInfo8+ + +表示OAuth令牌信息。 + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------ | ---- | -------------- | +| authType | string | 是 | 令牌的鉴权类型。 | +| token | string | 是 | 令牌的取值。 | + +## AuthenticatorInfo8+ + +表示OAuth认证器信息。 + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------ | ---- | ------------------ | +| owner | string | 是 | 认证器的所有者包名。 | +| iconId | string | 是 | 认证器的图标标识。 | +| labelId | string | 是 | 认证器的标签标识。 | + +## Constants8+ + +表示常量的枚举。 + +| 名称 | 默认值 | 描述 | +| ----------------------------- | ---------------------- | ----------------------- | +| ACTION_ADD_ACCOUNT_IMPLICITLY | "addAccountImplicitly" | 表示操作_隐式添加帐号。 | +| ACTION_AUTHENTICATE | "authenticate" | 表示操作_鉴权。 | +| KEY_NAME | "name" | 表示键名_应用帐户名称。 | +| KEY_OWNER | "owner" | 表示键名_应用帐户所有者。 | +| KEY_TOKEN | "token" | 表示键名_令牌。 | +| KEY_ACTION | "action" | 表示键名_操作。 | +| KEY_AUTH_TYPE | "authType" | 表示键名_鉴权类型。 | +| KEY_SESSION_ID | "sessionId" | 表示键名_会话标识。 | +| KEY_CALLER_PID | "callerPid" | 表示键名_调用方PID。 | +| KEY_CALLER_UID | "callerUid" | 表示键名_调用方UID。 | +| KEY_CALLER_BUNDLE_NAME | "callerBundleName" | 表示键名_调用方包名。 | + +## ResultCode8+ +表示返回码的枚举。 -| 参数名 | 类型 | 必填 | 说明 | -| ------ | ------ | ---- | ------------------ | -| owner | string | 是 | 所有者是应用帐户。 | -| name | string | 是 | 应用帐户的名称。 | +| 名称 | 默认值 | 描述 | +| ----------------------------------- | ----- | ---------------------- | +| SUCCESS | 0 | 表示操作成功。 | +| ERROR_ACCOUNT_NOT_EXIST | 10001 | 表示应用帐户不存在。 | +| ERROR_APP_ACCOUNT_SERVICE_EXCEPTION | 10002 | 表示应用帐户服务异常。 | +| ERROR_INVALID_PASSWORD | 10003 | 表示密码无效。 | +| ERROR_INVALID_REQUEST | 10004 | 表示请求无效。 | +| ERROR_INVALID_RESPONSE | 10005 | 表示响应无效。 | +| ERROR_NETWORK_EXCEPTION | 10006 | 表示网络异常。 | +| ERROR_OAUTH_AUTHENTICATOR_NOT_EXIST | 10007 | 表示认证器不存在。 | +| ERROR_OAUTH_CANCELED | 10008 | 表示鉴权取消。 | +| ERROR_OAUTH_LIST_TOO_LARGE | 10009 | 表示开放授权列表过大。 | +| ERROR_OAUTH_SERVICE_BUSY | 10010 | 表示开放授权服务忙碌。 | +| ERROR_OAUTH_SERVICE_EXCEPTION | 10011 | 表示开放授权服务异常。 | +| ERROR_OAUTH_SESSION_NOT_EXIST | 10012 | 表示鉴权会话不存在。 | +| ERROR_OAUTH_TIMEOUT | 10013 | 表示鉴权超时。 | +| ERROR_OAUTH_TOKEN_NOT_EXIST | 10014 | 表示开放授权令牌不存在。 | +| ERROR_OAUTH_TOKEN_TOO_MANY | 10015 | 表示开放授权令牌过多。 | +| ERROR_OAUTH_UNSUPPORT_ACTION | 10016 | 表示不支持的鉴权操作。 | +| ERROR_OAUTH_UNSUPPORT_AUTH_TYPE | 10017 | 表示不支持的鉴权类型。 | +| ERROR_PERMISSION_DENIED | 10018 | 表示权限不足。 | +## AuthenticatorCallback8+ + +OAuth认证器回调接口。 + +### onResult8+ + +onResult: (code: number, result: {[key: string]: any}) => void; + +通知鉴权结果。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | ------ | -------------------- | ---- | ----------- | + | code | number | 是 | 鉴权结果码。 | + | result | {[key: string]: any} | 是 | 鉴权结果。 | + +- 示例: + + ``` + const appAccountManager = account_appAccount.createAppAccountManager(); + var sessionId = "1234"; + appAccountManager.getAuthenticatorCallback(sessionId).then((callback) => { + var result = {Constants.KEY_NAME: "LiSi", Constants.KEY_OWNER: "com.example.ohos.accountjsdemo", + Constants.KEY_AUTH_TYPE: "readAge", Constants.KEY_TOKEN: "xxxxxx"}; + callback.OnResult(ResultCode.SUCCESS, result); + }).catch((err) => { + console.log("getAuthenticatorCallback err: " + JSON.stringify(err)); + }); + ``` + +### onRequestRedirected8+ + +onRequestRedirected: (request: Want) => void; + +通知鉴权请求被跳转。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | ------- | ---- | ---- | ------------------ | + | request | Want | 是 | 用于跳转的请求信息。 | + +- 示例: + + ``` + class MyAuthenticator extends account_appAccount.Authenticator { + addAccountImplicitly(authType, callerBundleName, options, callback) { + callback.onRequestRedirected({ + bundleName: "com.example.ohos.accountjsdemo", + abilityName: "com.example.ohos.accountjsdemo.LoginAbility", + }); + } + + authenticate(name, authType, callerBundleName, options, callback) { + var result = {Constants.KEY_NAME: name, Constants.KEY_AUTH_TYPE: authType, Constants.KEY_TOKEN: "xxxxxx"}; + callback.onResult(ResultCode.SUCCESS, result); + } + } + ``` + +## Authenticator8+ + +OAuth认证器基类。 + +### addAccountImplicitly8+ + +addAccountImplicitly(authType: string, callerBundleName: string, options: {[key: string]: any}, callback: AuthenticatorCallback): void; + +根据指定的鉴权类型和可选项,隐式地添加应用帐户,并使用callback回调异步返回结果。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | ---------------- | --------------------- | --- | -------------------------- | + | authType | string | 是 | 应用帐户的鉴权类型。 | + | callerBundleName | string | 是 | 鉴权请求方的包名。 | + | options | {[key: string]: any} | 是 | 鉴权所需要的可选项。 | + | callback | AuthenticatorCallback | 是 | 认证器回调,用于返回鉴权结果。 | + +### authenticate8+ + +authenticate(name: string, authType: string, callerBundleName: string, options: {[key: string]: any}, callback: AuthenticatorCallback): void; + +对应用帐户进行鉴权,获取OAuth令牌,并使用callback回调异步返回结果。 + +- 参数: + | 接口名 | 类型 | 必填 | 说明 | + | ---------------- | --------------------- | ---- | -------------------------- | + | name | string | 是 | 应用帐户的名称。 | + | authType | string | 是 | 应用帐户的鉴权类型。 | + | callerBundleName | string | 是 | 鉴权请求方的包名。 | + | options | {[key: string]: any} | 是 | 鉴权所需要的可选项。 | + | callback | AuthenticatorCallback | 是 | 认证器回调,用于返回鉴权结果。 | + +- 示例: + + ``` + class MyAuthenticator extends account_appAccount.Authenticator { + addAccountImplicitly(authType, callerBundleName, options, callback) { + callback.onRequestRedirected({ + bundleName: "com.example.ohos.accountjsdemo", + abilityName: "com.example.ohos.accountjsdemo.LoginAbility", + }); + } + + authenticate(name, authType, callerBundleName, options, callback) { + var result = {Constants.KEY_NAME: name, Constants.KEY_AUTH_TYPE: authType, Constants.KEY_TOKEN: "xxxxxx"}; + callback.onResult(ResultCode.SUCCESS, result); + } + } + + export default { + onConnect(want) { + return new MyAuthenticator(); + } + } + ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-distributed-account.md b/zh-cn/application-dev/reference/apis/js-apis-distributed-account.md index e61397cf8b0820b160081327b319b3ca18ea93a5..33ba4d7b94fc998cd072365f1045cc351ca1670a 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-distributed-account.md +++ b/zh-cn/application-dev/reference/apis/js-apis-distributed-account.md @@ -11,6 +11,11 @@ import account_distributedAccount from '@ohos.account.distributedAccount'; ``` +## 系统能力 + +SystemCapability.Account.OsAccount + + ## account_distributedAccount.getDistributedAccountAbility getDistributedAccountAbility(): DistributedAccountAbility