I need help with PHP to Python 3+ code conversion. By the link https://icobench.com/developers at Auth and examples
section, you can find the code of a class for work with website API.
I already wrote Python class, but it doesn't work correctly.
import html
import io
import json
import hashlib
import hmac
import pycurl
class ICObenchAPI:
private_key = ''
public_key = ''
api_url = 'https://icobench.com/api/v1/'
result = None
def get_icos(self, parameter='all', data=''):
return self.send('icos/{0}'.format(parameter), data)
def get_ico(self, ico_id, data=''):
return self.send('ico/{0}'.format(str(ico_id)), data)
def get_other(self, parameter):
return self.send('other/{0}'.format(parameter), '')
def get_people(self, parameter='registered', data=''):
return self.send('people/{0}'.format(parameter), data)
def send(self, action, data):
data_json = json.dumps(data)
print("data_json: ", data_json)
#$sig = base64_encode(hash_hmac('sha384', $dataJson, $this->privateKey, true));
signature = hmac.new(self.private_key.encode('utf-8'), msg=data_json.encode('utf-8'), digestmod=hashlib.sha384).hexdigest()
print("signature: ", signature)
headers = [
'Content-Type: application/json',
'Content-Length: {0}'.format(len(data_json)),
'X-ICObench-Key: {0}'.format(self.public_key),
'X-ICObench-Sig: {0}'.format(signature)
]
print("headers: ", headers)
buffer = io.BytesIO()
action_url = self.api_url + action
print("action_url: ", action_url)
ch = pycurl.Curl()
ch.setopt(pycurl.URL, action_url)
ch.setopt(pycurl.POSTFIELDS, data_json)
ch.setopt(pycurl.SSL_VERIFYPEER, False)
ch.setopt(pycurl.HTTPHEADER, headers)
ch.setopt(pycurl.WRITEFUNCTION, buffer.write)
ch.perform()
reply = buffer.getvalue().decode('UTF-8')
ff = reply
reply = json.loads(reply)
if reply['error'] is not None:
self.result = reply['error']
return False
elif reply['message'] is not None:
self.result = reply['message']
return True
elif reply is not None:
self.result = json.dumps(reply)
return True
else:
self.result = html.escape(ff)
return False
def get_result(self):
return self.result
A server already answering:
Invalid Content Verification Signature
I think the problem with HMAC part. What is wrong with my code? Thanks!