最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Binance API Hmac Signature - Stack Overflow

programmeradmin6浏览0评论

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 Doc: .md

SIGNED endpoints require an additional parameter, signature, to be sent in the query string or request body. Endpoints use HMAC SHA256 signatures. The HMAC SHA256 signature is a keyed HMAC SHA256 operation. Use your secretKey as the key and totalParams as the value for the HMAC operation. The signature is not case sensitive. totalParams is defined as the query string concatenated with the request body.

My code:

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

const binanceConfig = {
  API_KEY: 'XXXXXXX',
  API_SECRET: 'XXXXXX',
  HOST_URL: '',
};

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');

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 Doc: https://github./binance-exchange/binance-official-api-docs/blob/master/rest-api.md

SIGNED endpoints require an additional parameter, signature, to be sent in the query string or request body. Endpoints use HMAC SHA256 signatures. The HMAC SHA256 signature is a keyed HMAC SHA256 operation. Use your secretKey as the key and totalParams as the value for the HMAC operation. The signature is not case sensitive. totalParams is defined as the query string concatenated with the request body.

My code:

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.',
};

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');
Share Improve this question edited May 12, 2018 at 8:51 David Rivera asked May 12, 2018 at 8:46 David RiveraDavid Rivera 3691 gold badge5 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

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

headers: {
  'X-MBX-APIKEY': binanceConfig.API_KEY,
},
发布评论

评论列表(0)

  1. 暂无评论