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

币安合约API调用报错-1022:签名无效问题求助(Node.js+axios场景)

错误原因排查
  • 核心错误:POST请求参数传递不符合币安API规范。你当前将所有业务参数拼接在URL查询串中,请求体传空值,但币安POST类型的接口要求application/x-www-form-urlencoded格式的参数必须放在请求体中,服务器验签时读取的参数集与你用来生成签名的参数集匹配逻辑不一致,直接触发签名无效错误。
  • 潜在问题1:price参数精度不符合要求。如果price变量的小数位数超过LTCUSDT交易对规定的价格精度,拼接后的查询串内容会和服务器实际接收的标准化参数内容不一致,导致签名校验失败。
  • 潜在问题2:参数序列化不标准。手动拼接查询串如果遇到含特殊字符的参数时,未做URL编码也会导致签名内容和实际发送内容不一致。
可行解决方案

优先使用专业的查询串序列化库处理参数,调整POST请求的参数传递位置,修复后的核心代码如下:

const qs = require('qs') 
// 构造参数对象
const params = {
  symbol: 'LTCUSDT',
  side: 'BUY',
  type: 'LIMIT',
  timeInForce: 'GTC',
  quantity: 0.05,
  price: price.toFixed(2), // 按LTCUSDT的官方价格精度调整小数位
  recvWindow: 5000,
  timestamp: Date.now()
}
// 序列化参数生成签名用字符串
const querystring = qs.stringify(params)
// 生成签名
const signature = crypto
  .createHmac('sha256', credentials.secret)
  .update(querystring)
  .digest('hex')
// 发送POST请求,参数放在请求体中
axios.post(
  'https://fapi.binance.com/fapi/v1/order',
  `${querystring}&signature=${signature}`, 
  {
    headers: {
      'X-MBX-APIKEY': credentials.key,
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  }
).then((response) => {
  console.log(response.data)
  resolve(response)
}).catch(function (error) {
  if (error.response) {
    console.log(error.response.data)
    console.log(error.response.status)
    console.log(error.response.headers)
  } else if (error.request) {
    console.log(error.request)
  } else {
    console.log('Error', error.message)
  }
})

额外注意事项:

  • 可以提前查询币安对应交易对的规则,确认价格、数量的精度要求,避免参数格式化错误
  • 如果出现时间相关的报错,可先调用币安服务器时间接口获取官方时间戳生成请求,避免本地时间偏差
  • 生成签名前可打印querystring的内容,和实际发送的请求体内容逐字符对比,确保完全一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 12:06:03