首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用cURL连接到OKEX私有api?

使用 cURL 连接到 OKEX 的私有 API 需要进行身份验证。OKEX 的私有 API 通常需要以下几种身份验证信息:

  1. API Key:你的 API 密钥。
  2. Secret Key:你的 API 密钥的秘密。
  3. Passphrase:你在创建 API 密钥时设置的密码短语。
  4. 签名:根据请求的内容和时间戳生成的 HMAC SHA256 签名。

以下是一个示例,展示如何使用 cURL 连接到 OKEX 的私有 API。假设你要获取账户余额信息。

步骤 1:生成签名

首先,你需要生成一个签名。签名是基于请求的时间戳、请求方法、请求路径和请求体生成的 HMAC SHA256 哈希。

以下是一个 Python 示例,展示如何生成签名:

代码语言:javascript
复制
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}')

步骤 2:使用 cURL 发送请求

使用生成的时间戳和签名,通过 cURL 发送请求:

代码语言:javascript
复制
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"

解释代码

  1. 生成时间戳:使用 time.time() 生成当前时间戳。
  2. 生成签名:使用 Python 生成签名。签名是基于时间戳、请求方法、请求路径和请求体生成的 HMAC SHA256 哈希。
  3. 发送请求:使用 cURL 发送 GET 请求到 OKEX 的 /api/v5/account/balance 端点。请求头中包含 API Key、签名、时间戳和密码短语。

注意事项

  1. API Key 和 Secret Key:确保你的 API Key 和 Secret Key 保密,不要泄露给他人。
  2. 时间同步:确保你的服务器时间与 OKEX 服务器时间同步,否则可能会导致请求被拒绝。
  3. 请求方法和路径:根据你要访问的 API 端点,修改请求方法和路径。
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券