You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用NiceHash API控制矿机时POST请求Query String设置疑问

关于NiceHash API POST请求Query String设置的问题

我正在开发一款通过NiceHash API实现矿机启停控制的Python程序,其余开发步骤均已完成,但卡在POST请求的Query String设置环节。

POST请求的请求头中必须包含由API Key和HMAC签名生成的X-auth字段,NiceHash官网文档规定HMAC签名的输入结构如下:

输入结构如下:
-API Key
-X-Time请求头值
-X-Nonce请求头值
-空字段
-X-Organization-Id请求头值
-空字段
-请求方法(示例:GET、POST、DELETE、PUT)
-请求路径(示例:/main/api/v2/hashpower/orderBook)
-请求Query String(示例:algorithm=X16R&page=0&size=100,需与发送给服务器的内容一致,不带开头的问号)

输入为按顺序排列的字段组成的字节数组,使用空字节(0x00)作为分隔符。第一个字段前和最后一个字段后无分隔符。部分字段始终为空,此时分隔符会连续出现。请求体需使用发送给服务器的原始字节,JSON消息需采用UTF-8字符编码。

签名输入示例(⊠代表0x00空字节):

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)

我的Python代码如下:

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())

官网示例针对的是获取算力订单簿的场景,我理解该场景的Query String设置,但不清楚调用/mining/rigs/status2接口时应填写什么内容作为Query String。


解决方案

  1. Query String的设置:
    对于/mining/rigs/status2这个POST接口,它的参数是通过请求体(JSON)传递的,不需要在URL中添加查询参数,因此query直接设为空字符串即可:

    query = ""
    
  2. POST请求参数修正:
    你代码中错误地使用了params=body,params是用来传递URL查询参数的,POST请求的JSON请求体应该用json=body参数,否则服务器无法正确解析你的请求内容:

    r=requests.post(url=url, headers=header, json=body)
    

修正后的完整代码片段

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, json=body)


print(r)
print(r.json())

内容的提问来源于stack exchange,提问作者MaR_11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 12:15:34