当前位置: 首页 > 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.xdnf.cn/news/948565.html

相关文章:

  • SAP Fiori UI5 开发环境搭建和部署(含增强开发)
  • 从零手写Java版本的LSM Tree (一):LSM Tree 概述
  • XXL-JOB——源码分析解读(2)
  • 什么是VR全景技术
  • 【JMeter】接口断言
  • 在WSL2的Ubuntu镜像中安装Docker
  • claude3.7高阶玩法,生成系统架构图,国内直接使用
  • CSS 工具对比:UnoCSS vs Tailwind CSS,谁是你的菜?
  • Linux信号保存与处理机制详解
  • 自然语言处理——循环神经网络
  • PKIX path building failed问题小结
  • Element-Plus:popconfirm与tooltip一起使用不生效?
  • Spring数据访问模块设计
  • Python自然语言处理库之gensim使用详解
  • Appuploader:在WindowsLinux上完成iOS APP上架的一种解决方案
  • RLHF vs RLVR:对齐学习中的两种强化方式详解
  • Rsync+inotify+nfs实现数据实时备份方案
  • Socket 编程
  • 架构设计之存储高性能——非关系型数据库(NoSQL)
  • 代购商城系统怎么选?从业务痛点看系统核心价值
  • SOC-ESP32S3部分:QA-关于唤醒词更改及配置操作步骤
  • 解锁Vscode:C/C++环境配置超详细指南
  • Python训练营---DAY49
  • 卷积神经网络设计指南:从理论到实践的经验总结
  • FDMA:解锁PL DDR性能的“高速快递系统”
  • Java 与 MySQL 性能优化:MySQL 慢 SQL 诊断与分析方法详解
  • 论文笔记:Urban Computing in the Era of Large Language Models
  • 多模态大语言模型arxiv论文略读(113)
  • Vue3+ts项目,在ts文件中导入vue文件,报错:找不到模块“./App.vue“或响应的类型声明
  • Easy Rules规则引擎:轻量级Java规则处理实践指南