· 5 years ago · Aug 04, 2020, 10:02 AM
1import requests
2import json
3import hmac, hashlib
4import urllib
5import time
6
7url = 'https://cex.io/api/balance/'
8cex_key = "my_key"
9cex_sign = b"my_private_key"
10user_id = "my_id"
11nonce = str(int(time.time()*1000))
12
13
14def api_query():
15 req = {
16 "key": cex_key,
17 "id": user_id,
18 "nonce": nonce
19 }
20 post_data = urllib.parse.urlencode(req)
21 sign = hmac.new(cex_sign, post_data.encode('utf-8'), hashlib.sha512).hexdigest()
22 headers = {
23 'Content-Type': 'application/x-www-form-urlencoded',
24 'key': cex_key,
25 'signature': sign,
26 # "nonce": nonce
27 }
28
29 with requests.Session() as s:
30 api = s.post(url, data=post_data, headers=headers)
31 data = json.loads(api.text)
32 return data
33
34
35res = api_query()
36
37print(res)
38