# 签名

ACCESS-SIGN的请求头是对 **timestamp + method.toUpperCase() + requestPath + "?" + queryString + body** 字符串(+表示字符串连接)使用 **HMAC SHA256** 方法加密，通过**BASE64** 编码输出而得到的。

<strong>时间戳</strong>

请求签名中的 `ACCESS-TIMESTAMP` 单位是毫秒。请求时间戳必须在 API 服务时间的 30 秒内，否则请求将被视为过期并被拒绝。如果本地服务器时间和 API 服务器时间之间存在较大偏差，建议通过查询 API 服务器时间来更新 HTTP Header。

<strong>请求格式</strong>

当前支持以下请求方法：

- GET：参数通过 queryString 在路径中传输到服务器。
- POST：参数按照 JSON 格式通过 body 传输到服务器。
- DELETE：参数按照接口说明通过 queryString 或 JSON body 传输到服务器。

参与签名时，请按照实际请求内容拼接 `requestPath`、`queryString` 和 `body`。

<strong>签名各字段说明</strong>

- timestamp：与 ACCESS-TIMESTAMP 请求头相同。
- method：请求方法（GET/POST/DELETE），字母全部大写。
- requestPath：请求接口路径。
- queryString：请求URL中(?后的请求参数)的查询字符串。
- body：请求主体对应的字符串，如果请求没有主体则 body 可省略。

<strong>queryString为空时，签名格式</strong>

- timestamp + method.toUpperCase() + requestPath + body

<strong>queryString不为空时，签名格式</strong>

- timestamp + method.toUpperCase() + requestPath + "?" + queryString + body



<strong>举例说明</strong>

获取深度信息，以 btcusdt 为例:

- Timestamp = 1591089508404
- Method = "GET"
- requestPath = "/api/v3/market/depth"
- queryString= "symbol=BTCUSDT&limit=20"

<strong>生成待签名的字符串:</strong>

- '1591089508404GET/api/v3/market/depth?symbol=BTCUSDT&limit=20'



下单，以 btcusdt 为例:

- Timestamp = 1561022985382

- Method = "POST"

- requestPath = "/api/v3/order"

- body =

  ```json
  {"symbol":"BTCUSDT","side":"BUY","type":"LIMIT","timeInForce":"GTC","quantity":"1","price":"68900","newClientOrderId":"my-order-001"}
  ```

<strong>生成待签名的字符串:</strong>

- ```
  '1561022985382POST/api/v3/order{"symbol":"BTCUSDT","side":"BUY","type":"LIMIT","timeInForce":"GTC","quantity":"1","price":"68900","newClientOrderId":"my-order-001"}'
  ```



<strong>生成最终签名的步骤</strong>

1. 将待签名字符串使用私钥secretkey进行hmac sha256加密
   - Signature = hmac_sha256(secretkey, Message)
2. 对于Signature进行base64编码
   - Signature = base64.encode(Signature)
