首页 > 解决方案 > 错误:此请求的时间戳在 revcWindow 之外

问题描述

我正在尝试向需要 api-key 和签名的 binance 服务器发送请求,但控制台说时间戳在revcWindow 之外

我查找了问题,发现我需要将计算机的时间同步到 Binance 的时间。我不太确定如何做到这一点(Python中的漂亮新手)

def test(self):
    self.url += self.url_list['test']

    params = {'symbol': 'BTCETH', "timestamp": 0, "side": "BUY", "type": "LIMIT", "quantity": 0.0005, "recvWindow": 500 }

    data = parammanger.encode_params(params)

    data += "&signature=" + self.hash(data)

    headers = {'X-MBX-APIKEY': self.a_key}

    print(data)

    r_body = {'signature': self.hash(data)}

    r = requests.post(self.url, data, headers=headers)

    print(r.request.headers)

    print(r.json())

def hash(self, data):
    return hashmanager.create_hash(self.s_key.encode("utf-8"), data.encode("utf-8"))

{'code': -1021, 'msg': '此请求的时间戳在 recvWindow 之外。'}

标签: pythonrestbinance

解决方案


与 Python 相比,同步必须与您做更多的事情。这里的那些人:有一些解决方案在此处输入链接描述 这个似乎是防弹的(到目前为止)

import time
from binance.client import Client

class Binance:
    def __init__(self, public_key = '', secret_key = '', sync = False):
        self.time_offset = 0
        self.b = Client(public_key, secret_key)

        if sync:
            self.time_offset = self._get_time_offset()

    def _get_time_offset(self):
        res = self.b.get_server_time()
        return res['serverTime'] - int(time.time() * 1000)

    def synced(self, fn_name, **args):
        args['timestamp'] = int(time.time() - self.time_offset)
        return getattr(self.b, fn_name)(**args)
#I then instantiate it and call my private account functions with synced

binance = Binance(public_key = 'my_pub_key', secret_key = 'my_secret_key', sync=True)
binance.synced('order_market_buy',  symbol='BNBBTC', quantity=10)

编辑:我在这里找到了一个更简单的解决方案,而另一个似乎不是那么防弹:在此处输入链接描述

from binance.client import Client
client = Client('api_key', 'api_secret')
import time
import win32api
gt = client.get_server_time()
tt=time.gmtime(int((gt["serverTime"])/1000))
win32api.SetSystemTime(tt[0],tt[1],0,tt[2],tt[3],tt[4],tt[5],0)

推荐阅读