【发布时间】:2022-08-16 03:08:57
【问题描述】:
我正在开发一个使用 Nicehash API 打开/关闭我的采矿设备的 python 程序。我做了我需要做的一切,但我坚持要在查询字符串中输入什么。
在 post 请求的标头中必须是 X-auth,它是由 API 密钥和 HMAC 签名构建的。在NiceHash website 上,它说必须像这样构建 HMAC 签名:
Input structure is the following:
-API Key
-X-Time request header value
-X-Nonce request header value
-Empty field
-X-Organization-Id request header value
-Empty field
-Request method (example: GET, POST, DELETE, PUT)
-Request path (example: /main/api/v2/hashpower/orderBook)
-Request query string (example: algorithm=X16R&page=0&size=100, The query string should be the same as
passed to the server - without the leading question mark)
Input is a byte array composed of ordered fields using zero byte (0x00) as a separator. There is no
separator before the first field or after the last field. Some fields are always empty in which case the
separators immediately follow one another. For request body you should use the raw bytes as they are sent
to the server. For JSON messages the character encoding should always be UTF-8.
所以输入应该看起来像这样(已经在 UTF-8 中散列)然后再次被散列(从第一个到最后一个顺序:
API KEY,TIME,NONCE,ORGANISATION ID,REQUEST METHOD,REQUEST PATH,REQUEST QUERY String) :
4ebd366d-76f4-4400-a3b6-e51515d054d6 ⊠ 1543597115712 ⊠ 9675d0f8-1325-484b-9594-c9d6d3268890 ⊠ ⊠ da41b3bc-3d0b-4226-b7ea-aee73f94a518 ⊠ ⊠ GET ⊠ /main/api/v2/hashpower/orderBook ⊠ algorithm=X16R&page=0&size=100
(this sign: ⊠ is zero byte: 0x00)
我的代码:
import hashlib
import uuid
import requests
import json
import hmac
url = \"https://api2.nicehash.com/main/api/v2/mining/rigs/status2\"
path = \"/main/api/v2/mining/rigs/status2\"
ms = str(json.loads(requests.get(\'https://api2.nicehash.com/api/v2/time\').text)[\'serverTime\'])
req_id = str(uuid.uuid4())
nonce = str(uuid.uuid4())
org_id = organizationId
sec_key = secretKey
apiKey = apiKey
method = \"POST\"
query = ?
input = bytearray(f\"{apiKey}\\00{ms}\\00{nonce}\\00\\00{org_id}\\00\\00{method}\\00{path}\\00{query}\", \"utf-8\")
secret=hmac.new(bytearray(sec_key, \"utf-8\"), input, hashlib.sha256).hexdigest()
auth = apiKey + \":\" + secret
#HEADER
header = {
\"X-Time\":ms,
\"X-Nonce\":nonce,
\"X-Organization-Id\":org_id,
\"X-Auth\":auth,
\"X-Request-Id\":req_id}
#BODY
body = {
\"rigId\": \"SOMETHING\",
\"action\": \"STOP\"
}
r=requests.post(url=url, headers=header, params=body)
print(r)
print(r.json())
但是在网站上,该示例演示了您是否想要获得哈希功率订单簿,并且我理解那里的查询字符串。但在我的情况下,我不知道在查询字符串中放入什么。
现在这是我的主要问题,所以其余的代码还没有完成。
标签: python api post python-requests hmac