时间戳过期错误问题
论坛会员您好,
我在与 OKEx API 交互时遇到 Python 代码问题。具体来说,我收到了 {'msg': 'Invalid OK-ACCESS-TIMESTAMP', 'code': '50112'}。这是相关代码片段的简化版本:
class OkexBot:
def init(self, APIKEY: str, APISECRET: str, PASS: str):
self.apikey = APIKEY
self.apisecret = APISECRET
self.password = PASS
self.baseURL = 'https://www.okex.com'
@staticmethod
def get_time():
time_cur = dt.datetime.now(dt.timezone.utc).isoformat("T", "milliseconds") + 'Z'
return time_cur
@staticmethod
def signature(timestamp, method, request_path, body, secret_key):
message = timestamp + method + request_path + body
mac = hmac.new(bytes(secret_key, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256')
output = mac.digest()
return base64.b64encode(output)
def get_header(self, request='GET', endpoint='', body=''):
cur_time = self.get_time()
header = dict()
header['CONTENT-TYPE'] = "application/json"
header['OK-ACCESS-KEY'] = APIKEY
header['OK-ACCESS-SIGN'] = self.signature(cur_time, request, endpoint, body, APISECRET)
header['OK-ACCESS-TIMESTAMP'] = str(cur_time)
header['OK-ACCESS-PASSPHRASE'] = PASS
return header
def withdrawal(self, account, amount):
endpoint = '/api/v5/asset/withdrawal'
url = self.baseURL + endpoint
request = 'POST'
body = {
"amt": str(amount),
"fee": "0.0096",
"dest": "4",
"ccy": "SOL",
"chain": "SOL-Solana",
"toAddr": account
}
body = json.dumps(body)
header = self.get_header(request, endpoint, str(body))
response = requests.post(url, headers=header, data=body)
return response
我改变了获取当前时间的方式
我找到了这个解决方案
// 获取当前UTC时间戳(毫秒精度)的方法 公共静态字符串 getCurrentUTCTimestamp() { 返回 java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") .withZone(java.time.ZoneOffset.UTC) .format(java.time.Instant.now().truncatedTo(java.time.temporal.ChronoUnit.MILLIS));