使用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
相关问题
使用华为账号服务登录示例代码
1466浏览 • 1回复 待解决
HarmonyOS 无法拉起账号登录页面
88浏览 • 1回复 待解决
HarmonyOS 华为账号登录报错1001500001
302浏览 • 1回复 待解决
HarmonyOS 华为账号登录接口返回信息
203浏览 • 1回复 待解决
华为账号登录获取不到手机号
151浏览 • 1回复 待解决
使用Account Kit 获取用户头像昵称
111浏览 • 1回复 待解决
取消Account Kit 获取用户头像昵称授权
114浏览 • 1回复 待解决
HarmonyOS 怎么判断登录账号有变动
74浏览 • 1回复 待解决
使用华为账号DEMO需要应用上架么
568浏览 • 1回复 待解决
华为账号对外开放范围
1286浏览 • 1回复 待解决
如何拉起华为app界面
1922浏览 • 0回复 待解决
华为账户登录release环境报错1001500001
161浏览 • 1回复 待解决
华为账号实时验证/二次放号相关咨询
1456浏览 • 1回复 待解决