该错误代码表示API密钥无效。解决方法是检查API密钥是否正确,并确保在发出请求时将其包含在请求头中。以下是Python示例代码,展示了如何使用有效的API密钥调用Bybit API:
import requests
import hashlib
import time
api_key = 'your_api_key'
secret_key = b'your_secret_key'
# 获取服务器时间戳
response = requests.get('https://api-testnet.bybit.com/v2/public/time')
timestamp = response.json()['time_now']
# 创建签名
sign_string = f"GET/realtime/orderBookL2_25?api_key={api_key}×tamp={timestamp}"
signature = hashlib.sha256(secret_key + sign_string.encode()).hexdigest()
# 发送请求
headers = {
'api_key': api_key,
'timestamp': timestamp,
'sign': signature,
}
response = requests.get('https://api-testnet.bybit.com/realtime/orderBookL2_25', headers=headers)
请注意,此示例代码使用测试网址,您需要将其替换为Bybit主网址:https://api.bybit.com/v2/public/time 或 https://api.bybit.com/realtime/orderBookL2_25。