Generate signature
生成签名
本页介绍的内容,我们的 SDK 已经完整实现了,如果是SDK用户,可以直接忽略本页内容。
此部分仅为给非 SDK 用户提供 参考。
This section is for reference only for non-SDK users.
设置请求头
#python3示例
import json,time
import requests
# 请替换为你的真实 API 认证信息
APP_KEY = '${app_key}'
ACCESS_TOKEN = '${access_token}'
secret='${secret}'
# 请求方法
method = "POST"
# 请求路径
host="https://openapi.longbridge.com"
uri = "/v1/whaleapi/account/get"
# get接口请求params 如 params = "member_id=1&account_channel=2"
params = ""
#设置请求头
headers = {}
headers['X-Api-Key'] = APP_KEY
headers['Authorization'] = ACCESS_TOKEN
headers['X-Timestamp'] = str(time.time()) # Unix Timestamp, eg: 1539095200.123
headers['Content-Type'] = 'application/json; charset=utf-8'
# post接口请求body 如json.dumps({ "account_no": 'L6VQEU00121996' })
body = json.dumps({ "account_no": 'L6VQEU00121996' })
# 签名并设置
headers['X-Api-Signature'] = sign(method, uri, headers, params, body, secret)
resp = requests.post(url=host+uri, headers=headers, data=body)
print(resp.json())使用签名函数对请求签名
The signature function is as follows:
# python3 签名函数
def sign(method, uri, headers, params, body, secret):
ts = headers["X-Timestamp"]
access_token = headers["Authorization"]
app_key = headers["X-Api-Key"]
mtd = method.upper()
canonical_request = mtd + "|" + uri + "|" + params + "|authorization:" + access_token + "\nx-api-key:" + app_key + "\nx-timestamp:" + ts + "\n|authorization;x-api-key;x-timestamp|"
if body != "":
payload_hash = hashlib.sha1(body.encode("utf-8")).hexdigest()
canonical_request = canonical_request + payload_hash
sign_str = "HMAC-SHA256|" + hashlib.sha1(canonical_request.encode("utf-8")).hexdigest()
signature = hmac.new(secret.encode('utf-8'), sign_str.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
return "HMAC-SHA256 SignedHeaders=authorization;x-api-key;x-timestamp, Signature=" + signatureModified at 2026-03-31 10:56:43