8ToThePowerOfMol
8ToThePowerOfMol

Reputation: 91

"can't concat bytes to str" on encoded urlopen request

I am trying to make a hashed user request function using coinmate API, which returns users current balance:

def getBalances(self):
        from urllib.request import Request, urlopen
        url = 'https://coinmate.io/api/balances'
        signature = self.makeSignature()
        values = {
            'clientId': str(self.clientId), 
            'publicKey': str(self.publicKey), 
            'nonce': str(self.nonce), 
            'signature': str(signature)
        }
        _headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        request = Request(url, data=values, headers=_headers)
        response_body = urlopen(request).read().decode('utf-8')   ## Type error here ##
        print(response_body)

... and a always got a TypeError: can't concat bytes to str. As it is in code, I've tried to convert byte type to string, but decode() probably has no effect.

However when I call a simple public request function, it works fine without any nessesary convertions:

def getOrderBook(self):
        from urllib.request import Request, urlopen
        url = 'https://coinmate.io/api/orderBook?currencyPair=BTC_CZK&groupByPriceLimit=False'      
        request = Request(url)
        response_body = urlopen(request).read()
        print(response_body)

Any suggestions are welcomed!

EDIT: In addiction, here is more detailed info:

0) Traceback:

Traceback (most recent call last):
  File "C:\Users\CaptainObvious\documents\visual studio 2015\Projects\bitbot\bitbot\bitbot.py", line 14, in <module>
    exch1.getBalances()
  File "C:\Users\CaptainObvious\documents\visual studio 2015\Projects\bitbot\bitbot\Exchange.py", line 102, in getBalances
    self.userRequest(url, values)
  File "C:\Users\CaptainObvious\documents\visual studio 2015\Projects\bitbot\bitbot\Exchange.py", line 82, in userRequest
    response_body = urlopen(request).read().decode('utf-8')
  File "C:\Users\CaptainObvious\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\CaptainObvious\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 526, in open
    response = self._open(req, data)
  File "C:\Users\CaptainObvious\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 544, in _open
    '_open', req)
  File "C:\Users\CaptainObvious\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Users\CaptainObvious\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\Users\CaptainObvious\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "C:\Users\CaptainObvious\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\CaptainObvious\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\CaptainObvious\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\CaptainObvious\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1064, in _send_output
    + b'\r\n'
TypeError: can't concat bytes to str

Here, it is better to view: https://drive.google.com/open?id=0B9hNaVJ3odeGQjNkTGZCRk1zQWs

1) Atributes like

2) Signature

This is very well described in API documentation:

Signature is created as a message encrypted using HMAC-SHA256 algorithm. Its input contains a nonce, client ID and public API key, such as signatureInput = nonce + clientId + publicApiKey. This signatureInput is then encrypted using private key. Resulting string must be converted to hexadecimal format as 64 characters containing only numbers and digits A to F.

Here it is implemented:

def makeSignature(self):
    import hmac, hashlib
    message = str(self.getNonce()) + str(self.clientId) + str(self.publicKey)
    signature = hmac.new(key=self.privateKey.encode('utf-8'), msg=message.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
    return signature.upper()

3) Nonce

Nonce is generated from timestamp like this:

def getNonce(self):
    import time
    self.nonce = int(time.time())
    return self.nonce

Upvotes: 2

Views: 2318

Answers (1)

Rockarolla
Rockarolla

Reputation: 99

Try to read first and then decode the stream: response_body = urlopen(request).read().decode('utf-8') There is a very similar example on the documentation: https://docs.python.org/3.5/library/urllib.request.html#examples

EDIT: With the added traces I see the problem is the data, not the reponse_body.

To fix it cast the values to bytes:

values = {
        b'clientId': bytes(self.clientId, encoding='utf-8'), 
        b'publicKey': bytes(self.publicKey,encoding='utf-8'), 
        b'nonce': bytes(self.nonce, encoding='utf-8'), 
        b'signature': bytes(signature, encoding='utf-8')
    }

Upvotes: 1

Related Questions