HarmonyOS 鸿蒙Next使用一键登录并获取用户头像昵称信息

HarmonyOS 鸿蒙Next使用一键登录并获取用户头像昵称信息
HarmonyOS NEXT之前就接入了华为的一键登录功能,所以当用户升级到纯血鸿蒙版本的时候,就需要继续调用一键登录功能获取用户的华为侧标识编号,这样才能在服务器读取到用户的历史数据。

image.png

解决方案 上面图红色的就是华为的一键登录图标,实现这个功能不难,可以直接使用华为登录组件

          LoginWithHuaweiIDButton({
            params: {
              // LoginWithHuaweiIDButton支持的样式
              style: loginComponentManager.Style.ICON_RED,
              textAndIconStyle: true,
              // 登录按钮在登录过程中展示加载态
              extraStyle: {
                buttonStyle: new loginComponentManager.ButtonStyle().loadingStyle({
                  show: true
                })
              },
              // LoginWithHuaweiIDButton的边框圆角半径
              borderRadius: 24,
              // LoginWithHuaweiIDButton支持的登录类型
              loginType: loginComponentManager.LoginType.ID,
              // LoginWithHuaweiIDButton支持按钮的样式跟随系统深浅色模式切换
              supportDarkMode: true,
              // verifyPhoneNumber:如果用户在过去90天内未进行短信验证,是否拉起Account Kit的短信验证码页面
              verifyPhoneNumber: true
            },
            controller: this.controller
          })
        }

好了,重点是下面如果你想通过一键登录获取到用户的头像,昵称这些,你需要得到用户的授权。具体代码如下

  controller: loginComponentManager.LoginWithHuaweiIDButtonController =
    new loginComponentManager.LoginWithHuaweiIDButtonController()
      .onClickLoginWithHuaweiIDButton((error: BusinessError, response: loginComponentManager.HuaweiIDCredential) => {
        if (error) {
          this.loginId = error.code.toString()
          return;
        }

        if (response) {
          this.loginId = response.authorizationCode

          // 创建授权请求,并设置参数
          let authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
          // 获取头像昵称需要传如下scope
          authRequest.scopes = ['profile'];
          // 若开发者需要进行服务端开发,则需传如下permission获取authorizationCode
          authRequest.permissions = ['serviceauthcode'];
          // 用户是否需要登录授权,该值为true且用户未登录或未授权时,会拉起用户登录或授权页面
          authRequest.forceAuthorization = true;
          // 用于防跨站点请求伪造
          authRequest.state = util.generateRandomUUID();

          // 执行授权请求
          try {
            let controller = new authentication.AuthenticationController(getContext(this));
            controller.executeRequest(authRequest).then((data) => {
              let authorizationWithHuaweiIDResponse = data as authentication.AuthorizationWithHuaweiIDResponse;
              let state = authorizationWithHuaweiIDResponse.state;
              if (state != undefined && 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.');
              let authorizationWithHuaweiIDCredential = authorizationWithHuaweiIDResponse.data!;
              let avatarUri = authorizationWithHuaweiIDCredential.avatarUri;
              let nickName = authorizationWithHuaweiIDCredential.nickName;
              let unionID = authorizationWithHuaweiIDCredential.unionID;
              let openID = authorizationWithHuaweiIDCredential.openID;
              let authorizationCode = authorizationWithHuaweiIDCredential.authorizationCode;
              // 开发者处理avatarUri, nickName, unionID,openID,authorizationCode

              console.log(`avatarUri=${avatarUri}`)
              console.log(`nickName=${nickName}`)
              console.log(`openId=${openID}`)
              console.log(`huaweiunionID=${unionID}`)
              let userinfo = new HuaweiUserInfoModel()
              userinfo.open_id = openID??''
              userinfo.displayName = nickName??''
              userinfo.avatarUri = avatarUri??''
              userinfo.union_id = unionID??''


          }).catch((err: BusinessError) => {
            });
          } catch (error) {
          }

          return;
        }
      });

userinfo就是拿到的用户信息,这时候你可以把这个信息传到服务器端去进行匹配和保存。如果用户编号存在,你就更新原来的用户信息,并且返回用户的相关信息,如果编号不存在,那就是一个新用户,就可以创建这个用户。


更多关于HarmonyOS 鸿蒙Next使用一键登录并获取用户头像昵称信息的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS 鸿蒙Next使用一键登录并获取用户头像昵称信息的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在《便了么》APP开发技术分享二中,针对HarmonyOS(鸿蒙)Next使用一键登录并获取用户头像昵称信息的需求,可以通过以下步骤实现:

  1. 接入鸿蒙SDK:首先,确保你的开发环境已正确配置HarmonyOS SDK,并已在项目中成功引入相关依赖。

  2. 申请权限:在config.json中声明必要的权限,如网络访问、用户信息读取等,确保APP有权访问这些信息。

  3. 使用鸿蒙账号服务:鸿蒙提供了账号服务API,可用于实现一键登录。调用相关API进行用户授权,获取用户的登录凭证(如token)。

  4. 获取用户信息:使用获取的登录凭证,通过鸿蒙提供的用户信息API,请求用户的头像和昵称信息。注意处理用户隐私保护,确保在用户同意的前提下进行信息获取。

  5. 数据处理与展示:将获取到的用户头像和昵称信息进行处理,如缓存到本地或上传至服务器,并在APP界面上进行展示。

以上步骤是基于鸿蒙系统特性的基本实现流程。如果在实际开发中遇到具体的技术问题,如API调用失败、数据格式不匹配等,建议直接查阅鸿蒙官方文档或示例代码,以获取更详细的解决方案。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部