币安时间戳错误(此请求的时间戳超出了recvWindow。M 1)

问题描述 投票:0回答:3

在执行我的代码时,我正在使用 Binance Broker api。

我收到此错误: 数据:{ 代码:-1021,在此处输入代码 msg: '此请求的时间戳位于 recvWindow 之外。' }

然后我使用了 binance 服务器时间戳并收到另一个错误: data: { code: -1022, msg: '此请求的签名无效。' }

----- My sample code -------

'use strict'

//Modules
const crypto = require('crypto');
const axios = require('axios');
const moment = require('moment');
const keys = require('./config/keys');


//Keys
const apiKey = keys.keys.apiKey;

async function api_call(config){
  var response  = {}
  try {
    response = await axios(config);
    response =  {
      error: false,
      data : response.data
    }
  } catch (error) {
    response =  { 
      error: true,
      data : error
    }
  }
  return response;
}


async function get_binance_server_time(recvWindow=50000){
  var timestamp = moment().unix();
  var serverTime = 0;

  var config = {
    method: 'get',
    url: `https://api.binance.com/api/v3/time`,
    headers: { 
      'Content-Type': 'application/json', 
      'X-MBX-APIKEY': apiKey 
    }
  };  
  var response = await api_call(config);

  if(response.error){
    response.data = {
      serverTime : serverTime
    }
  }
  serverTime = response.data.serverTime

  console.log(timestamp);
  console.log(response.data.serverTime);

  if (timestamp < (serverTime + 1000) && (serverTime - timestamp) <= recvWindow) {
    console.log("process");
  } else {
    console.log("Rejected");
  }
  return serverTime;
}


async function get_broker_account_information( _signature = null){

  // Make the api call here
  var serverTime  = await get_binance_server_time();
  var _timestamp = serverTime;

  const query_string = `timestamp=${_timestamp}`;
  const recvWindow = 50000
  const signature = crypto.createHmac('sha256', apiKey).update(query_string).digest('hex')

  var config = {
    method: 'get',
    url: `https://api.binance.com/sapi/v1/broker/info?timestamp=${_timestamp}&signature=${signature}&recvWindow=${recvWindow}`,
    headers: { 
      'Content-Type': 'application/json', 
      'X-MBX-APIKEY': apiKey 
    }
  };  
  
  var response = await api_call(config)
  console.log(response.data);
  return response;
}




// create_sub_account()
get_broker_account_information()
// get_binance_server_time()

unix-timestamp binance
3个回答
5
投票

Binance 需要以毫秒为单位的 Unix 时间戳。 所以你应该改变

var timestamp = moment().unix();

var timestamp = moment().unix()*1000;

一些时间同步问题可以通过使用大约 15 秒的偏移来解决:

const offset = 15000;
var timestamp = moment().unix()*1000 - offset;  // works flawlessly in my case

也尝试使用

const recvWindow = 60000  // maximum allowed

重要: 您的签名必须从密钥生成,而不是 api 密钥。您需要两者,标头中的 api 密钥,签名计算中的密钥。

要生成有效的签名,所有查询参数都必须位于用于生成签名的查询字符串中。

创建一个参数对象并将其转换为查询字符串:

var params = {
    'timestamp': _timeStamp,
    'recvWindow: 60000,
    // other query parameters
};

var queryString = Object.keys(params).map(key => key + '=' + params[key]).join('&');

然后生成签名:

const signature = crypto.createHmac('sha256', apiSecret).update(queryString).digest('hex')

1
投票

只需将系统时间与网络同步,对我来说就可以了。


0
投票

当我遇到此错误时,网络连接出现问题。请求没有及时到达币安。

© www.soinside.com 2019 - 2024. All rights reserved.