GwiHwan Go
GwiHwan Go

Reputation: 51

Python request post vs get, API key example

I'm working on huobi API, but I have a problem on sending signature.

I got the data from 'GET' method, but I don't know how I do this with 'POST' method.

I need help.

Here is the description of the website:

https://alphaex-api.github.io/openapi/spot/v1/en/#authentication

# access_key = hb_access, secret_key = hb_secret

timestamp = str(datetime.utcnow().isoformat())[0:19]
params = urlencode({'AccessKeyId': hb_access,
                    
                    'SignatureMethod': 'HmacSHA256',
                    
                    'SignatureVersion': '2',
                    
                    'Timestamp': timestamp,
                    'account-id':account_id,
                   })
account_id = accts['data'][0]['id']
method = 'GET'

endpoint = '/v1/account/history'

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
response = requests.request(method, url)
r = json.loads(response.text)
print(url)
print(r)

it gives :

https‍://api.huobi.pro/v1/account/history?AccessKeyId=bf32d9ad-bgrdawsdsd-323042b0-d80d8&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2021-05-23T00%3A17%3A55&account-id=1153465&Signature=FiknimWYr8eaIg%2Bccth1PWF8P%2FY3O8x%2Bp%2FJGsxV9Psc%3D
{'status': 'ok', 'data': []}

but with 'POST' method, here is the website description of placing market order:

https://alphaex-api.github.io/openapi/spot/v1/en/#place-a-new-order

#Let's try to order!
data = {
      'symbol' :  'xrpusdt',
      'type' : 'buy-market',
      'amount' : '5',
       'price' : '200'
      }
params = urlencode({'AccessKeyId': hb_access,
                   'SignatureMethod': 'HmacSHA256',
                   'SignatureVersion': '2',
                   'Timestamp': timestamp,
                  })
method = 'POST'
base_uri = 'api.huobi.pro'
# base_uri = 'https://api-cloud.huobi.co.kr'
endpoint = '/v1/order/orders/place'
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
resp = requests.post(url, data=data)
print(url)
print(resp.json())

it gives :

https‍://api.huobi.pro/v1/order/orders/place?AccessKeyId=bf32d9ad-bgrdawsdsd-323042b0-d80d8&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2021-05-23T01%3A23%3A39&Signature=yabd%2B%2FcQ%2F1lGo6HxNUzzu9x9aadEOGSBbfRicmwpAD0%3D
{'status': 'error', 'err-code': 'api-signature-not-valid', 'err-msg': 'Signature not valid: Verification failure [校验失败]', 'data': None}

Upvotes: 0

Views: 885

Answers (1)

yang zhou
yang zhou

Reputation: 318

You post usage is correct.

But I guess the API should accept Content-Type="application/json"

So you should use resp = request.post(url, json=data)

Upvotes: 1

Related Questions