Binance TIMESTAMP ERROR(此请求的时间戳在 recvWindow 之外。M 1)
Binance TIMESTAMP ERROR (Timestamp for this request is outside of the recvWindow. M 1)
我在执行代码时使用了 binance broker api。
我遇到了这个错误:
数据:{
代码:-1021,在此输入代码
消息:'Timestamp for this request is outside of the recvWindow.'
}
然后我使用了 binance server timestamp 并得到另一个错误:
数据:{代码:-1022,消息:'Signature for this request is not valid.'}
----- 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()
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 密钥 header,签名计算中的密钥。
要生成有效签名,所有查询参数都必须在用于生成签名的查询字符串中。
创建参数object并将其转换为queryString:
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')
我在执行代码时使用了 binance broker api。
我遇到了这个错误: 数据:{ 代码:-1021,在此输入代码 消息:'Timestamp for this request is outside of the recvWindow.' }
然后我使用了 binance server timestamp 并得到另一个错误: 数据:{代码:-1022,消息:'Signature for this request is not valid.'}
----- 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()
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 密钥 header,签名计算中的密钥。
要生成有效签名,所有查询参数都必须在用于生成签名的查询字符串中。
创建参数object并将其转换为queryString:
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')