【求助】HarmonyOS 鸿蒙Next 获取不到openId和unionId。

发布于 1周前 作者 bupafengyu 最后一次编辑是 5天前 来自 鸿蒙OS

【求助】HarmonyOS 鸿蒙Next 获取不到openId和unionId。

我想要获取openId和unionId根据官方的文档写了

 // 创建授权请求,并设置参数
let authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
authRequest.scopes = [‘profile’, ‘openid’];
authRequest.permissions = [‘idtoken’,‘serviceauthcode’];
authRequest.forceAuthorization = true;
authRequest.state = util.generateRandomUUID(); // 应用建议使用generateRandomUUID生成state
authRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256;
// 执行授权请求,并处理结果
try {
let controller = new authentication.AuthenticationController(getContext(this));
controller.executeRequest(authRequest, (error: BusinessError<Object>, data) => {
if (error) {
return;
}
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. %{public}s’,
JSON.stringify(authorizationWithHuaweiIDResponse));
let authorizationWithHuaweiIDCredential = authorizationWithHuaweiIDResponse.data!;
let avatarUri = authorizationWithHuaweiIDCredential.avatarUri; // 元服务不支持该字段
let nickName = authorizationWithHuaweiIDCredential.nickName; // 元服务不支持该字段
let idToken = authorizationWithHuaweiIDCredential.idToken;
let openID = authorizationWithHuaweiIDCredential.openID;
let unionID = authorizationWithHuaweiIDCredential.unionID;
let code = authorizationWithHuaweiIDCredential.authorizationCode;
// 开发者处理avatarUri, nickName, idToken, openID, unionID, code
});
} catch (error) {
}

最终openId,unionId都是undefind。请问大佬这是什么情况。

cke_18146.png


更多关于【求助】HarmonyOS 鸿蒙Next 获取不到openId和unionId。的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于【求助】HarmonyOS 鸿蒙Next 获取不到openId和unionId。的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙系统中,如果无法获取openIdunionId,请检查以下几点:

  1. 确保已正确集成华为账号服务,并在manifest.json中配置了所需的权限和回调URL。
  2. 检查API调用时传递的参数是否正确,包括应用ID和授权范围。
  3. 查看是否用户已授权登录,并且授权了必要的权限。
  4. 检查服务器端的接口逻辑,确保能够正确处理来自鸿蒙的认证请求。

如果问题还没有解决,建议看看HarmonyOS鸿蒙课程讲解:https://www.bilibili.com/video/BV1fw2ZYSEk2

配套课件及涉及源码网盘下载地址:http://bbs.itying.com/topic/6568836e6a4621002b88a6f6

回到顶部