使用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 获取用户头像昵称授权
108浏览 • 1回复 待解决
HarmonyOS如何获取用户相册所有目录?
1703浏览 • 1回复 待解决
Data Guard Kit权限获取
1624浏览 • 1回复 待解决
怎么获取当前用户用户ID
447浏览 • 1回复 待解决
使用Ability Kit报错16000001
190浏览 • 1回复 待解决
Payment Kit的签名公私钥怎么获取
1243浏览 • 1回复 待解决
用户相册, 获取不到albumName
1521浏览 • 1回复 待解决
Weather Service Kit获取天气数据
154浏览 • 1回复 待解决
Network Kit(网络服务)中使用DoH
133浏览 • 1回复 待解决
选择头像Button的实现
110浏览 • 1回复 待解决
HarmonyOS头像上传问题
129浏览 • 1回复 待解决
来电横幅通知头像无法显示
1594浏览 • 1回复 待解决
进入相册或拍照选择图片做头像
13132浏览 • 2回复 已解决
关于群聊微信朋友头像的问题!
6522浏览 • 2回复 待解决