minigame-unity-webgl-transform icon indicating copy to clipboard operation
minigame-unity-webgl-transform copied to clipboard

unitySDK获取用户信息失败

Open JomW opened this issue 1 year ago • 0 comments

描述Bug

        WX.InitSDK((code) => {
            WX.Login(new LoginOption { 
                success = (res) =>
                {
                    WX.RequirePrivacyAuthorize(new RequirePrivacyAuthorizeOption
                    {
                        success = (res) =>
                        {
                            Debug.Log("同意隐私使用" + JsonMapper.ToJson(res));
                            WX.GetSetting(new GetSettingOption
                            {
                                success = (setresult) =>
                                {
                                    Debug.Log("获取设置成功" + JsonMapper.ToJson(setresult));
                                    if (setresult.authSetting["scope.userInfo"])
                                    {
                                        Debug.Log("存在userInfo");
                                        WX.GetUserInfo(new GetUserInfoOption
                                        {
                                            success = (res) =>
                                            {
                                                Debug.Log("getuserinfo\n" + JsonMapper.ToJson(res));
                                                var userInfo = res.userInfo;
                                                var nickName = userInfo.nickName;
                                                var avatarUrl = userInfo.avatarUrl;
                                                Debug.Log("userinfo:" + nickName + "//" + avatarUrl);
                                            }
                                        });
                                    }
                                    else
                                    {
                                        Debug.Log("失败");
                                        // 参数依次为 x, y, width, height, lang, withCredentials
                                        WXUserInfoButton button = WX.CreateUserInfoButton(0, 0, 400, 200, "zh_CN", true);
                                        button.Show();
                                        button.OnTap((res) =>
                                        {
                                            if (res.errCode == 0)
                                            {
                                                //用户已允许获取个人信息,返回的data即为用户信息
                                                Debug.Log(res.userInfo.nickName);
                                            }
                                            else
                                            {
                                                Debug.Log("用户未允许获取个人信息");
                                            }
                                            button.Hide();
                                        });
                                    }
                                }
                            });
                        },
                        fail = (res) =>
                        {
                            Debug.Log("不同意隐私使用");
                        }
                    });
                },
                fail = (res) =>
                {
                    Debug.Log("Login调用失败");
                }
            });
        });

按照文档的用法尝试通过scope.userInfo来做判断 结果只能获得报错 KeyNotFoundException: The given key 'scope.userInfo' was not present in the dictionary. 卡在GetSetting的success这里,连else都进不去 改了写法if (setresult.authSetting.ContainsKey("scope.userInfo"))也不行,后台管理已经打开了隐私保护指引和隐私授权弹窗

运行环境

WX-SDK 202405171605
团结引擎 1.1.3
构建平台 windows

JomW avatar Jun 02 '24 15:06 JomW