HarmonyOS 使用华为帐号一键登录无法获取匿名手机号?

调用华为账号api进行华为帐号一键登录,返回的结果里面只有unionID和openID,没有匿名手机号anonymousPhone。

阅读 543
1 个回答

确认下scope权限有没有申请,可参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/account-config-permissions-V5\#section132012717318

刚申请完权限可能需要将系统时间调后24小时。

保证华为帐号一键登录敏感权限已获取。

getQuickLoginAnonymousPhone() {
  // 创建授权请求,并设置参数。
  let authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
  // 获取手机号需要传如下scope,传参数之前需要先申请对应scope权限,才能返回对应数据。
  authRequest.scopes = ['quickLoginAnonymousPhone'];
  // 用于防跨站点请求伪造,非空字符串即可。
  authRequest.state = util.generateRandomUUID();
  authRequest.forceAuthorization = false;
  let controller = new authentication.AuthenticationController(getContext(this));
  try {
    controller.executeRequest(authRequest).then((response: authentication.AuthorizationWithHuaweiIDResponse) => {
      let anonymousPhone = response.data?.extraInfo?.quickLoginAnonymousPhone;
      if (anonymousPhone) {
        hilog.info(0x0000, 'testTag', 'authorization success');
        return;
      }
      hilog.info(0x0000, 'testTag', 'authorization success anonymousPhone is empty');
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'testTag', 'authorization fail: %{public}s', JSON.stringify(err));
    })
  } catch (err) {
    hilog.error(0x0000, 'testTag', 'authorization fail: %{public}s', JSON.stringify(err));
  }
}

建议在跟页面通过Router跳转,进入页面以后再用navigation,参考代码:

let observer: UIObserver = this.getUIContext().getUIObserver();
observer.on('navDestinationUpdate', (info: observer.NavDestinationInfo) => {
  let navDestinationInfo: observer.NavDestinationInfo | undefined = this.queryNavDestinationInfo();
  if (info?.navDestinationId !== navDestinationInfo?.navDestinationId) {
    return;
  }

  if (info.state === uiObserver.NavDestinationState.ON_SHOWN) {
    hilog.info(domainId, loginPanelTag, 'observerComponentState navDestinationInfo ON_SHOWN');
  } else if (info.state === uiObserver.NavDestinationState.ON_HIDDEN) {
    hilog.info(domainId, loginPanelTag, 'observerComponentState navDestinationInfo ON_HIDDEN');
  }
});
}
logo
HarmonyOS
子站问答
访问
宣传栏