You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

容器内运行测试报错KeyError: 'user_name'排查求助

容器内测试报错:KeyError: 'user_name'

在容器内运行测试时,所有测试均返回以下错误:

E               KeyError: 'user_name'

错误详情

args = (<api.views.Connector object at 0x7f5c66288d30>,)
kwargs = {'connector': 'googleanalytics', 'user_info': {'active': True, 'adwords_account': None, 'adwords_include_productgroup_data': None, 'adwords_medium_value': None, ...}}
start_time = 1676136763.008559, error_response = <Response 27 bytes [400 BAD REQUEST]>, api_key = '2ee6af7ff92f753363d420fc4a2b1233f136'
auth_token = None
users = [{'active': True, 'adwords_account': None, 'adwords_include_productgroup_data': None, 'adwords_medium_value': None, ...}]

    @wraps(f)
    def decorated_function(*args, **kwargs):
        start_time = time.time()

        error_response = jsonify({"error": "未授权"})
        error_response.status_code = 400

        # 检查用户是否通过api_key进行认证
        api_key = request.values.get("api_key")
        auth_token = request.values.get("access_token")

        if api_key:
            logger.info(
                "OAuth验证请求,api_key=%s : %s 秒",
                api_key,
                time.time() - start_time,
            )

            users = api_helpers.get_users_db_info({"user.api_key": f"$eq.{api_key}"})

            logger.info(
                "OAuth验证返回数据,api_key=%s : %s 秒",
                api_key,
                time.time() - start_time,
            )

            # 如果通过指定key找到用户
            # 设置`user`参数并运行连接器
            if len(users) > 0:
                kwargs["user_info"] = users[0]

                logger.info(
                    "OAuth验证耗时,api_key=%s user_name=%s : %s 秒",
                    api_key,
>                   kwargs["user_info"]["user_name"],
                    time.time() - start_time,
                )
E               KeyError: 'user_name'

../api/oauth.py:131: KeyError

问题原因

报错发生在oauth.py第131行,代码尝试获取kwargs["user_info"]["user_name"],但当前从数据库返回的用户数据中不存在user_name字段。从打印的users和kwargs内容可以看到,返回的用户字典里只有active、adwords_account等字段,没有user_name。

解决建议

  • 检查数据库中对应用户记录,确认是否存在user_name字段,若不存在则需要添加该字段,或者改用现有可用的用户标识字段(比如邮箱)
  • 查看api_helpers.get_users_db_info方法的实现,确认查询逻辑是否遗漏了user_name字段的获取,调整查询语句确保返回该字段
  • 检查容器内测试环境的数据库数据,确认测试用户数据是否完整,是否缺少user_name字段

内容的提问来源于stack exchange,提问作者visiwat303

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 12:25:14