You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OKEx API调用报错Invalid OK-ACCESS-TIMESTAMP(错误码50112)求助

解决OKEx API 50112 Invalid OK-ACCESS-TIMESTAMP错误

错误原因分析

错误码50112表示请求头中的OK-ACCESS-TIMESTAMP不符合要求,常见触发场景:

  • 本地时间与OKEx服务器时间偏差超过30秒
  • 签名生成时使用的时间与请求头中的时间不一致
  • 时间格式未严格遵循ISO 8601 UTC毫秒标准
  • 代码变量引用错误导致签名/头信息生成异常

代码修复点

1. 修正类初始化方法

类的初始化方法必须用双下划线包裹的__init__,否则实例变量无法正确绑定:

def __init__(self, APIKEY: str, APISECRET: str, PASS: str):
    self.apikey = APIKEY
    self.apisecret = APISECRET
    self.password = PASS
    self.baseURL = 'https://www.okex.com'

2. 修复变量引用错误

get_header方法错误使用全局变量,需改为引用实例自身的属性:

header['OK-ACCESS-KEY'] = self.apikey
header['OK-ACCESS-SIGN'] = self.signature(cur_time, request, endpoint, body, self.apisecret)
header['OK-ACCESS-PASSPHRASE'] = self.password

3. 修正签名返回格式

signature方法返回的是bytes类型,需转成字符串才能放入请求头:

return base64.b64encode(output).decode('utf-8')

4. 优化时间获取(避免本地时间偏差)

优先使用OKEx服务器的标准时间,替代可能有偏差的本地时间:

@staticmethod
def get_time():
    # 优先请求OKEx服务器时间
    try:
        resp = requests.get('https://www.okex.com/api/v5/public/time')
        resp.raise_for_status()
        return resp.json()['data'][0]['iso']
    except Exception:
        # 本地时间降级方案
        time_cur = dt.datetime.now(dt.timezone.utc).isoformat("T", "milliseconds") + 'Z'
        return time_cur

5. 避免重复序列化请求体

withdrawal方法中json.dumps(body)已生成字符串,无需再用str(body)传递:

header = self.get_header(request_method, endpoint, body_str)

修复后的完整代码

import datetime as dt
import hmac
import base64
import json
import requests

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():
        try:
            resp = requests.get('https://www.okex.com/api/v5/public/time')
            resp.raise_for_status()
            return resp.json()['data'][0]['iso']
        except Exception:
            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).decode('utf-8')

    def get_header(self, request='GET', endpoint='', body=''):
        cur_time = self.get_time()
        header = dict()
        header['CONTENT-TYPE'] = "application/json"
        header['OK-ACCESS-KEY'] = self.apikey
        header['OK-ACCESS-SIGN'] = self.signature(cur_time, request, endpoint, body, self.apisecret)
        header['OK-ACCESS-TIMESTAMP'] = cur_time
        header['OK-ACCESS-PASSPHRASE'] = self.password
        return header

    def withdrawal(self, account, amount):
        endpoint = '/api/v5/asset/withdrawal'
        url = self.baseURL + endpoint
        request_method = 'POST'
        body = {
            "amt": str(amount),
            "fee": "0.0096",
            "dest": "4",
            "ccy": "SOL",
            "chain": "SOL-Solana",
            "toAddr": account
        }
        body_str = json.dumps(body)
        header = self.get_header(request_method, endpoint, body_str)
        response = requests.post(url, headers=header, data=body_str)
        return response

额外注意事项

  • 确保本地服务器开启NTP时间同步,避免时间偏差
  • 检查API密钥、密码是否正确,且账号拥有提现权限
  • 调试时可打印请求头时间与签名使用的时间,确认两者完全一致

内容的提问来源于stack exchange,提问作者Костя Приходько

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 19:47:48