Last active
January 19, 2019 21:20
-
-
Save MarvinBaral/e30763aac68c9dbc8ca6d7f3d20b3a4e to your computer and use it in GitHub Desktop.
Bitforex API Call with signed Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# See https://github.com/bitforexapi/API_Doc_en/wiki/API-Call-Description | |
import urllib | |
import requests, json, time | |
import hmac, hashlib | |
def simple_api_call(requestLink, params) : #GET | |
url = "https://api.bitforex.com" + requestLink | |
i = 0 | |
for key, value in params.iteritems(): | |
if i == 0: | |
url += "?" | |
else: | |
url += "&" | |
url += key + "=" + str(value) | |
i += 1 | |
response = urllib.urlopen(url) | |
data = json.loads(response.read()) | |
return data | |
def signed_api_call(requestLink, params, secretKey) : #POST | |
#=============================================================================== | |
#create data to sign | |
#=============================================================================== | |
url = "https://api.bitforex.com" + requestLink | |
timestamp = int(round(time.time() * 1000)) | |
params["nonce"] = timestamp #add timestamp | |
sortedPostData = sorted(params.items()) #alphabetical order important!!! | |
postDataString = requestLink | |
i = 0 | |
for key, value in sortedPostData: | |
if i == 0: | |
postDataString += "?" | |
else: | |
postDataString += "&" | |
postDataString += key + "=" + str(value) | |
i += 1 | |
#=============================================================================== | |
#sign the data | |
#=============================================================================== | |
contentToSign = postDataString | |
secret = secretKey.encode(encoding='UTF-8') | |
signData = hmac.new(secret, postDataString, digestmod=hashlib.sha256).hexdigest() | |
#=============================================================================== | |
#send signed request | |
#=============================================================================== | |
sortedPostData.append(('signData', signData)) | |
r = requests.post(url, data=sortedPostData, allow_redirects=False) | |
respData = json.loads(r.content) | |
return respData |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use: