参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/account-phone-unionid-login-V5
代码直接使用参考文档中的:
getQuickLoginAnonymousPhone() {
// 创建授权请求,并设置参数
const authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
// 获取匿名手机号需传quickLoginAnonymousPhone这个scope,传参之前需要先申请“华为账号一键登录”权限
//(权限名称为:quickLoginMobilePhone),后续才能获取匿名手机号数据
authRequest.scopes = ['quickLoginAnonymousPhone'];
// 用于防跨站点请求伪造
authRequest.state = util.generateRandomUUID();
// 一键登录场景该参数只能设置为false
authRequest.forceAuthorization = false;
const controller = new authentication.AuthenticationController();
try {
controller.executeRequest(authRequest).then((response: authentication.AuthorizationWithHuaweiIDResponse) => {
// 获取到UnionID、OpenID、匿名手机号
const unionID = response.data?.unionID;
const openID = response.data?.openID;
const anonymousPhone = response.data?.extraInfo?.quickLoginAnonymousPhone as string;
if (anonymousPhone) {
hilog.info(this.domainId, this.logTag, 'Succeeded in authentication.');
this.quickLoginAnonymousPhone = anonymousPhone;
return;
}
hilog.info(this.domainId, this.logTag, 'Succeeded in authentication. AnonymousPhone is empty.');
// 未获取到匿名手机号需要跳转到应用自定义的登录页面
}).catch((error: BusinessError) => {
this.dealAllError(error);
})
} catch (error) {
this.dealAllError(error);
}
}
// 错误处理
dealAllError(error: BusinessError): void {
hilog.error(this.domainId, this.logTag,
`Failed to login, errorCode is ${error.code}, errorMessage is ${error.message}`);
// TODO: 错误码处理,请参考API中的错误码根据实际情况处理
}
返回错误结果:
Failed to auth, errorCode=1001500001, errorMsg=Failed to check the fingerprint of the app bundle.Fingerprint verification error.
这个1001500001错误,文档里面查不到。
1001500001表示应用指纹证书校验失败,参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/account-api-error-code-V5\#section1581681312511