使用Account Kit 获取用户头像昵称

使用Account Kit 获取用户头像昵称

HarmonyOS
2024-08-07 08:40:24
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
鸡翅可乐
private getAvatarAndNickNameWithCallback(): void {
    hilog.info(0x0000, 'testTag', 'getAvatarAndNickNameWithCallback');
    // 创建授权请求,并设置参数
    let authRequest: authentication.AuthorizationWithHuaweiIDRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
    // 获取头像昵称需传如下scope
    authRequest.scopes = ['profile'];
    // forceAuthorization参数用来控制是否拉起授权页面
    authRequest.forceAuthorization = true;
    // 用于防跨站点请求伪造,推荐采用如下方式给state赋值
    authRequest.state = util.generateRandomUUID();

    // 执行授权请求
    try {
      let controller: authentication.AuthenticationController = new authentication.AuthenticationController(getContext(this));
      controller.executeRequest(authRequest).then((data: authentication.AuthenticationResponse) => {
        let authorizationWithHuaweiIDResponse = data as authentication.AuthorizationWithHuaweiIDResponse;
        let state: string = authorizationWithHuaweiIDResponse.state!;
        if (authRequest.state != state) {
          hilog.error(0x0000, 'testTag', 'Failed to authorize. The state is different');
          this.result = 'cancel false: The state is different.';
          return;
        }
        let authorizationWithHuaweiIDCredential: authentication.AuthorizationWithHuaweiIDCredential = authorizationWithHuaweiIDResponse.data!;
        let avatarUri: string = authorizationWithHuaweiIDCredential.avatarUri!;
        let nickName: string = authorizationWithHuaweiIDCredential.nickName!;
        this.result = 'avatarUri:' + avatarUri + '  nickName:' + nickName;
      }).catch((err: BusinessError) => {
        hilog.error(0x0000, 'testTag', `Failed to getAvatarAndNickNameWithCallback. BusinessError Code: ${err.code}, message: ${err.message}`);
      })
    } catch (error) {
      hilog.error(0x0000, 'testTag', `Failed to getAvatarAndNickNameWithCallback. Code: ${error.code}, message: ${error.message}`);
    }
  }
分享
微博
QQ
微信
回复
2024-08-07 10:53:07
相关问题
取消Account Kit 获取用户头像昵称授权
414浏览 • 1回复 待解决
HarmonyOS如何获取用户相册所有目录?
1981浏览 • 1回复 待解决
HarmonyOS 如何获取用户所在省市区?
106浏览 • 1回复 待解决
Data Guard Kit权限获取
1802浏览 • 1回复 待解决
怎么获取当前用户用户ID
604浏览 • 1回复 待解决