错误:此请求的时间戳不在revcWindow之外

时间:2019-01-26 21:57:10

标签: python rest binance

我正在尝试向需要api键和签名的币安服务器发送请求,但控制台表示时间戳不在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之外。'}

1 个答案:

答案 0 :(得分:0)

与Python同步需要与您做更多的事情。 那些人在这里:有一些解决方法enter link description here 到目前为止,这似乎是防弹的

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)

编辑: 我在这里找到了一个更简单的解决方案,而另一个似乎并不那么防弹:enter link description here

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)