【问题标题】:Unable to send authenticated OKEx API POST requests with body (in python). 401 Signature invalid. Authenticated GET requests work无法使用正文(在 python 中)发送经过身份验证的 OKEx API POST 请求。 401 签名无效。经过身份验证的 GET 请求有效
【发布时间】:2021-08-30 20:56:57
【问题描述】:

正如标题所示,我可以毫无问题地为 OKEx API 发送经过身份验证的 GET 请求。但是,一旦我通过在签名以及请求正文中添加额外参数来发出 POST 请求,我就会得到 401 响应 {'msg': 'Invalid Sign', 'code': '50113'}.


def signature(timestamp, method, request_path, body,secret_key):
    if str(body) == '{}' or str(body) == 'None':
        message = str(timestamp) + str.upper(method) + request_path + ''
    else:

        message = str(timestamp) + str.upper(method) + request_path +body
    
    mac = hmac.new(bytes(secret_key, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256')
    d = mac.digest()
    return base64.b64encode(d)


def get_header(endpoint,request,body={}):

    header = dict()
    header['CONTENT-TYPE'] = 'application/json'
    header['OK-ACCESS-KEY'] = okex_key
    current_time=get_time()
    header['OK-ACCESS-SIGN'] = signature(current_time, request, endpoint , body, okex_secret)
    header['OK-ACCESS-TIMESTAMP'] = str(current_time)
    header['OK-ACCESS-PASSPHRASE'] = okex_pass
    return header


def place_market_order(url,pair,side,amount,tdMode='cash'):

    endpoint='/api/v5/trade/order' 
    request='POST'

    body={
            "instId":pair,
            "tdMode":tdMode, #cash, cross, isolated
            "side":side,
            "ordType":"market",
            "sz":str(Decimal(str(amount)))
        }

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

    return response

#
url = 'http://www.okex.com'

place_market_order(url,pair="BTC-USDT",side="buy",amount=0.001)

签名逻辑应该没问题,因为经过身份验证的 GET 请求有效。

但是我处理身体的方式有什么问题?我使用的是 OKEx API v5。

提前致谢!

【问题讨论】:

    标签: python api post


    【解决方案1】:

    您需要以 JSON 格式发送 POST 请求。

    好例子:

    {"instId":"BTC-USDT","tdMode":"cash","side":"buy","ordType":"limit","sz":"0.00001","px": “30000”}

    不好的例子:

    instId=BTC-USDT&tdMode=cash&side=buy&ordType=limit&sz=0.00001&px=30000

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2011-11-01
      • 2016-05-01
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 2020-10-31
      相关资源
      最近更新 更多