使用Account Kit拉起华为账号登录的方法

使用Account Kit拉起华为账号登录的方法


HarmonyOS
2024-08-07 08:39:10
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
xbibi
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}`);
    }
  }
分享
微博
QQ
微信
回复
2024-08-07 10:51:26
相关问题
使用华为账号服务登录示例代码
1821浏览 • 1回复 待解决
HarmonyOS 无法拉起账号登录页面
420浏览 • 1回复 待解决
HarmonyOS 华为账号登录报错1001500001
1455浏览 • 1回复 待解决
HarmonyOS 华为账号登录问题咨询
28浏览 • 1回复 待解决
使用Account Kit 获取用户头像昵称
417浏览 • 1回复 待解决
HarmonyOS 华为账号一键登录
59浏览 • 1回复 待解决
HarmonyOS 华为账号登录接口返回信息
685浏览 • 1回复 待解决
华为账号登录获取不到手机号
462浏览 • 1回复 待解决
HarmonyOS 登录账号问题
8浏览 • 1回复 待解决
HarmonyOS 使用华为登录API 报错1001500001
527浏览 • 1回复 待解决