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

自定义Steampy库itemordershistogram请求返回400错误求助

问题排查与解决方案

核心问题分析及修改方向

  1. URL格式问题
    Steam的itemordershistogram接口URL不需要结尾斜杠,浏览器访问的合法链接也无斜杠,你的代码中添加的结尾斜杠可能导致服务器路由解析异常。

  2. 参数类型不兼容
    Steam部分接口要求GET参数为字符串类型,你代码中传递的整数类型参数,可能触发服务器的格式校验失败。

  3. 缺失必要请求头
    Steam市场接口会校验Referer和User-Agent头,确保请求来自合法上下文。steampy默认session未携带这些头,会被服务器判定为非法请求。

  4. 调试信息不足
    原代码仅抛出状态码错误,无法获取Steam返回的具体错误描述,不利于定位问题。

修改后的函数代码

@login_required
def fetch_orders_histogram(self) -> dict:
    # 移除URL结尾的斜杠
    url = SteamUrl.COMMUNITY_URL + '/market/itemordershistogram'
    # 将数值型参数转为字符串,匹配接口要求
    params = {
        'country': 'RU',
        'language': 'russian',
        'currency': '1',
        'item_nameid': '14962984',
        'two_factor': '0'
    }
    # 添加模拟浏览器的请求头,通过服务器校验
    headers = {
        'Referer': SteamUrl.COMMUNITY_URL + '/market/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
    }
    response = self._session.get(url, params=params, headers=headers)
    
    if response.status_code == 429:
        raise TooManyRequests("You can fetch maximum 20 prices in 60s period")
    if response.status_code != 200:
        # 打印响应内容,获取Steam给出的具体错误信息
        print(f"Steam error details: {response.text}")
        raise ApiException(f"http error code: {response.status_code}, message: {response.text}")
    
    return response.json()

额外调试建议

  • 调用前可打印session的Cookie,确认是否携带steamLoginSecure、sessionid等必要验证字段:
    print(steam_client.market._session.cookies)
    
  • 若仍报错,查看打印出的Steam error details,Steam通常会返回明确的错误原因(如参数缺失、验证失败等)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 03:26:12