本文介绍了Binance API Hmac签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

请帮助我不知道我的代码有什么问题.不需要签名的端点可以正常工作,所以我想我如何获得签名是一个问题.我收到此错误:

please help I don't know what is wrong with my code.Endpoints that doesn't need signature work fine, so I guess is a problem with how I am getting the signature. I am getting this error:

data: { code: -2014, msg: 'API-key format invalid.' } } }

API文档: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md

我的代码:

const axios = require('axios');
const crypto = require('crypto');
const qs = require('qs');

const binanceConfig = {
  API_KEY: 'XXXXXXX',
  API_SECRET: 'XXXXXX',
  HOST_URL: 'https://api.binance.com',
};

const buildSign = (data, config) => {
  return crypto.createHmac('sha256', config.API_SECRET).update(data).digest('hex');
};

const privateRequest = async (data, endPoint, type) => {
  const dataQueryString = qs.stringify(data);
  const signature = buildSign(dataQueryString, binanceConfig);
  const requestConfig = {
    method: type,
    url: binanceConfig.HOST_URL + endPoint + '?' + dataQueryString + '&signature=' + signature,
    headers: {
      'Authorization': `X-MBX-APIKEY: ${binanceConfig.API_KEY}`,
    },
  };

  try {
    console.log('URL: ', requestConfig.url);
    const response = await axios(requestConfig);
    console.log(response);
    return response;
  }
  catch (err) {
    console.log(err);
    return err;
  }
};

const data = {
  symbol: 'ARKBTC',
  recvWindow: 20000,
  timestamp: Date.now(),
};

privateRequest(data, '/api/v3/openOrders', 'GET');

推荐答案

尝试将headers对象设置为直接具有X-MBX-APIKEY键:

Try setting the headers object to have a key of X-MBX-APIKEY directly:

headers: {
  'X-MBX-APIKEY': binanceConfig.API_KEY,
},

这篇关于Binance API Hmac签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:00