unix-timestamp - Binance TIMESTAMP ERROR(此请求的时间戳在recvWindow之外。M 1)
问题描述
在执行我的代码时,我正在使用 binance broker api。
我收到此错误: 数据:{ 代码:-1021,在此处输入代码 msg:“此请求的时间戳在 recvWindow 之外。” }
然后我使用了币安服务器时间戳并得到另一个错误: 数据:{代码:-1022,味精:'此请求的签名无效。' }
----- 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 密钥,签名计算中的密钥。
要生成有效签名,所有查询参数都必须在用于生成签名的 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')
推荐阅读
- json - 尝试打开 json 文件时出错 [json.decoder.JSONDecodeError:期望属性名称用双引号括起来]
- android - 为 android 设置 react native crashlytics 后无法发送崩溃报告
- javascript - 有没有办法在反应中将 setState 导出到无状态组件之外?
- sql-server - SQL Server 数据库中的加密列
- javascript - React - 在兄弟组件之间共享状态和方法
- authorization - Blazor webassembly,检查策略是否存在
- ruby-on-rails - 我怎样才能模拟一个只返回一个对象一次的方法?(我正在测试分页光标)
- google-apps-script - 使用谷歌脚本将更新的文件夹复制到另一张表,然后删除原始文件夹并将新文件夹重命名为旧名称
- java - 如何从 Docker 连接到本地 Mysql
- kubernetes-helm - 如何在本地从 AWS ecr 提取 Helm 图表