1.首先,你需要注册Binance账户并创建API密钥。
2.打开API文档(https://binance-docs.github.io/apidocs/spot/en/#authentication),阅读“SIGNED Endpoint Examples for POST /api/v3/order”一章并了解如何使用API密钥进行签名。
3.借助于代码库或自己编写代码,实现登录功能。样例代码如下:
import requests
import time
import hashlib
import hmac
#API密钥和秘钥
api_key = 'Replace with your API key'
api_secret = 'Replace with your API secret'
#时间戳
timestamp = int(time.time() * 1000)
#签名
sign = hmac.new(api_secret.encode('utf-8'), f'timestamp={timestamp}'.encode('utf-8'), hashlib.sha256).hexdigest()
#请求头
headers = {
'Content-Type': 'application/json',
'X-MBX-APIKEY': api_key,
}
#请求体
payload = {
'timestamp': timestamp,
'signature': sign,
}
#发送请求
response = requests.post('https://api.binance.com/api/v3/userDataStream', headers=headers, json=payload)
#返回结果
print(response.json())
4.运行代码,输出结果中包含userListenKey,即为登录成功,可以使用Binance的API功能。