Pieter H.
Pieter H.

Reputation: 11

Error: Timestamp for this request is outside of revcWindow

I'm trying to send a request to the binance servers which require an api-key and a signature but the console is saying that the timestamp is outside of the revcWindow

I've looked up the problem and found that I need sync my computer's time to the Binance's. I'm not quite sure how to do that though (pretty newbie in 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': 'Timestamp for this request is outside of the recvWindow.'}

Upvotes: 1

Views: 4952

Answers (1)

Chuox
Chuox

Reputation: 793

The syncing has to do more with you SO that with Python. Those guys here: have some solutions enter link description here This one seems to be bulletproof (so far)

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)

Edit: I found an easier solution here, and the other one does not seems to be so bulletproof: 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)

Upvotes: 2

Related Questions