50006无效的Content_Type,请使用"application/json"格式
GET请求正常,POST请求格式签名正确,但请求报错
import requests,base64,hmac,datetime
pass_phrase = '***' # 申请
api_key = "***" # 申请
secret_key = "***" # 申请
def GetTimeUTC():
rest = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
suffix = 'Z'
rest = rest + suffix
return rest
def signature(timestamp, method, request_path, body, secret_key):
#签名
body = {
"instId":"BTC-USDT-SWAP",
"tdMode":"cross",
"side":"buy",
"posSide":"long",
"ordType":"market",
"sz":"1"
}
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')
d = mac.digest()
return base64.b64encode(d)
def get_header(api_key, current_time, pass_phrase, secret_key):
#拼接请求头
return {
'OK-ACCESS-KEY': api_key,
'OK-ACCESS-SIGN': signature(current_time, 'POST', '/api/v5/trade/order', 'None', secret_key),
'OK-ACCESS-TIMESTAMP': current_time,
'OK-ACCESS-PASSPHRASE': pass_phrase,
}
timestamp = GetTimeUTC()
url = 'https://www.okex.com/api/v5/trade/order'
headers=get_header(api_key, timestamp, pass_phrase, secret_key)
response = requests.post(url=url, headers=headers).json()