HarmonyOS鸿蒙Next中云开发(Serverless)AGC认证服务auth.signIn华为账号登录获取头像昵称等信息

HarmonyOS鸿蒙Next中云开发(Serverless)AGC认证服务auth.signIn华为账号登录获取头像昵称等信息

搞了我一晚上,各种查资料、问AI,事实证明AI还是一本正经的胡说八道

背景

端云一体化开发项目,使用AGC认证服务进行华为账号登录

代码中通过以下方式进行登录,然后就能获取华为账号的头像、昵称等信息

auth.signIn({
      autoCreateUser: true,
      credentialInfo: {
        kind: 'hwid'
      },
  }).then(signInResult => {
    let userResult = signInResult.getUser();
  })

问题

方法返回的signInResult,通过 getUser() 方法可以返回当前登录的用户信息,包括用户名称、头像、uid等

但是当你通过 userResult.getDisplayName() / userResult.getPhotoUrl() 获取名称/头像时,发现全是空的,整个 userResult 几乎除了id外全是空的!

cke_58900.png

然后我就以为是没权限,或者没申请获取头像,问AI,也是这么说

可是,auth.signIn 方法的参数里就没有提供权限申请的配置

解决

我得出结论:头像昵称,auth.signIn 方法不会自动获取,需要单独调用 Account Kit(华为账号服务)进行获取

然后通过 auth.signIn -> signInResult对象的getUser方法 -> userResult对象的updateProfile方法,对头像昵称进行数据更新存储,下次再调用auth.signIn,就会拿到你存进去的头像和昵称了

以下是实现代码,不是完整示例,缺少实际判断逻辑,仅做用法展示

auth.signIn({
    autoCreateUser: true,
    credentialInfo: {
      kind: 'hwid'
    },
  }).then(signInResult => {
    let userResult = signInResult.getUser();
    // 此处可获取头像昵称
    userResult.updateProfile({
      displayName: "获取到的昵称",
      photoUrl: "获取到的头像地址"
    }).then(() => {
      // 可以重新获取用户信息,也可直接更改storage存储的用户信息
      auth.getCurrentUser().then(user => {
        if (user) {
          let phone = user.getPhotoUrl();
        }
      })
    })
    Logger.info(undefined, TAG, `华为账号登录成功。 result: ${userResult.getUid()}`)
  }).catch((error: BusinessError) => {
    Logger.error(undefined, TAG, `华为账号登陆失败, Code: ${error.code}, message: ${error.message}`)
  })
// 创建授权请求,并设置参数
const authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
// 获取头像昵称需要传如下scope
authRequest.scopes = ['profile'];
// 用户是否需要登录授权,该值为true且用户未登录或未授权时,会拉起用户登录或授权页面
authRequest.forceAuthorization = true;
// 用于防跨站点请求伪造
authRequest.state = util.generateRandomUUID();
try {
  // 此示例为代码片段,实际需在自定义组件实例中使用,以获取UIContext对象作为函数入参
  const controller = new authentication.AuthenticationController(this.getUIContext().getHostContext());
  controller.executeRequest(authRequest).then((data) => {
    const authorizationWithHuaweiIDResponse = data as authentication.AuthorizationWithHuaweiIDResponse;
    const state = authorizationWithHuaweiIDResponse.state;
    if (state && authRequest.state !== state) {
      return;
    }
    const authorizationWithHuaweiIDCredential = authorizationWithHuaweiIDResponse?.data;
    // 这里就是获取的头像、昵称
    const avatarUri = authorizationWithHuaweiIDCredential?.avatarUri;
    const nickName = authorizationWithHuaweiIDCredential?.nickName;
    // 开发者处理avatarUri, nickName
    const authorizationCode = authorizationWithHuaweiIDCredential?.authorizationCode;
    // 涉及服务端开发以获取头像昵称场景,开发者处理authorizationCode
  }).catch((err: BusinessError<string>) => {
    console.log(err.name);
  });
} catch (error) {
  console.log(error);
}

若有不对的地方,或者有其他获取头像昵称的方式,欢迎评论区指正,感谢!


更多关于HarmonyOS鸿蒙Next中云开发(Serverless)AGC认证服务auth.signIn华为账号登录获取头像昵称等信息的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

HarmonyOS Next云开发AGC认证服务中,通过auth.signIn登录华为账号后,使用getCurrentUser()获取用户对象,再调用getUserInfo()方法可获取头像、昵称等用户信息。

更多关于HarmonyOS鸿蒙Next中云开发(Serverless)AGC认证服务auth.signIn华为账号登录获取头像昵称等信息的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


你的分析和解决方案基本正确,这是一个在HarmonyOS Next开发中常见的理解误区。

核心问题在于:AGC认证服务(Auth Service)的signIn方法主要职责是完成用户身份验证(登录/注册),并管理一套独立的用户体系(UID、邮箱等)。它默认并不直接同步华为账号(Huawei ID)的详细个人资料(如头像、昵称)。

你的解决路径是标准的:

  1. 使用AGC Auth Service完成登录:获取到代表用户的User对象。
  2. 通过Account Kit(华为账号服务)申请profile权限并获取资料:这是获取华为账号头像、昵称等信息的正确途径。你代码中使用的authentication.HuaweiIDProvider和相关授权流程是正确的。
  3. 将获取的资料更新至AGC用户档案:通过User.updateProfile()方法,将昵称和头像URL保存到AGC侧的用户信息中。此后,通过auth.getCurrentUser()获取到的用户对象便会包含这些信息。

关键点澄清:

  • 权限分离:华为账号(Account Kit)的详细资料访问需要用户明确授权(scope: 'profile'),这是一个独立的OAuth授权流程,与基础的登录认证(signIn)是分开的。
  • 信息存储updateProfile后,信息存储在AGC的认证服务中,与华为账号侧的原始资料是独立的副本。如果用户在华为账号侧更改了头像,需要重新执行授权和更新流程才能同步到你的应用内。

代码补充说明: 你提供的Account Kit授权代码片段是核心。在实际开发中,通常需要将步骤2(获取资料)和步骤3(更新档案)进行逻辑串联,并考虑错误处理和用户拒绝授权等场景。

你的总结非常到位,帮助厘清了AGC Auth Service与Account Kit在获取用户信息上的分工,这对其他开发者是很有价值的经验分享。

回到顶部