目录

authentication (华为账号应用统一认证服务)

本模块提供Account Kit(华为账号服务)认证能力,包括账号登录、授权、取消授权等功能。

起始版本:4.0.0(10)

导入模块

import { authentication } from '@kit.AccountKit';

HuaweiIDProvider

该类提供实现认证服务的方法,用于创建登录、授权、取消授权请求对象。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

继承:HuaweiIDProvider继承自AuthenticationProvider。

[h2]createLoginWithHuaweiIDRequest

createLoginWithHuaweiIDRequest(): LoginWithHuaweiIDRequest

创建默认scope和permission登录请求对象,可通过返回值设置请求参数。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

返回值:

类型

说明

LoginWithHuaweiIDRequest

定义HarmonyOS应用使用Account Kit登录API获取UnionID、OpenID等数据的请求对象。

示例:

import { authentication } from '@kit.AccountKit';

const huaweiIdProvider = new authentication.HuaweiIDProvider();
const loginWithHuaweiIDRequest = huaweiIdProvider.createLoginWithHuaweiIDRequest();

[h2]createAuthorizationWithHuaweiIDRequest

createAuthorizationWithHuaweiIDRequest(): AuthorizationWithHuaweiIDRequest

创建一个Account Kit授权请求对象,可通过返回值设置请求参数。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

返回值:

类型

说明

AuthorizationWithHuaweiIDRequest

定义HarmonyOS应用授权获取用户信息请求对象。

示例:

import { authentication } from '@kit.AccountKit';

const huaweiIdProvider = new authentication.HuaweiIDProvider();
const request = huaweiIdProvider.createAuthorizationWithHuaweiIDRequest();

[h2]createCancelAuthorizationRequest

createCancelAuthorizationRequest(): CancelAuthorizationRequest

创建一个Account Kit取消授权请求对象,可通过返回值设置参数。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

返回值:

类型

说明

CancelAuthorizationRequest

定义HarmonyOS应用取消Account Kit授权请求对象。

示例:

import { authentication } from '@kit.AccountKit';

const huaweiIdProvider = new authentication.HuaweiIDProvider();
const cancelAuthorizationRequest = huaweiIdProvider.createCancelAuthorizationRequest();

[h2]getHuaweiIDState

getHuaweiIDState(request: StateRequest): Promise<StateResult>

获取华为账号登录状态,使用Promise异步回调。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:5.0.0(12)

参数:

参数名

类型

必填

说明

request

StateRequest

是

获取华为账号登录状态请求对象,包含请求参数。

返回值:

类型

说明

Promise<StateResult>

Promise对象,返回StateResult对象。

错误码:

以下错误码的详细介绍请参见账号管理错误码和ArkTS错误码。

错误码ID

错误信息

12300001

System service works abnormally.

1001502003

Invalid input parameter value.

示例:
import { authentication } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

// 创建请求参数
const stateRequest: authentication.StateRequest = {
  idType: authentication.IdType.UNION_ID,
  idValue: '<可通过华为账号登录接口获取>' // 该值可以通过华为账号登录接口获取
}
try {
  // 执行获取华为账号登录状态请求,并处理结果
  new authentication.HuaweiIDProvider().getHuaweiIDState(stateRequest).then((data: authentication.StateResult) => {
    hilog.info(0x0000, 'testTag', 'Succeeded in getting huaweiIdState result.');
    const state = data.state;
    // 处理state
  }).catch((error: BusinessError) => {
    this.dealAllError(error);
  })
} catch (error) {
  this.dealAllError(error);
}

// 错误处理
dealAllError(error: BusinessError): void {
  hilog.error(0x0000, 'testTag', 'Failed to get huaweiIdState, errorCode=%{public}d, errorMsg=%{public}s', error.code, error.message);  
}

[h2]getMobileNumberConsistency

getMobileNumberConsistency(request: ConsistencyRequest): Promise<ConsistencyResult>

获取手机号一致性状态,使用Promise异步回调。

开发者在使用获取手机号一致性状态接口前,需要完成quickLoginMobilePhone(华为账号一键登录)的scope权限申请,详情参见开发前提。scope权限申请审批未完成或未通过,将报错1001502014 应用未申请scopes或permissions权限。

模型约束:此接口仅可在Stage模型下使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:5.0.0(12)

参数:

参数名

类型

必填

说明

request

ConsistencyRequest

是

获取手机号一致性请求对象,包含请求参数。

返回值:

类型

说明

Promise<ConsistencyResult>

Promise对象,返回ConsistencyResult对象。

错误码:

以下错误码的详细介绍请参见ArkTS错误码和通用错误码。

错误码ID

错误信息

401

Parameter error. Possible causes:

1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

1001500001

Failed to check the fingerprint of the app bundle.

1001500004

The account does not support this function.

1001500005

This function is restricted from being called.

1001502001

The user has not logged in with HUAWEI ID.

1001502002

The application is not authorized.

1001502005

Network error.

1001502009

Internal error.

1001502014

The app does not have the required scopes or permissions.

示例:
import { authentication } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

// 创建请求参数
const consistencyRequest: authentication.ConsistencyRequest = {
  idType: authentication.IdType.UNION_ID,
  idValue: '<可通过华为账号登录接口获取>', // 该值可以通过华为账号登录接口获取
  mobileNumber: '+86xxxxxxxxxxx' // 通过华为账号一键登录功能获取到的明文手机号
}
try {
  // 执行获取手机号一致性状态请求,并处理结果
  new authentication.HuaweiIDProvider().getMobileNumberConsistency(consistencyRequest).then((data: authentication.ConsistencyResult) => {
    hilog.info(0x0000, 'testTag', 'Succeeded in getting getMobileNumberConsistency result = %{public}d', data.state);
    const state = data.state;
    // 处理state
  }).catch((err: BusinessError) => {
    this.dealAllError(err);
  })
} catch (error) {
  this.dealAllError(error);
}

// 错误处理
dealAllError(error: BusinessError): void {
  hilog.error(0x0000, 'testTag', 'Failed to get mobileNumberConsistency, errorCode=%{public}d, errorMsg=%{public}s', error.code, error.message);  
}

LoginWithHuaweiIDRequest

定义HarmonyOS应用使用Account Kit登录API获取UnionID、OpenID等数据的请求对象。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

继承:LoginWithHuaweiIDRequest继承自AuthenticationRequest。

名称

类型

只读

可选

说明

forceLogin

boolean

否

是

表示用户是否需要登录。

如果该值为true,华为账号未登录时,则将拉起华为账号登录页。此外,LoginWithHuaweiIDRequest必须在ArkUI页面上下文中执行,否则会抛出异常。

如果该值false,且华为账号未登录,则返回1001502001 用户未登录华为账号。默认值:true。

state

string

否

是

请求体中的state参数,该参数与响应体中返回的state比较,校验是否是当前请求,可防止跨站攻击。

开发者可自定义,字符包含0-9、a-z、A-Z、英文点号、英文冒号、斜杠、下划线等,长度限制255,校验规则^[0-9a-zA-Z:/\.\-_]{1,255}$。

推荐开发者用随机数并做一致性校验。建议生成方式:util.generateRandomUUID()。

nonce

string

否

是

请求体中的nonce参数,该参数会包含在返回的ID Token中,通过校验一致性,可用于防止重放攻击。

字符包含0-9、a-z、A-Z、点号、冒号、斜杠、下划线等,长度限制255,校验规则^[0-9a-zA-Z:/\.\-_]{1,255}$。

如该参数未传、传空,ID Token中nonce默认值:“default”。

推荐开发者用随机数并做一致性校验。建议生成方式:util.generateRandomUUID()。

idTokenSignAlgorithm

IdTokenSignAlgorithm

否

是

用于指定ID Token的签名算法,默认值:PS256。

示例:

import { authentication } from '@kit.AccountKit';
import { util } from '@kit.ArkTS';

const loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
loginRequest.forceLogin = true;
loginRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256; // 默认为PS256
loginRequest.state = util.generateRandomUUID(); // 建议使用generateRandomUUID生成state

IdTokenSignAlgorithm

ID Token签名算法,该类型为枚举,根据IdTokenSignAlgorithm的不同类型,对ID Token进行不同方式的加密。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

名称

值

说明

PS256

1

RSASSA-PSS使用SHA-256和基于SHA-256的MGF1。

RS256

2

RSASSA-PKCS1-v1_5使用SHA-256。

示例:

import { authentication } from '@kit.AccountKit';

const loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
loginRequest.forceLogin = true;
loginRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256;

LoginWithHuaweiIDResponse

Account Kit登录请求响应,解析响应可得到OpenID、UnionID、Authorization Code、ID Token数据。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

继承:LoginWithHuaweiIDResponse继承自AuthenticationResponse。

名称

类型

只读

可选

说明

data

LoginWithHuaweiIDCredential

是

是

登录结果数据,包含openID、unionID、authorizationCode、idToken字段。

state

string

是

是

响应体中返回的state,账号服务将该字段与请求体中传入的state比较,防止跨站攻击。字符包含“0-9”、“a-z”、“A-Z”、英文点号、英文冒号、斜杠、下划线等,长度限制255。校验规则^[0-9a-zA-Z:/\.\-_]{1,255}$。

示例:

import { authentication } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';

// 创建登录请求,并设置参数
const loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
loginRequest.forceLogin = true;
loginRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256;
loginRequest.state = util.generateRandomUUID(); // 建议使用generateRandomUUID生成state

// 执行登录请求,并处理结果
try {
  const controller = new authentication.AuthenticationController(getContext(this));
  controller.executeRequest(loginRequest, (error: BusinessError<Object>, data) => {
    if (error) {
      this.dealAllError(error);
      return;
    }
    const loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;
    const state = loginWithHuaweiIDResponse.state;
    if (state && loginRequest.state !== state) {
      hilog.error(0x0000, 'testTag', `Failed to login. The state is different, response state: ${state}`);
      return;
    }
    hilog.info(0x0000, 'testTag', 'Succeeded in login.');
    const loginWithHuaweiIDCredential = loginWithHuaweiIDResponse.data!;
    const code = loginWithHuaweiIDCredential.authorizationCode;
    const idToken = loginWithHuaweiIDCredential.idToken;
    const openID = loginWithHuaweiIDCredential.openID;
    const unionID = loginWithHuaweiIDCredential.unionID;
    // 开发者处理code, idToken, openID, unionID
  });
} catch (error) {
  this.dealAllError(error);
}

// 错误处理
dealAllError(error: BusinessError<Object>): void {
  hilog.error(0x0000, 'testTag', `Failed to login. Code: ${error.code}, message: ${error.message}`);
}

LoginWithHuaweiIDCredential

Account Kit登录成功返回的凭据,用于解析出Authorization Code、ID Token等登录返回的数据。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

名称

类型

只读

可选

说明

authorizationCode

string

是

是

Authorization Code。临时凭据,有效时间5分钟,并且只能使用一次。

idToken

string

是

是

ID Token。JWT格式的字符串,包含用户信息。

openID

string

是

否

OpenID。OpenID是华为账号用户在不同类型的产品的身份ID,同一个用户,不同应用,OpenID值不同。具体格式要求请参考OpenID和UnionID的格式说明。

unionID

string

是

否

UnionID。UnionID是华为账号用户在同一个开发者账号下产品的身份ID,同一个用户,同一个开发者账号下管理的不同应用,UnionID值相同。具体格式要求请参考OpenID和UnionID的格式说明。

示例:

import { authentication } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';

// 创建登录请求,并设置参数
const loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
loginRequest.forceLogin = true;
loginRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256;
loginRequest.state = util.generateRandomUUID(); // 建议使用generateRandomUUID生成state

// 执行登录请求,并处理结果
try {
  const controller = new authentication.AuthenticationController(getContext(this));
  controller.executeRequest(loginRequest, (error: BusinessError<Object>, data) => {
    if (error) {
      this.dealAllError(error);
      return;
    }
    const loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;
    const state = loginWithHuaweiIDResponse.state;
    if (state && loginRequest.state !== state) {
      hilog.error(0x0000,
        'testTag', `Failed to login. The state is different, response state: ${state}`);
      return;
    }
    hilog.info(0x0000, 'testTag', 'Succeeded in login.');

    const loginWithHuaweiIDCredential = loginWithHuaweiIDResponse.data!;
    const code = loginWithHuaweiIDCredential.authorizationCode;
    const idToken = loginWithHuaweiIDCredential.idToken;
    const openID = loginWithHuaweiIDCredential.openID;
    const unionID = loginWithHuaweiIDCredential.unionID;
    // 开发者处理code, idToken, openID, unionID
  });
} catch (error) {
  this.dealAllError(error);
}

// 错误处理
dealAllError(error: BusinessError<Object>): void {
  hilog.error(0x0000, 'testTag', `Failed to login. Code: ${error.code}, message: ${error.message}`);
}

AuthorizationWithHuaweiIDRequest

该类为对应用的授权请求,使用Account Kit来请求授权以申请更多的用户信息,包括scopes、permissions等属性。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

继承:AuthorizationWithHuaweiIDRequest继承自AuthenticationRequest。

名称

类型

只读

可选

说明

scopes

string[]

否

是

scope列表,用于获取用户数据。与permissions属性不能同时为空,否则会返回1001502003 输入参数值无效错误码。如果传入不合法的scope(例如空值等)则直接返回OpenID和UnionID。

scope取值范围:

  • profile:华为账号用户的基本信息,如昵称头像等(元服务不支持该scope)。
  • openid:华为账号用户的OpenID、UnionID。
  • phone:华为账号快速验证手机号。(元服务不能直接调用该接口获取手机号,可参考场景化控件快速验证手机号获取)。
  • quickLoginAnonymousPhone:获取华为账号绑定的匿名手机号(该scope只能与openid同时使用)。
    说明:

    中国境内(不包含中国香港、中国澳门、中国台湾)匿名手机号的返回格式不包含国际电话区号,其他国家和地区默认包含国际电话区号。

permissions

string[]

否

是

permission列表。与scopes属性不能同时为空,否则会返回1001502003 输入参数值无效错误码。如果传入不合法的permission(例如空值等)则直接返回OpenID和UnionID。

permission取值范围:

  • serviceauthcode:用户授权临时凭据。
  • idtoken:用户身份认证信息。

forceAuthorization

boolean

否

是

用户是否需要登录授权,默认值:true。

如果该值为true且用户未登录或未授权,则会拉起用户登录或授权页面。另外,AuthorizationWithHuaweiIDRequest必须在ArkUI页面上下文中执行,否则会抛出异常。

如果该值为false并且用户未登录,则在执行AuthorizationWithHuaweiIDRequest情况下将返回1001502001 用户未登录华为账号。

state

string

否

是

请求体中的state参数,开发者可自定义,字符包含0-9、a-z、A-Z、点号、冒号、斜杠、下划线等,长度限制255,校验规则^[0-9a-zA-Z:/\.\-_]{1,255}$。该参数与响应体中返回的state比较,校验是否是当前请求,可防止跨站攻击。

推荐开发者用随机数并做一致性校验。建议生成方式:util.generateRandomUUID()。

nonce

string

否

是

请求体中的nonce参数,字符包含0-9、a-z、A-Z、点号、冒号、斜杠、下划线等,长度限制255,校验规则^[0-9a-zA-Z:/\.\-_]{1,255}$。该参数会包含在返回的ID Token中,通过校验一致性,可用于防止重放攻击。如该参数未传、传空,ID Token中nonce默认值:“default”。

推荐开发者用随机数并做一致性校验。建议生成方式:util.generateRandomUUID()。

idTokenSignAlgorithm

IdTokenSignAlgorithm

否

是

默认值:PS256,用于指定ID Token的签名算法。

示例:

import { authentication } from '@kit.AccountKit';
import { util } from '@kit.ArkTS';

const authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
authRequest.scopes = ['profile']; // 元服务不支持profile,需传其他支持的scope
authRequest.permissions = ['idtoken'];
authRequest.forceAuthorization = true;
authRequest.state = util.generateRandomUUID(); // 建议使用generateRandomUUID生成state
authRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256;

AuthorizationWithHuaweiIDResponse

该类封装授权请求获取的用户信息结果。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

继承:AuthorizationWithHuaweiIDResponse继承自AuthenticationResponse。

名称

类型

只读

可选

说明

data

AuthorizationWithHuaweiIDCredential

是

是

用户授权结果数据。

state

string

是

是

响应体中返回的state,字符包含0-9、a-z、A-Z、英文点号、英文冒号、斜杠、下划线等,长度限制255,校验规则^[0-9a-zA-Z:/\.\-_]{1,255}$。与请求体中传入的state比较,校验是否是当前请求,防止跨站攻击。

示例:

import { authentication } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';

// 创建授权请求,并设置参数
const authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
authRequest.scopes = ['profile']; // 元服务不支持profile,需传其他支持的scope。默认返回UnionID、OpenID
authRequest.permissions = ['idtoken', 'serviceauthcode']; 
authRequest.forceAuthorization = true;
authRequest.state = util.generateRandomUUID(); // 建议使用generateRandomUUID生成state
authRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256;

// 执行授权请求,并处理结果
try {
  const controller = new authentication.AuthenticationController(getContext(this));
  controller.executeRequest(authRequest, (error: BusinessError<Object>, data) => {
    if (error) {
      this.dealAllError(error);
      return;
    }
    const authorizationWithHuaweiIDResponse = data as authentication.AuthorizationWithHuaweiIDResponse;
    const state = authorizationWithHuaweiIDResponse.state;
    if (state && authRequest.state !== state) {
      hilog.error(0x0000, 'testTag', `Failed to authorize. The state is different, response state: ${state}`);
      return;
    }
    hilog.info(0x0000, 'testTag', 'Succeeded in authentication.');
    const authorizationWithHuaweiIDCredential = authorizationWithHuaweiIDResponse.data!;
    const avatarUri = authorizationWithHuaweiIDCredential.avatarUri; // 元服务不支持该字段
    const nickName = authorizationWithHuaweiIDCredential.nickName; // 元服务不支持该字段
    const idToken = authorizationWithHuaweiIDCredential.idToken;
    const openID = authorizationWithHuaweiIDCredential.openID;
    const unionID = authorizationWithHuaweiIDCredential.unionID;
    const code = authorizationWithHuaweiIDCredential.authorizationCode;
    // 开发者处理avatarUri, nickName, idToken, openID, unionID, code
  });
} catch (error) {
  this.dealAllError(error);
}

// 错误处理
dealAllError(error: BusinessError<Object>): void {
  hilog.error(0x0000, 'testTag', `Failed to auth. Code: ${error.code}, message: ${error.message}`);
}

AuthorizationWithHuaweiIDCredential

该接口用于解析授权请求返回的用户信息数据。

模型约束:此接口仅可在Stage模型下使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

名称

类型

只读

可选

说明

authorizationCode

string

是

是

Authorization Code。临时凭据,有效时间5分钟,并且只能使用一次。

返回场景:AuthorizationWithHuaweiIDRequest接口的permissions中传入'serviceauthcode'参数时返回。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

idToken

string

是

是

ID Token。JWT格式的字符串,包含用户信息。

返回场景:AuthorizationWithHuaweiIDRequest接口的permissions中传入'idtoken'参数时返回。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

openID

string

是

是

OpenID。OpenID是华为账号用户在不同类型的产品的身份ID,同一个用户不同应用,OpenID值不同。具体格式要求请参考OpenID和UnionID的格式说明。

返回场景:默认返回。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

unionID

string

是

是

UnionID。UnionID是华为账号用户在同一个开发者账号下产品的身份ID,同一个用户,同一个开发者账号下管理的不同应用,UnionID值相同。具体格式要求请参考OpenID和UnionID的格式说明。

返回场景:默认返回。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

avatarUri

string

是

是

用户头像链接,有效期较短,建议先将头像下载保存后再使用。

返回场景:AuthorizationWithHuaweiIDRequest接口的scopes中传入'profile'参数时返回。

须知:

元服务不支持该字段。

nickName

string

是

是

用户昵称。

返回场景:AuthorizationWithHuaweiIDRequest接口的scopes中传入'profile'参数时返回。

须知:

元服务不支持该字段。

email

string

是

是

用户邮箱。

返回场景:AuthorizationWithHuaweiIDRequest接口的scopes中传入'email'参数时返回。

须知:

元服务不支持该字段。

authorizedScopes

string[]

是

是

本次授权的scope清单。

返回场景:默认返回。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

extraInfo

Record<string, Object>

是

是

扩展信息。如果开发者开启了代码混淆需要配置混淆白名单防止其中包含的属性被混淆。

返回场景:AuthorizationWithHuaweiIDRequest接口的scopes中传入扩展请求参数('quickLoginAnonymousPhone'等)时返回。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

说明:华为账号一键登录场景下,可通过"localNumberConsistency"字段获取华为账号绑定手机号和用户本机SIM卡手机号对比结果:

true:结果相同。

false:结果不同。

说明:

若用户本机无SIM卡,返回false。

若用户本机有SIM卡,只要其中有1张SIM卡手机号比对成功即返回true,否则返回false。

示例:

import { authentication } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';

// 创建授权请求,并设置参数
const authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
authRequest.scopes = ['profile']; // 元服务不支持profile,需传其他支持的scope
authRequest.permissions = ['idtoken'];
authRequest.forceAuthorization = true;
authRequest.state = util.generateRandomUUID(); // 建议使用generateRandomUUID生成state
authRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256;

// 执行授权请求,并处理结果
try {
  const controller = new authentication.AuthenticationController(getContext(this));
  controller.executeRequest(authRequest, (error: BusinessError<Object>, data) => {
    if (error) {
      this.dealAllError(error);
      return;
    }
    const authorizationWithHuaweiIDResponse = data as authentication.AuthorizationWithHuaweiIDResponse;
    const state = authorizationWithHuaweiIDResponse.state;
    if (state && authRequest.state !== state) {
      hilog.error(0x0000, 'testTag', `Failed to authorize. The state is different, response state: ${state}`);
      return;
    }
    hilog.info(0x0000, 'testTag', 'Succeeded in authentication.');
    const authorizationWithHuaweiIDCredential = authorizationWithHuaweiIDResponse.data!;
    const avatarUri = authorizationWithHuaweiIDCredential.avatarUri; // 元服务不支持该字段
    const nickName = authorizationWithHuaweiIDCredential.nickName; // 元服务不支持该字段
    const idToken = authorizationWithHuaweiIDCredential.idToken;
    const unionID = authorizationWithHuaweiIDCredential.unionID;
    const openID = authorizationWithHuaweiIDCredential.openID;
    // 开发者处理avatarUri, nickName, idToken, unionID, openID
  });
} catch (error) {
  this.dealAllError(error);
}

// 错误处理
dealAllError(error: BusinessError<Object>): void {
  hilog.error(0x0000, 'testTag', `Failed to auth. Code: ${error.code}, message: ${error.message}`);
}

CancelAuthorizationRequest

该类为应用取消Account Kit授权的请求。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

继承:CancelAuthorizationRequest继承自AuthenticationRequest。

名称

类型

只读

可选

说明

state

string

否

是

请求体中的state参数,开发者可自定义,字符包含0-9、a-z、A-Z、英文点号、英文冒号、斜杠、下划线等,长度限制255,校验规则^[0-9a-zA-Z:/\.\-_]{1,255}$。

该参数与响应体中返回的state比较,校验是否是当前请求,可防止跨站攻击。

推荐开发者用随机数并做一致性校验。建议生成方式:util.generateRandomUUID()。

示例:

import { authentication } from '@kit.AccountKit';
import { util } from '@kit.ArkTS';

const cancelRequest = new authentication.HuaweiIDProvider().createCancelAuthorizationRequest();
cancelRequest.state = util.generateRandomUUID(); // 建议使用generateRandomUUID生成state

CancelAuthorizationResponse

该类为应用取消Account Kit授权的响应。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

继承:CancelAuthorizationResponse继承自AuthenticationResponse。

属性:

名称

类型

只读

可选

说明

state

string

是

是

响应体中返回的state,字符包含0-9、a-z、A-Z、英文点号、英文冒号、斜杠、下划线等,长度限制255,校验规则^[0-9a-zA-Z:/\.\-_]{1,255}$。

与请求体中传入的state比较,校验是否是当前请求,防止跨站攻击。

示例:

import { authentication } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';

// 创建取消授权请求,并设置参数
const cancelRequest = new authentication.HuaweiIDProvider().createCancelAuthorizationRequest();
cancelRequest.state = util.generateRandomUUID(); // 建议使用generateRandomUUID生成state

// 执行取消授权请求,并处理结果
try {
  const controller = new authentication.AuthenticationController(getContext(this));
  controller.executeRequest(cancelRequest, (error: BusinessError<Object>, data) => {
    if (error) {
      this.dealAllError(error);
      return;
    }
    const cancelAuthorizationResponse = data as authentication.CancelAuthorizationResponse;
    const state = cancelAuthorizationResponse.state;
    if (state && cancelRequest.state !== state) {
      hilog.error(0x0000, 'testTag', `Failed to cancel. The state is different, response state: ${state}`);
      return;
    }
    hilog.info(0x0000, 'testTag', 'Succeeded in canceling.');
  });
} catch (error) {
  this.dealAllError(error);
}

// 错误处理
dealAllError(error: BusinessError<Object>): void {
  hilog.error(0x0000, 'testTag', `Failed to cancel. Code: ${error.code}, message: ${error.message}`);
}

AuthenticationErrorCode

该枚举为登录、授权、取消授权等接口的错误码。

模型约束:此接口仅可在Stage模型下使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

名称

值

说明

PACKAGE_FINGERPRINT_CHECK_ERROR

1001500001

应用指纹证书校验失败。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

DUPLICATE_REQUEST_REJECTED

1001500002

重复请求,当已有相同的请求在处理时,返回此错误码,此错误码不需要处理。你的应用需实现点击控制,防止连续点击发起相同请求。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

SCOPE_OR_PERRMISSION_UNSUPPORTED

1001500003

不支持该scopes或permissions。

起始版本:5.0.0(12)

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

UNSUPPORTED

1001500004

已登录的华为账号不支持该功能。

起始版本:5.0.0(12)

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

REQUEST_RESTRICTED

1001500005

该功能被限制调用。

起始版本:5.0.0(12)

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

ACCOUNT_NOT_LOGGED_IN

1001502001

用户未登录华为账号。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

APP_NOT_AUTHORIZED

1001502002

应用未授权。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

PARAMETER_INVALID

1001502003

输入参数值无效,接口传参异常等。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

NETWORK_ERROR

1001502005

网络异常。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

INTERNAL_ERROR

1001502009

内部错误,如华为账号服务器错误或其他内部错误等。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

USER_CANCELED

1001502012

用户取消授权。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

SCOPE_OR_PERMISSION_NOT_REQUESTED

1001502014

应用未申请scopes或permissions权限。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

AuthenticationController

该类为Account Kit登录授权、取消授权请求Controller。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

[h2]constructor

constructor(context?: common.Context)

构造器,构造Account Kit登录授权等请求Controller实例。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

参数:

参数名

类型

必填

说明

context

common.Context

否

Context上下文,当需要拉起华为账号登录、授权页面时必须传该参数,否则会报401 参数检查失败错误码。

应用可支持的Context有:UIAbilityContext和UIExtensionContext。不支持应用在半模态、弹出框、子窗口等非全页面组件中使用UIExtensionContext调用。

元服务可支持的Context有:UIAbilityContext。

说明:

示例:

import { authentication } from '@kit.AccountKit';

const controller = new authentication.AuthenticationController(getContext(this));

[h2]executeRequest

executeRequest(request: AuthenticationRequest, callback: AsyncCallback<AuthenticationResponse, Record<string, Object>>): void

执行Account Kit登录、授权等请求。使用callback异步回调。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

参数:

参数名

类型

必填

说明

request

AuthenticationRequest

是

认证请求体。

如该参数未正确传入,会抛出401 参数检查失败错误码。

callback

AsyncCallback<AuthenticationResponse, Record<string, Object>>

是

回调函数。

当获取响应成功操作时,err为undefined,data为获取到的AuthenticationResponse对象;否则为错误对象。

如该参数未正确传入,会抛出401 参数检查失败错误码。

错误码:

以下错误码的详细介绍请参见通用错误码、账号管理错误码和ArkTS错误码。

错误码ID

错误信息

401

Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.

12300001

System service works abnormally.

1001502001

The user has not logged in with HUAWEI ID.

1001502002

The application is not authorized.

1001502003

Invalid input parameter value.

1001502005

Network error.

1001502009

Internal error.

1001500001

Failed to check the fingerprint of the app bundle.

1001502012

The user canceled the authorization.

1001502014

The app does not have the required scopes or permissions.

1001500002

This error code is reported when a request is already being processed.

1001500003

The scopes or permissions are not supported.

示例:

import { authentication } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';

// 创建授权请求,并设置参数
const authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
authRequest.scopes = ['profile']; // 元服务不支持profile,需传其他支持的scope
authRequest.permissions = ['idtoken'];
// 用户是否需要登录授权,该值为true且用户未登录或未授权时,会拉起用户登录或授权页面
authRequest.forceAuthorization = true;
authRequest.state = util.generateRandomUUID(); // 建议使用generateRandomUUID生成state
authRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256;

// 执行授权请求,并处理结果
try {
  const controller = new authentication.AuthenticationController(getContext(this));
  controller.executeRequest(authRequest, (err: BusinessError<Object>, data) => {
    if (err) {
      this.dealAllError(err);
      return;
    }
    const authorizationWithHuaweiIDResponse = data as authentication.AuthorizationWithHuaweiIDResponse;
    const state = authorizationWithHuaweiIDResponse.state;
    if (state && authRequest.state !== state) {
      hilog.error(0x0000, 'testTag', `Failed to authorize. The state is different, response state: ${state}`);
      return;
    }
    hilog.info(0x0000, 'testTag', 'Succeeded in authentication.');
    const authorizationWithHuaweiIDCredential = authorizationWithHuaweiIDResponse.data!;
    const avatarUri = authorizationWithHuaweiIDCredential.avatarUri; // 元服务不支持该字段
    const nickName = authorizationWithHuaweiIDCredential.nickName; // 元服务不支持该字段
    const idToken = authorizationWithHuaweiIDCredential.idToken;
    const unionID = authorizationWithHuaweiIDCredential.unionID;
    const openID = authorizationWithHuaweiIDCredential.openID;
    // 开发者处理avatarUri, nickName, idToken, unionID, openID
  });
} catch (error) {
  this.dealAllError(error);
}

// 错误处理
dealAllError(error: BusinessError<Object>): void {
  hilog.error(0x0000, 'testTag', `Failed to auth. Code: ${error.code}, message: ${error.message}`);
}

[h2]executeRequest

executeRequest(request: AuthenticationRequest): Promise<AuthenticationResponse>

执行Account Kit登录授权等请求,使用Promise异步回调。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

参数:

参数名

类型

必填

说明

request

AuthenticationRequest

是

认证请求体。如该参数未正确传入,会抛出401 参数检查失败错误码。

返回值:

类型

说明

Promise<AuthenticationResponse>

Promise对象,返回AuthenticationResponse对象。

错误码:

以下错误码的详细介绍请参见通用错误码、账号管理错误码和ArkTS错误码。

错误码ID

错误信息

401

Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.

12300001

System service works abnormally.

1001502001

The user has not logged in with HUAWEI ID.

1001502002

The application is not authorized.

1001502003

Invalid input parameter value.

1001502005

Network error.

1001502009

Internal error.

1001500001

Failed to check the fingerprint of the app bundle.

1001502012

The user canceled the authorization.

1001502014

The app does not have the required scopes or permissions.

1001500002

This error code is reported when a request is already being processed.

1001500003

The scopes or permissions are not supported.

示例:

import { authentication } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';

// 创建授权请求,并设置参数
const authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
// 获取手机号需要传如下scope,传参数之前需要先申请对应scope权限,才能返回对应数据
authRequest.scopes = ['phone'];
// 获取code需传如下permission
authRequest.permissions = ['serviceauthcode'];
// 用户是否需要登录授权,该值为true且用户未登录或未授权时,会拉起用户登录或授权页面
authRequest.forceAuthorization = true;
// 用于防跨站点请求伪造
authRequest.state = util.generateRandomUUID();

// 执行请求
try {
  const controller = new authentication.AuthenticationController(getContext(this));
  controller.executeRequest(authRequest).then((data) => {
    const authorizationWithHuaweiIDResponse = data as authentication.AuthorizationWithHuaweiIDResponse;
    const state = authorizationWithHuaweiIDResponse.state;
    if (state && authRequest.state !== state) {
      hilog.error(0x0000, 'testTag', `Failed to authorize. The state is different, response state: ${state}`);
      return;
    }
    hilog.info(0x0000, 'testTag', 'Succeeded in authentication.');
    const authorizationWithHuaweiIDCredential = authorizationWithHuaweiIDResponse.data!;
    const code = authorizationWithHuaweiIDCredential.authorizationCode;
    const unionID = authorizationWithHuaweiIDCredential.unionID;
    const openID = authorizationWithHuaweiIDCredential.openID;
    // 开发者处理code, unionID, openID
  }).catch((err: BusinessError) => {
    this.dealAllError(err);
  });
} catch (error) {
  this.dealAllError(error);
}

// 错误处理
dealAllError(error: BusinessError): void {
  hilog.error(0x0000, 'testTag', 'Failed to auth, errorCode=%{public}d, errorMsg=%{public}s', error.code,
    error.message);
}

AuthenticationRequest

认证请求父类对象。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

AuthenticationResponse

认证请求响应父类对象。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

AuthenticationProvider

认证请求provider父类对象。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本4.1.0(11)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:4.0.0(10)

IdType

该枚举为ID类型枚举对象。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:5.0.0(12)

名称

值

说明

USER_ID

1

华为账号用户的UID。

说明:

该参数仅对系统应用开放。

OPEN_ID

2

华为账号用户的OpenID。具体格式要求请参考OpenID和UnionID的格式说明。

UNION_ID

3

华为账号用户的UnionID。具体格式要求请参考OpenID和UnionID的格式说明。

State

该枚举为华为账号登录状态枚举对象。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:5.0.0(12)

名称

值

说明

UNLOGGED_IN

0

华为账号未登录。

AUTHORIZED

1

华为账号已登录且传入账号与当前账号一致。

UNAUTHORIZED

2

华为账号已登录且传入账号与当前账号不一致。

StateRequest

该类获取华为账号登录状态请求对象。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:5.0.0(12)

名称

类型

只读

可选

说明

idType

IdType

否

否

属性idValue的ID类型,当前非系统应用只能传IdType.UNION_ID或IdType.OPEN_ID。

idValue

string

否

否

用户获取的UnionID、OpenID值,不可为空,传递的类型通过idType属性定义。

UnionID、OpenID值可以通过LoginWithHuaweiIDResponse、AuthorizationWithHuaweiIDResponse、LoginPanel或LoginWithHuaweiIDButton接口获取,具体方法参考其示例代码。

示例:

import { authentication } from '@kit.AccountKit';

// 创建请求参数
const request: authentication.StateRequest = {
  idType: authentication.IdType.UNION_ID,
  idValue: '<可通过华为账号登录接口获取>' // 该值可以通过华为账号登录接口获取
}

StateResult

该类为获取华为账号登录状态结果对象。

模型约束:此接口仅可在Stage模型下使用。

元服务API:从版本5.0.0(12)开始,该接口支持在元服务中使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:5.0.0(12)

名称

类型

只读

可选

说明

state

State

否

否

华为账号登录状态枚举对象。

示例:

import { authentication } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

// 创建请求参数
const stateRequest: authentication.StateRequest = {
  idType: authentication.IdType.UNION_ID,
  idValue: '<可通过华为账号登录接口获取>' // 该值可以通过华为账号登录接口获取
}
try {
  // 执行获取华为账号登录状态请求,并处理结果
  new authentication.HuaweiIDProvider().getHuaweiIDState(stateRequest).then((data: authentication.StateResult) => {
    hilog.info(0x0000, 'testTag', 'Succeeded in getting huaweiIdState result.');
    const state = data.state;
    // 处理state
  }).catch((err: BusinessError) => {
    this.dealAllError(err);
  })
} catch (error) {
  this.dealAllError(error);
}

// 错误处理
dealAllError(error: BusinessError): void {
  hilog.error(0x0000, 'testTag', 'Failed to getHuaweiIdState, errorCode=%{public}d, errorMsg=%{public}s', error.code, error.message);  
}

ConsistencyState

该枚举为手机号一致性状态枚举对象。

模型约束:此接口仅可在Stage模型下使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:5.0.0(12)

名称

值

说明

CONSISTENT

0

华为账号已登录,传入的手机号与当前账号绑定的手机号一致,与当前设备任意一个SIM卡手机号一致。

INCONSISTENT_WITH_DEVICES

1

华为账号已登录,传入的手机号与当前账号绑定的手机号一致,与当前设备SIM卡手机号不一致或当前设备无SIM卡。

INCONSISTENT

2

华为账号已登录,传入的手机号与当前账号绑定的手机号不一致。

ConsistencyRequest

该类为获取手机号一致性状态请求对象。

模型约束:此接口仅可在Stage模型下使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:5.0.0(12)

名称

类型

只读

可选

说明

idType

IdType

否

否

属性idValue的ID类型,当前非系统应用只能传IdType.UNION_ID或IdType.OPEN_ID。

idValue

string

否

否

用户获取的UnionID、OpenID值,不可为空,且最大长度不能超过256,传递的类型通过idType属性定义。

UnionID、OpenID值可以通过LoginWithHuaweiIDResponse、AuthorizationWithHuaweiIDResponse、LoginPanel或LoginWithHuaweiIDButton接口获取,具体方法参考其示例代码。

mobileNumber

string

否

否

通过LoginWithHuaweiIDButton组件的一键登录功能获取到的手机号,传入完整的手机号需要添加国家码,例如中国境内(不包含中国香港、中国澳门、中国台湾)为+86,值不可为空,且最大长度不能超过256。

手机号示例:+86xxxxxxxxxxx(明文手机号)

示例:

import { authentication } from '@kit.AccountKit';

// 创建请求参数
const request: authentication.ConsistencyRequest = {
  idType: authentication.IdType.UNION_ID,
  idValue: '<可通过华为账号登录接口获取>', // 该值可以通过华为账号登录接口获取
  mobileNumber: '+86xxxxxxxxxxx' // 通过华为账号一键登录功能获取到的明文手机号
}

ConsistencyResult

该类为获取手机号一致性状态结果对象。

模型约束:此接口仅可在Stage模型下使用。

系统能力:SystemCapability.AuthenticationServices.HuaweiID.Auth

起始版本:5.0.0(12)

名称

类型

只读

可选

说明

state

ConsistencyState

否

否

手机号一致性状态枚举对象。

示例:

import { authentication } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

// 创建请求参数
const consistencyRequest: authentication.ConsistencyRequest = {
  idType: authentication.IdType.UNION_ID,
  idValue: '<可通过华为账号登录接口获取>', // 该值可以通过华为账号登录接口获取
  mobileNumber: '+86xxxxxxxxxxx' // 通过华为账号一键登录功能获取到的明文手机号
}
try {
  // 执行获取手机号一致性状态请求,并处理结果
  new authentication.HuaweiIDProvider().getMobileNumberConsistency(consistencyRequest).then((data: authentication.ConsistencyResult) => {
    hilog.info(0x0000, 'testTag', 'Succeeded in getting getMobileNumberConsistency result = %{public}d', data.state);
    const state = data.state;
    // 处理state
  }).catch((err: BusinessError) => {
    this.dealAllError(err);
  })
} catch (error) {
  this.dealAllError(error);
}

// 错误处理
dealAllError(error: BusinessError): void {
  hilog.error(0x0000, 'testTag', 'Failed to get mobileNumberConsistency, errorCode=%{public}d, errorMsg=%{public}s', error.code, error.message);  
}