HarmonyOS鸿蒙Next开发华为一键登录过程中,华为账号未登录时怎么拉起登录页
HarmonyOS鸿蒙Next开发华为一键登录过程中,华为账号未登录时怎么拉起登录页 在预取号阶段,若未登录华为账号,调用AuthorizationWithHuaweiIDRequest,并将其中forceAuthorization参数设置为true。具体代码怎么实现?
3 回复
具体代码:
- 创建授权请求并设置参数。
// 创建授权请求,并设置参数
const authRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
// 获取手机号需要传如下scope,传参数之前需要先申请对应scope权限,否则会返回1001502014错误码
authRequest.scopes = ['phone'];
// 获取authorizationCode需传如下permission
authRequest.permissions = ['serviceauthcode'];
// 用户是否需要登录授权,该值为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) {
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 authorizationCode = authorizationWithHuaweiIDCredential?.authorizationCode;
// 开发者处理authorizationCode
}).catch((err: BusinessError) => {
dealAllError(err);
});
} catch (error) {
dealAllError(error);
}
- 错误处理
// 错误处理
function dealAllError(error: BusinessError): void {
hilog.error(0x0000, 'testTag', `Failed to obtain userInfo. Code: ${error.code}, message: ${error.message}`);
// 在应用快速验证手机号场景下,涉及UI交互时,建议按照如下错误码指导提示用户
if (error.code === ErrorCode.ERROR_CODE_LOGIN_OUT) {
// 用户未登录华为账号,请登录华为账号并重试
} else if (error.code === ErrorCode.ERROR_CODE_NETWORK_ERROR) {
// 网络异常,请检查当前网络状态并重试
} else if (error.code === ErrorCode.ERROR_CODE_USER_CANCEL) {
// 用户取消授权
} else if (error.code === ErrorCode.ERROR_CODE_SYSTEM_SERVICE) {
// 系统服务异常,请稍后重试
} else if (error.code === ErrorCode.ERROR_CODE_REQUEST_REFUSE) {
// 重复请求,应用无需处理
} else {
// 获取用户信息失败,请尝试使用其他方式登录
}
}
export enum ErrorCode {
// 账号未登录
ERROR_CODE_LOGIN_OUT = 1001502001,
// 网络错误
ERROR_CODE_NETWORK_ERROR = 1001502005,
// 用户取消授权
ERROR_CODE_USER_CANCEL = 1001502012,
// 系统服务异常
ERROR_CODE_SYSTEM_SERVICE = 12300001,
// 重复请求
ERROR_CODE_REQUEST_REFUSE = 1001500002
}
更多关于HarmonyOS鸿蒙Next开发华为一键登录过程中,华为账号未登录时怎么拉起登录页的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next开发中,当华为账号未登录时,使用AccountAuthManager.requestAuth方法可拉起登录页。该方法通过AuthParam配置认证参数,如AuthScope.OPENID,并调用requestAuth触发登录界面。用户完成授权后,通过回调返回AuthResult处理登录结果。无需额外处理未登录状态,系统自动管理登录流程。
在HarmonyOS Next中,当检测到华为账号未登录时,可以通过设置AuthorizationWithHuaweiIDRequest的forceAuthorization参数为true来强制拉起登录页面。以下是具体代码实现:
import { account } from '@kit.AccountKit';
// 创建授权请求对象
let authRequest: account.AuthorizationWithHuaweiIDRequest = {
forceAuthorization: true // 强制拉起登录页
};
// 执行授权请求
try {
let authResult = await account.authorize(authRequest);
console.log('Authorization successful, access token: ' + authResult.accessToken);
} catch (err) {
console.error('Authorization failed, error: ' + JSON.stringify(err));
}
关键点说明:
forceAuthorization: true确保在账号未登录时自动弹出登录界面- 使用
account.authorize()异步方法执行授权流程 - 授权成功后可通过
authResult.accessToken获取访问令牌 - 建议在try-catch中处理授权异常情况
此实现适用于HarmonyOS Next的Stage模型,确保在module.json5中已声明ohos.permission.ACCOUNT_MANAGER权限。

