使用 cURL 连接到 OKEX 的私有 API 需要进行身份验证。OKEX 的私有 API 通常需要以下几种身份验证信息:
以下是一个示例,展示如何使用 cURL 连接到 OKEX 的私有 API。假设你要获取账户余额信息。
首先,你需要生成一个签名。签名是基于请求的时间戳、请求方法、请求路径和请求体生成的 HMAC SHA256 哈希。
以下是一个 Python 示例,展示如何生成签名:
import hmac
import hashlib
import base64
import time
api_key = 'your_api_key'
secret_key = 'your_secret_key'
passphrase = 'your_passphrase'
timestamp = str(time.time())
method = 'GET'
request_path = '/api/v5/account/balance'
body = ''
message = timestamp + method + request_path + body
hmac_key = base64.b64decode(secret_key)
signature = hmac.new(hmac_key, message.encode('utf-8'), hashlib.sha256).digest()
signature = base64.b64encode(signature).decode()
print(f'Timestamp: {timestamp}')
print(f'Signature: {signature}')
使用生成的时间戳和签名,通过 cURL 发送请求:
timestamp=$(python3 -c "import time; print(time.time())")
signature=$(python3 -c "
import hmac
import hashlib
import base64
import time
api_key = 'your_api_key'
secret_key = 'your_secret_key'
passphrase = 'your_passphrase'
timestamp = '$timestamp'
method = 'GET'
request_path = '/api/v5/account/balance'
body = ''
message = timestamp + method + request_path + body
hmac_key = base64.b64decode(secret_key)
signature = hmac.new(hmac_key, message.encode('utf-8'), hashlib.sha256).digest()
signature = base64.b64encode(signature).decode()
print(signature)
")
curl -X GET "https://www.okex.com/api/v5/account/balance" \
-H "OK-ACCESS-KEY: your_api_key" \
-H "OK-ACCESS-SIGN: $signature" \
-H "OK-ACCESS-TIMESTAMP: $timestamp" \
-H "OK-ACCESS-PASSPHRASE: your_passphrase" \
-H "Content-Type: application/json"
time.time()
生成当前时间戳。/api/v5/account/balance
端点。请求头中包含 API Key、签名、时间戳和密码短语。领取专属 10元无门槛券
手把手带您无忧上云