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

使用Python调用Roblox API购买商品时遇JSON解析错误求助

调用Roblox API购买物品时遇到JSON解析错误

我正在用Python编写代码调用Roblox API,实现从Roblox商城购买物品的功能,但遇到了JSON解析错误,需要技术支持。

我的代码

import re
import json
from requests import post, get

cookie = "somethingicantshow"
ID = 1562150

# 获取x-csrf-token
token = post("https://auth.roblox.com/v2/logout", cookies={".ROBLOSECURITY": cookie}).headers['X-CSRF-TOKEN']
print(token)

# 获取物品详情
detail_res = get("https://www.roblox.com/library/" + str(ID))
text = detail_res.text

info = post("https://catalog.roblox.com/v1/catalog/items/details",
                           json={"items": [{"itemType": "Asset", "id": int(ID)}]},
                           headers={"x-csrf-token": token}, cookies={".ROBLOSECURITY": cookie}).json()["data"][0]

productId = post("https://apis.roblox.com/marketplace-items/v1/items/details",
                   json={"itemIds": [info.get("collectibleItemId")]},
                   headers={"x-csrf-token": token}, cookies={".ROBLOSECURITY": cookie})
productId = productId.json()[0]["collectibleProductId"]
expectedPrice = 0 #int(re.search("data-expected-price=\"(\d+)\"", text).group(1))
expectedSellerId = 1 #int(re.search("data-expected-seller-id=\"(\d+)\"", text).group(1))

headers = {
    "x-csrf-token": token,
    "content-type": "application/json; charset=UTF-8"
}

data = {
    "expectedCurrency": 1,
    "expectedPrice": expectedPrice,
    "expectedSellerId": expectedSellerId
}

dataLoad = json.dumps(data)
buyres = post("https://economy.roblox.com/v1/purchases/products/" + str(productId), headers=headers,
              data=dataLoad,
              cookies={".ROBLOSECURITY": cookie})

if buyres.status_code == 200:
    print("Successfully bought item")
else:
    print("failed" + str(buyres.status_code))

报错信息

Traceback (most recent call last):
  File "C:\Users\Aarush Kumar\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aarush Kumar\AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aarush Kumar\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aarush Kumar\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Aarush Kumar\Downloads\limitedsnipe\limitedSniper.py", line 24, in <module>
    productId = productId.json()[0]["collectibleProductId"]
                ^^^^^^^^^^^^^^^^
  File "C:\Users\Aarush Kumar\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我已经尝试多种方法,并查阅了Roblox开发者论坛的相关内容,但由于这是Python相关错误,特此寻求帮助。


解决方案

报错出现在第24行调用.json()时,说明这个请求的响应不是合法的JSON格式,大概率是请求失败返回了空内容或HTML错误页面,按以下步骤排查:

  1. 先检查请求状态和响应内容
    在调用.json()前,先打印请求的状态码和响应文本,确认问题:

    # 替换原productId请求部分
    productId_res = post("https://apis.roblox.com/marketplace-items/v1/items/details",
                       json={"itemIds": [info.get("collectibleItemId")]},
                       headers={"x-csrf-token": token},
                       cookies={".ROBLOSECURITY": cookie})
    print("请求状态码:", productId_res.status_code)
    print("响应内容:", productId_res.text)
    

    如果状态码不是200,比如401(未授权)、404(资源不存在),说明请求本身就失败了,自然无法解析JSON。

  2. 验证collectibleItemId是否有效
    检查info.get("collectibleItemId")是否返回了有效值,有可能该物品不是可收藏物品,这个字段不存在,导致请求的itemIds为空或无效,Roblox API返回错误。可以打印info查看所有字段:

    print("物品详情:", info)
    
  3. 确认Cookie有效性
    检查.ROBLOSECURITY cookie是否过期、权限是否足够,Roblox会定期失效Cookie,需要确保使用的是当前有效的Cookie。

  4. 添加必要请求头
    Roblox API可能会验证User-Agent,尝试添加浏览器类型的User-Agent头:

    headers = {
        "x-csrf-token": token,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
    }
    
  5. 处理可能的API限制
    Roblox有请求频率限制,避免短时间内重复请求,必要时添加请求间隔。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 13:35:06