中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
使用Account Kit拉起华为账号登录的方法
微信扫码分享
private loginWithCallBack(): void { hilog.info(0x0000, 'testTag', 'loginWithCallBack'); // 创建登录请求,并设置参数 let loginRequest: authentication.LoginWithHuaweiIDRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest(); // 当用户未登录华为账号时,是否强制拉起华为账号登录界面 loginRequest.forceLogin = true; // 用于防跨站点请求伪造,推荐采用如下方式给state赋值 loginRequest.state = util.generateRandomUUID(); // 执行登录请求 try { let controller: authentication.AuthenticationController = new authentication.AuthenticationController(getContext(this)); controller.executeRequest(loginRequest).then((data: authentication.AuthenticationResponse) => { let loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse; let state: string = loginWithHuaweiIDResponse.state!; if (loginRequest.state != state) { hilog.error(0x0000, 'testTag', 'Failed to login. The state is different.'); this.result = 'login false: The state is different.'; return; } let loginWithHuaweiIDCredential: authentication.LoginWithHuaweiIDCredential = loginWithHuaweiIDResponse.data!; let code: string = loginWithHuaweiIDCredential.authorizationCode!; let idToken: string = loginWithHuaweiIDCredential.idToken!; let openID: string = loginWithHuaweiIDCredential.openID; let unionID: string = loginWithHuaweiIDCredential.unionID; this.result = ' openID:' + openID + ' unionID:' + unionID; // 获取code后需要将code传给应用服务器,调用华为账号服务器接口换取Access Token,然后再获取用户其他信息。 }).catch((err: BusinessError) => { hilog.error(0x0000, 'testTag', `Failed to login. BusinessError Code: ${err.code}, message: ${err.message}`); }) } catch (error) { hilog.error(0x0000, 'testTag', `Failed to login. Code: ${error.code}, message: ${error.message}`); } }