【发布时间】:2021-05-10 15:45:56
【问题描述】:
我正在尝试使用 python 请求在 Binance 测试网上下订单。它总是抛出{"code":-2014,"msg":"API-key format invalid."}。但是相同的 url 在与 curl 一起使用时会成功返回数据。有人可以指出我的 python 请求代码有什么问题吗?
API_KEY= <api_key>
SECRET= <password>
import time
import requests
import hashlib
import hmac
def hashing(query_string):
return hmac.new(SECRET.encode('utf-8'),
query_string.encode('utf-8'),
hashlib.sha256).hexdigest()
base_url = "https://testnet.binance.vision"
api_path = "/api/v3/order"
timestamp = int(time.time()*1000)
msg = "symbol=BNBUSDT&side=BUY&type=LIMIT&quantity=1&timeInForce=GTC&price=200×tamp={}".format(timestamp)
msg_hash = hashing(msg)
url = f"{base_url}{api_path}?{msg}&signature={msg_hash}"
print(url)
headers = {'Content-Type': 'application/json;charset=utf-8',
'X-MBX-APIKEY': API_KEY}
print("\nusing python-requests")
with requests.Session() as session:
session.headers.update(headers)
resp = session.put(url)
print(resp)
print("\nusing curl")
!curl -H "X-MBX-APIKEY: {API_KEY}" -X POST "{url}"
colab 链接:https://colab.research.google.com/drive/1qPuDK_mGBchpoD4cVbq6oKvt_WY4nX71?usp=sharing
【问题讨论】:
标签: python-3.x curl python-requests binance