'Why crypto.com exchange marking this as a bad websocket request
I am trying to use https://exchange-docs.crypto.com/spot/index.html#public-auth but when trying to authenticate myself, I am getting a "Bad Request" as a response with 10004 code. From the API docs, the code represents missing parameters.
I checked their sample request provided in document and I am sending all required parameters. I am trying to find out what is causing this so please any help will be appreciated.
request: '{"id": 1, "method": "public/auth", "api_key": “<actual key>”, "nonce": 1646344180357, "sig": “<computed signature>”}’
response: {"id":-1,"code":10004,"method":"unknown","message":"bad request: {"id": 1, "method": "public/auth", "api_key": “<key>“, "nonce": 1646344180357, "sig": “<sig>“}”}
Code:
class CryptoAPIClient:
__id = 0
def __init__(self):
try:
self.ws = websocket.WebSocketApp(MARKETBASEURL,
on_open=self.onOpen, on_close=self.onClose, on_message=self.onMsg)
self.ws.run_forever()
except BaseException as e:
print(e)
#when wesocket connection opens, send request with authentication info
def onOpen(self, ws):
auth_req = {
"id": self.getNewId(),
"method": "public/auth",
"api_key": APIKEY,
"nonce" : self.getNonce(),
}
payload_str = auth_req['method'] + str(auth_req['id']) + auth_req['api_key'] + "" + str(auth_req['nonce'])
auth_req['sig'] = self.__makeSig(payload_str)
#also tried adding endpoint to url by ws.url += /public/auth
ws.send(json.dumps(auth_req))
#sleep for 1 second as per crypto.com recommendation
time.sleep(1)
print("Opened a webscoket connection")
#when connection is closed
#NOTE: for some reason this is not getting called!!
def onClose(self, ws):
print("Closed connection")
#when message is received
def onMsg(self, ws, msg):
print("message received: " , msg)
#id for new websocket request
def getNewId(self):
if self.__id <= 1000:
return self.__id + 1
else:
return 1
#get nonce
def getNonce(self):
return int(time.time() * 1000)
#create a signature for the request
def __makeSig(self, payload_str):
return hmac.new(
bytes(str(SECRETKEY), 'utf-8'),
msg=bytes(payload_str, 'utf-8'),
digestmod=hashlib.sha256
).hexdigest()
Thank you very much!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|