1

Working example to generate a valid url (including signature) for the Huobi API.

In the Huobi API documenation there is no explicit example that allows you to verify your signature creation method step by step.

My intention is to create that here, but I need help, because I haven't managed yet.

Try to order using huobi api

First, I succeed to get account information

from datetime import datetime
import requests
import json
import hmac
import hashlib
import base64
from urllib.parse import urlencode

#Get all Accounts of the Current User
#apiAccessKey = hb_access, apiSecretKey = hb_secret

timestamp = str(datetime.utcnow().isoformat())[0:19]
params = urlencode({'AccessKeyId': hb_access,
                    'SignatureMethod': 'HmacSHA256',
                    'SignatureVersion': '2',
                    'Timestamp': timestamp
                   })
method = 'GET'
endpoint = '/v1/account/accounts'
base_uri = 'api.huobi.pro'
pre_signed_text = method + '\n' + base_uri + '\n' + endpoint + '\n' + params
hash_code = hmac.new(hb_secret.encode(), pre_signed_text.encode(), hashlib.sha256).digest()
signature = urlencode({'Signature': base64.b64encode(hash_code).decode()})
url = 'https://' + base_uri + endpoint + '?' + params + '&' + signature
# url = 'https://' + base_uri + endpoint2 + '?' + params + '&' + signature
response = requests.request(method, url)
accts = json.loads(response.text)

print(accts)

it gives :

{'status': 'ok', 'data': [{'id': 1153465, 'type': 'spot', 'subtype': '', 'state': 'working'}, {'id': 1170797, 'type': 'otc', 'subtype': '', 'state': 'working'}]}

but, I have problem with market order. want to look matchresults :

here is the discription from the website https://alphaex-api.github.io/openapi/spot/v1/en/#search-match-results

GET /v1/order/matchresults

Parameter DataType Required Default Description Value Range symbol string true btcusdt, bccbtc.Refer to

I know I should add symbol parameter, but I don't know how I do that,,, and my code :

timestamp = str(datetime.utcnow().isoformat())[0:19]
params = urlencode({'AccessKeyId': hb_access,
                    'SignatureMethod': 'HmacSHA256',
                    'SignatureVersion': '2',
                    'Timestamp': timestamp,
                   })

method = 'GET'
base_uri = 'api.huobi.pro'
enpoint ='/v1/order/matchresults'
pre_signed_text = base_uri + '\n' + endpoint + '\n' + params
hash_code = hmac.new(hb_secret.encode(), pre_signed_text.encode(), hashlib.sha256).digest()
signature = urlencode({'Signature': base64.b64encode(hash_code).decode()})

url = 'https://'+base_uri + endpoint +'?'+params+'&'+signature
resp = requests.request(method, url)
resp.json()

it gives :

{'status': 'error',
 'err-code': 'api-signature-not-valid',
 'err-msg': 'Signature not valid: Verification failure [校验失败]',
 'data': None}

I need help... and another question is : is way to make Sinature different with using Get method from using POST method? like placing market order(POST /v1/order/orders/place), when I request using POST do I need to give different Signature?

GwiHwan Go
  • 51
  • 3

1 Answers1

0

To get exact symbol you have to provide required parameters in param variable

params = urlencode({'AccessKeyId': hb_access,
                    'SignatureMethod': 'HmacSHA256',
                    'SignatureVersion': '2',
                    'Timestamp': timestamp,
                    "symbol": "btcusdt"  # allowed symbols: GET /v1/common/symbols              
                   })

url variable:

https://api.huobi.pro/v1/order/matchresults?AccessKeyId=39****-b******-4*****-3****&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2022-09-26T19%3A09%3A16&symbol=btcusdt&Signature=1K**************************

Output:

{'status': 'ok', 'data': []}

Here is my way adding parameters and making request:

def huobi(endpoint: str, extra_params: dict={}, AccessKeyId: str=None, SecretKey: str=None) -> dict:
    AccessKeyId = 'xxxxxx-xxxxxx-xxxxxx-xxxx'  # tmp
    SecretKey = 'xxxxxx-xxxxxx-xxxxxx-xxxx'    # tmp
    timestamp = str(datetime.utcnow().isoformat())[0:19]

    standart_params = {
        'AccessKeyId': AccessKeyId,
        'SignatureMethod': 'HmacSHA256',
        'SignatureVersion': '2',
        'Timestamp': timestamp
    }
    params = dict(standart_params, **extra_params)

    params_url_enc = urlencode(
        sorted(params.items(), key=lambda tup: tup[0])
    )

    pre_signed = 'GET\n'
    pre_signed += 'api.huobi.pro\n'
    pre_signed += f'{endpoint}\n'
    pre_signed += params_url_enc

    sig_bin = hmac.new(
        SecretKey.encode(),
        pre_signed.encode(),
        hashlib.sha256
    ).digest()

    sig_b64_bytes = base64.b64encode(sig_bin)
    sig_b64_str = sig_b64_bytes.decode()
    signature = urlencode({'Signature': sig_b64_str})

    url = f'https://api.huobi.pro{endpoint}?'
    url += params_url_enc + '&'
    url += signature

    return requests.get(url).json()


endpoint = "/v2/account/valuation"
extra_params = {
    "accountType": "1",
    "valuationCurrency": "BTC"
}

print(huobi(endpoint=endpoint, extra_params=extra_params))
Radikulit
  • 48
  • 1
  • 7