[Python] What to put in query string in post request? -NiceHash API

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a python program that turns on/off my mining rig with Nicehash API. I did everything that I need to but I am stuck with what to put in query string.

    In header of post request must be X-auth which which is built from API Key and HMAC signature. On NiceHash website it says that HMAC signature must be build like this:

    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.


    So input should look like this (already hashed in UTF-8) and then be hashed again (in order from first to last:

    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)


    My code:

    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":eek:rg_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())


    But on website the example demostrates if you want to get a hashpower order book and I understand query string there. But I dont know what to put in query string in my case.

    For now this is the main problem for me so the rest of the code is not finished yet.

    Continue reading...

Compartilhe esta Página