pythontimestamp

'Timestamp request expired', 'code': '50102'


I'm trying to retrieve user's data from okcoin.com, the code in Python:

        base_url = 'https://www.okcoin.com'
        endpoint = '/api/v5/account/balance'
        params = {'ccy': 'BTC,STX'}
        timestamp = datetime.now().isoformat(timespec='milliseconds') + 'Z'
        sign = timestamp + 'GET' + endpoint
        secret_key = base64.b64decode(okcoin_api_secret)
        signature = hmac.new(secret_key, sign.encode(), hashlib.sha256).hexdigest()
    
        print(f'OKcoin timestamp: {timestamp}')
        headers = {
            'OK-ACCESS-KEY': okcoin_api_key,
            'OK-ACCESS-SIGN': signature,
            'OK-ACCESS-TIMESTAMP': timestamp,
            'OK-ACCESS-PASSPHRASE': okcoin_api_passphrase,
            'Content-Type': 'application/json'

        response = requests.get(base_url + endpoint, params=params, headers=headers)
        data = response.json()
        return data

All API call requirements, described here https://www.okcoin.com/docs-v5/en/#rest-api-authentication-generating-an-apikey, checked many times but it returns:

{'msg': 'Timestamp request expired', 'code': '50102'}

Tried to get the timestamp one hour back, no effect. Asked the okcoin support, but they don't know. Tried a few different TZ, the same result. I'd appreciate any clue. The interesting detail is that a similar pattern works for both Kucoin and Binance.


Solution

  • The problem is that you are lying to them, by grabbing the local time and arbitrarily tacking on a 'Z' to claim it is UTC. Unless you live in a GMT+0 timezone, that's just wrong.

    If you check the documentation, you'll see that there is a utcnow method that is designed for exactly this situation:

            timestamp = datetime.utcnow().isoformat(timespec='milliseconds') + 'Z'