okxapi / python-okx

398 stars 154 forks source link

Incorrect Timestamp Format in 'OK-ACCESS-TIMESTAMP' Header When use_server_time=True is Used #55

Closed grzhan closed 5 months ago

grzhan commented 6 months ago

Description:

In the python-odx library, when creating an API instance (e.g., AccountAPI) with the use_server_time=True parameter, the OkxClient._get_timestamp method retrieves the server time as a Unix millisecond timestamp (e.g., "1607417337715"). However, the OKX API expects the 'OK-ACCESS-TIMESTAMP' request header to be in ISO 8601 format (e.g., "2020-12-08T09:08:57.715Z"). Due to this mismatch in the timestamp format, requests to the OKX API fail with error code 50102, indicating an expired timestamp.

Steps to Reproduce:

import okx.Account as Account

API_KEY = 'xxxxx'
SECRET_KEY = 'xxxxxx'
PASSPHRASE = 'xxxxxx'

flag = '0' 
accountAPI = Account.AccountAPI(API_KEY, SECRET_KEY, PASSPHRASE, flag=flag, use_server_time=True)

result = accountAPI.get_account_balance()
print(result)
  1. Initialize an AccountAPI instance with use_server_time=True.
  2. Perform any API request that requires the 'OK-ACCESS-TIMESTAMP' header.
  3. Observe that the OKX API returns an error code 50102, indicating that the request timestamp is expired.

Expected Behavior:

The OkxClient._get_timestamp method should correctly format the server time as an ISO 8601 timestamp to match the OKX API's expectations, ensuring that API requests with use_server_time=True succeed without timestamp-related errors.

Actual Behavior:

API requests fail with error code 50102 due to a mismatch in the expected timestamp format in the 'OK-ACCESS-TIMESTAMP' request header.

Possible Solution:

Adjust the OkxClient._get_timestamp method to convert the Unix millisecond timestamp to an ISO 8601 formatted string before including it in the 'OK-ACCESS-TIMESTAMP' header. This conversion should account for the necessary precision and timezone (UTC) to ensure compatibility with OKX API requirements.

Environment:

Additional Context:

This issue prevents all python-odx users from successfully making API requests with use_server_time=True, affecting operations that rely on accurate server time synchronization.

mzaeemnasir commented 6 months ago

Please commit this, I got the same issue.

standyyyy commented 6 months ago

me too , please commit it