【发布时间】: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。
提前致谢!
【问题讨论】: