【问题标题】:How can I solve msg: 'Signature for this request is not valid.' from binance API?我该如何解决 msg:“此请求的签名无效。”来自币安 API?
【发布时间】:2021-08-04 04:12:37
【问题描述】:

我正在尝试使用 Binance API 创建测试订单,但我收到此错误消息 msg:“此请求的签名无效。”我尝试使用密钥和 timestamp = Date.now() 获取签名,但没有成功。 我的代码是:

const credentials = require('./credentials');
const crypto = require('crypto');

function testNewOrder(symbol,side,type,timeInForce,quantity,price) {
    const url = credentials.urlTest;
    let timeStamp = Date.now();
    let signature = crypto.createHmac('sha256', credentials.secretKeyTest).update(`timestamp=${timeStamp}`).digest('hex')
    console.log(signature,timeStamp,timeStamp2);
    fetch(`${url}/api/v3/order/test?symbol=${symbol}&side=${side}&type=${type}&timeInForce=${timeInForce}&quantity=${quantity}&price=${price}&timestamp=${timeStamp}&signature=${signature}`, {
      method: 'POST',
      headers: {
        "Content-Type": "application/json",
        "X-MBX-APIKEY": credentials.apiKeyTest
      },

    }).then(response => {
      return response.json();
    }).then( data=> {
      console.log(data);
    }).catch(err => {
      console.log(err)
    })
}

testNewOrder('BTCUSDT','SELL','LIMIT','GTC','0.02','42000');

【问题讨论】:

  • 您是否有权访问说明如何创建此签名的文档?你见过github.com/binance-exchange/binance-signature-examples 准确地显示了你需要做什么,即签署整个“消息”而不仅仅是时间戳部分
  • 另外,在币安网站 binance-docs.github.io/apidocs/spot/en/… 上有很好的记录 - 不知道你为什么只签署时间戳
  • @Bravo 我阅读了整个文档,我相信我正在做 node.js 文件夹 link 中显示的确切内容。 credentials.secretKeyTest 有我的 apiSecret。
  • TL;DR - 做记录在案的 here 而不是一些一般解释这个概念的随机 github 页面
  • 这不是一个随机存储库!是官方的。确实是您在第一条评论中发给我的那个。

标签: javascript node.js binance


【解决方案1】:

我也遇到了同样的问题,希望对你有帮助

import { createHmac } from "crypto";

const SECRET = "";

export const addSignature = (params) => {
  // Object to query string
  const queryString = Object.keys(params)
    .map((key) => `${key}=${params[key]}`)
    .join("&");

  const signature = createHmac("sha256", SECRET)
    .update(queryString)
    .digest("hex");

  return {
    ...params,
    signature,
  };
};

addSignature({
  symbol: "BTC",
  // ...
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    相关资源
    最近更新 更多