首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OKEx APi - POST方法“无效符号”错误的问题

OKEx APi - POST方法“无效符号”错误的问题
EN

Stack Overflow用户
提问于 2022-09-05 20:57:35
回答 1查看 52关注 0票数 0

我无法向OKEx API发送邮件请求。但是,使用GET方法,一切都好--我可以检查我的帐户余额。一旦我发送邮件请求,我就收到了错误{'msg': 'Invalid Sign', 'code': '50113'}

代码语言:javascript
运行
复制
class OkexBot:
    def __init__(self, APIKEY: str, APISECRET: str, PASS: str):
        self.apikey = APIKEY
        self.apisecret = APISECRET
        self.password = PASS
        self.baseURL = 'https://okex.com'

    @staticmethod
    def get_time():
        return dt.datetime.utcnow().isoformat()[:-3] + 'Z'

    @staticmethod
    def signature(timestamp, method, request_path, body, secret_key):
        if str(body) == '{}' or str(body) == 'None':
            body = ''
        else:
            body = json.dumps(body)
        message = str(timestamp) + str.upper(method) + request_path + str(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: dict = dict()):
        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 place_market_order(self, pair, side, amount, tdMode='cash'):
        endpoint = '/api/v5/trade/order'
        url = self.baseURL + endpoint
        request = 'POST'
        body = {
            "instId": pair,
            "tdMode": tdMode,
            "side": side,
            "ordType": "market",
            "sz": str(amount)
        }

        body = json.dumps(body)
        header = self.get_header(endpoint=endpoint, request=request, body=body)
        response = requests.post(url=url, headers=header, data=body)
        return response

我研究了这个话题

但什么都帮不上忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-08 22:04:34

这段代码是vaid。

代码语言:javascript
运行
复制
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():
        return dt.datetime.utcnow().isoformat()[:-3] + 'Z'

    @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'] = cur_time
        header['OK-ACCESS-PASSPHRASE'] = PASS
        return header

    def place_market_order(self, pair, side, amount, tdMode='cash'):
        endpoint = '/api/v5/trade/order'
        url = self.baseURL + '/api/v5/trade/order'
        request = 'POST'
        body = {
            "instId": pair,
            "tdMode": tdMode,
            "side": side,
            "ordType": "market",
            "sz": str(Decimal(str(amount)))
        }
        body = json.dumps(body)
        header = self.get_header(request, endpoint, str(body))
        print(header)
        response = requests.post(url, headers=header, data=body)
        return response
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73614607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档