网站建设与维护的选择题,editplus怎么创网站,网站建设的完整流程图,访问wordpress数据库场景介绍
如应用需要完善用户头像昵称信息#xff0c;可使用Account Kit提供的头像昵称授权能力#xff0c;用户允许应用获取头像昵称后#xff0c;可快速完成个人信息填写。以下只针对Account kit提供的头像昵称授权能力进行介绍#xff0c;若要获取头像还可通过场景化控…场景介绍
如应用需要完善用户头像昵称信息可使用Account Kit提供的头像昵称授权能力用户允许应用获取头像昵称后可快速完成个人信息填写。以下只针对Account kit提供的头像昵称授权能力进行介绍若要获取头像还可通过场景化控件选择头像Button进行获取。 业务流程 流程说明
应用传对应scope调用授权API请求获取用户头像昵称。如用户已给应用授权则开发者能直接获取用户头像昵称、UnionID、OpenID。如用户未授权则授权请求会拉起授权页面在用户确认授权后开发者能获取到用户头像昵称、UnionID、OpenID。获取到头像信息开发者可以下载该url使用该头像。
接口说明
获取头像昵称关键接口如下表所示具体API说明详见API参考。 接口名 描述 createAuthorizationWithHuaweiIDRequest(): AuthorizationWithHuaweiIDRequest 获取授权接口通过AuthorizationWithHuaweiIDRequest传入头像昵称的scopeprofile及Authorization Code的permissionserviceauthcode即可在授权结果中获取到用户头像昵称、UnionID、OpenID和Authorization Code。 constructor(context?: common.Context) 创建授权请求Controller。 executeRequest(request: AuthenticationRequest): PromiseAuthenticationResponse 通过Promise方式执行授权操作。 头像昵称可从AuthenticationResponse的子类AuthorizationWithHuaweiIDResponse中解析具体解析方法请参考客户端开发的示例代码。
注意
1.上述接口需在页面或自定义组件生命周期内调用。
2.未设置昵称默认返回华为账号绑定的匿名手机号/邮箱。
开发前提
在进行代码开发前请先确认您已完成配置Client ID工作。该场景无需申请scope权限。
开发步骤
客户端开发
导入authentication模块及相关公共模块。 import { authentication } from kit.AccountKit;import { hilog } from kit.PerformanceAnalysisKit;import { util } from kit.ArkTS;import { BusinessError } from kit.BasicServicesKit;创建授权请求并设置参数。 // 创建授权请求并设置参数const authRequest new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();// 获取头像昵称需要传如下scopeauthRequest.scopes [profile];// 若开发者需要进行服务端开发则需传如下permission获取authorizationCodeauthRequest.permissions [serviceauthcode];// 用户是否需要登录授权该值为true且用户未登录或未授权时会拉起用户登录或授权页面authRequest.forceAuthorization true;// 用于防跨站点请求伪造authRequest.state util.generateRandomUUID();调用AuthenticationController对象的executeRequest方法执行授权请求并处理授权结果从授权结果中解析出头像昵称、UnionID、OpenID和Authorization Code。 // 执行授权请求try {const controller new authentication.AuthenticationController(getContext(this));controller.executeRequest(authRequest).then((data) {const authorizationWithHuaweiIDResponse data as authentication.AuthorizationWithHuaweiIDResponse;const 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.);const authorizationWithHuaweiIDCredential authorizationWithHuaweiIDResponse.data!;const avatarUri authorizationWithHuaweiIDCredential.avatarUri;const nickName authorizationWithHuaweiIDCredential.nickName;const unionID authorizationWithHuaweiIDCredential.unionID;const openID authorizationWithHuaweiIDCredential.openID;const authorizationCode authorizationWithHuaweiIDCredential.authorizationCode;// 开发者处理avatarUri, nickName, unionIDopenIDauthorizationCode}).catch((err: BusinessError) {this.dealAllError(err);});} catch (error) {this.dealAllError(error);}// 错误处理dealAllError(error: BusinessError): void {hilog.error(0x0000, testTag, Failed to auth. Code: ${error.code}, message: ${error.message});}
服务端开发可选
开发者根据业务需要选择是否进行服务端开发。
应用服务器使用Client ID、Client Secret、Authorization Code调用获取用户级凭证的接口向华为账号服务器请求获取Access Token、Refresh Token。使用Access Token调用获取用户信息接口获取用户信息从用户信息中获取用户头像昵称。 Access Token过期处理 由于Access Token的有效期仅为60分钟当Access Token失效或者即将失效时可通过REST API错误码判断可以使用Refresh Token有效期180天通过刷新凭证向华为账号服务器请求获取新的Access Token。 说明 当Access Token失效时若您不使用Refresh Token向账号服务器请求获取新的Access Token账号的授权信息将会失效导致使用Access Token的功能都会失败。当Access Token非正常失效如修改密码、退出账号、删除设备时业务可重新登录授权获取Authorization Code向账号服务器请求获取新的Access Token。Refresh Token过期处理 由于Refresh Token的有效期为180天当Refresh Token失效后可通过REST API错误码判断应用服务器端需要通知客户端重新调用授权接口请求用户重新授权。