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

调用DMarket API获取订单时出现404错误的技术求助

解决DMarket API 404错误的方案

你的代码出现404错误,核心原因是API请求路径不正确,DMarket当前的用户订单相关接口已调整到/exchange/v1前缀下,而非代码里的/marketplace-api/v1。以下是具体修复步骤:

1. 修正API路径

修改代码中两个接口的路径:

  • 获取用户订单的路径从/marketplace-api/v1/user-orders改为/exchange/v1/user-orders
  • 删除订单的路径从/marketplace-api/v1/orders/{order_id}改为/exchange/v1/orders/{order_id}

2. 修复后的完整代码

import time
import requests
from nacl.bindings import crypto_sign

PUBLIC_KEY = "你的公钥"  # 请填写你的DMarket公钥
SECRET_KEY = "你的私钥"  # 请填写你的DMarket私钥

ROOT_API_URL = "https://api.dmarket.com"


def sign_request(method, path, body=""):
    nonce = str(int(time.time()))
    string_to_sign = method + path + body + nonce
    signature = crypto_sign(string_to_sign.encode(),
                            bytes.fromhex(SECRET_KEY))[:64].hex()
    headers = {
        "X-Api-Key": PUBLIC_KEY,
        "X-Request-Sign": f"dmar ed25519 {signature}",
        "X-Sign-Date": nonce,
        "Content-Type": "application/json"
    }
    return headers


def get_user_orders():
    # 修正路径
    path = "/exchange/v1/user-orders"
    headers = sign_request("GET", path)
    # 可选:添加查询参数过滤订单类型(buy/sell),比如?type=buy
    response = requests.get(f"{ROOT_API_URL}{path}", headers=headers)
    if response.status_code == 200:
        return response.json().get("orders", [])
    else:
        print(f"[!] 接收订单失败: {response.status_code}, {response.text}")
        return []


def delete_order(order_id):
    # 修正路径
    path = f"/exchange/v1/orders/{order_id}"
    headers = sign_request("DELETE", path)
    response = requests.delete(f"{ROOT_API_URL}{path}", headers=headers)
    if response.status_code == 200:
        print(f"[-ORDER] 已删除订单: {order_id}")
    else:
        print(f"[!] 删除订单 {order_id} 失败: {response.status_code}, {response.text}")


def main():
    print("\n🧹 正在获取并删除订单...")
    orders = get_user_orders()
    if not orders:
        print("无活跃订单。")
        return
    for order in orders:
        order_id = order.get("orderId")
        if order_id:
            delete_order(order_id)


if __name__ == "__main__":
    main()

3. 额外检查项

  • 确认PUBLIC_KEY和SECRET_KEY已正确填写,且权限足够(需包含订单管理权限)
  • 若仍有问题,可在获取订单时添加查询参数过滤,比如?type=buy或?type=sell,明确获取买入/卖出订单
  • 验证签名逻辑:DMarket要求签名字符串为方法+路径+请求体+时间戳,你的代码逻辑是正确的,无需修改

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 16:45:15