如何解决无法使用正文在 python 中发送经过身份验证的 OKEx API POST 请求 401 签名无效经过身份验证的 GET 请求有效
正如标题所示,我可以毫无问题地发送 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,endpoint,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,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。
提前致谢!
解决方法
您需要以 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。