当前位置: 首页 > news >正文

【HarmonyOS 5 开发速记】如何获取用户信息(头像/昵称/手机号)

1.获取 authorizationCode:

2.利用 authorizationCode 获取 accessToken:文档中心

3.获取手机:文档中心

4.获取昵称头像:文档中心

首先创建 request

若要获取手机号,scope必填 phone,permissions 必填 serviceauthcode,否则第 3 步将无法返回预期的字段,第 3 步与第 4 步调用接口一致,报文返回结果根据 scope 进行返回

// 创建授权请求,并设置参数
const authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
authRequest.scopes = ['phone']; // 元服务不支持profile,需传其他支持的scope。默认返回UnionID、OpenID
authRequest.permissions = ['serviceauthcode']; 
authRequest.forceAuthorization = true;
authRequest.state = util.generateRandomUUID(); // 建议使用generateRandomUUID生成state
authRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256;

 若要获取昵称,头像,scope必填 phone,permissions 必填 serviceauthcode。

// 创建授权请求,并设置参数
const authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
authRequest.scopes = ['profile']; // 元服务不支持profile,需传其他支持的scope。默认返回UnionID、OpenID
authRequest.permissions = ['serviceauthcode']; 
authRequest.forceAuthorization = true;
authRequest.state = util.generateRandomUUID(); // 建议使用generateRandomUUID生成state
authRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256;

根据接口调用返回的 authorizationCode,去调用 RESET公共API:

https://account.cloud.huawei.com/rest.php?nsp_svc=GOpen.User.getInfo

接口文档: 文档中心

// 执行授权请求,并处理结果
try {const controller = new authentication.AuthenticationController(getContext(this));controller.executeRequest(authRequest, (error: BusinessError<Object>, data) => {if (error) {this.dealAllError(error);return;}const authorizationWithHuaweiIDResponse = data as authentication.AuthorizationWithHuaweiIDResponse;const state = authorizationWithHuaweiIDResponse.state;if (state && 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.');const authorizationWithHuaweiIDCredential = authorizationWithHuaweiIDResponse.data!;const avatarUri = authorizationWithHuaweiIDCredential.avatarUri; // 元服务不支持该字段const nickName = authorizationWithHuaweiIDCredential.nickName; // 元服务不支持该字段const idToken = authorizationWithHuaweiIDCredential.idToken;const openID = authorizationWithHuaweiIDCredential.openID;const unionID = authorizationWithHuaweiIDCredential.unionID;const code = authorizationWithHuaweiIDCredential.authorizationCode;// 开发者处理avatarUri, nickName, idToken, openID, unionID, code});
} catch (error) {this.dealAllError(error);
}// 错误处理
dealAllError(error: BusinessError<Object>): void {hilog.error(0x0000, 'testTag', `Failed to auth. Code: ${error.code}, message: ${error.message}`);
}

接口即可正常返回预期报文。

http://www.dtcms.com/a/240002.html

相关文章:

  • 自然语言处理——循环神经网络
  • Oracle常见进程杀进程测试
  • 《从零掌握MIPI CSI-2: 协议精解与FPGA摄像头开发实战》-- CSI-2 协议详细解析(四)DPHY ECC
  • 在阿里云上搭建n8n
  • Deepseek大模型私有化部署
  • 【论文阅读】:Weighted Graph Cuts without Eigenvectors:A Multilevel Approach
  • 精益数据分析(98/126):电商转化率优化与网站性能的底层逻辑
  • LabVIEW超声频率跟踪
  • C++实现分布式网络通信框架RPC(2)——rpc发布端
  • JS红宝书笔记 10.11-10.16 函数
  • 基于 Three.js 的数字雨波纹效果技术解析
  • 数据库分批入库
  • android13 app的触摸问题定位分析流程
  • Springboot项目集成Ai模型(阿里云百炼-DeepSeek)
  • NLP-数据集
  • 深入剖析AI大模型:大模型时代的 Prompt 工程全解析
  • 【系统架构设计师-2025上半年真题】案例分析-参考答案及部分详解(回忆版)
  • Blazor-Ant Design of Blazor快速开始
  • EXCEL 实现“点击跳转到指定 Sheet”的方法
  • 【Kafka】Kafka从入门到实战:构建高吞吐量分布式消息系统
  • WinUI3开发_使用mica效果
  • Ubuntu 可执行程序自启动方法
  • 【动态规划 数论】P9759 [COCI 2022/2023 #3] Bomboni|普及+
  • Maven 多仓库治理与发布策略深度实践
  • AD学习(3)
  • 教程:PyCharm 中搭建多级隔离的 Poetry 环境(从 Anaconda 到项目专属.venv)
  • pycharm 设置环境出错
  • P3 QT项目----记事本(3.8)
  • 【字节拥抱开源】字节团队开源视频模型 ContentV: 有限算力下的视频生成模型高效训练
  • PostgreSQL 对 IPv6 的支持情况