我正在尝试使用coinmate API创建一个哈希用户请求函数,该函数返回用户当前余额:
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)
...并且总是得到一个TypeError:无法将字节连接到str。 因为它在代码中,我试图将字节类型转换为字符串,但decode()可能没有效果。
但是,当我调用一个简单的公共请求函数时,它可以正常运行而无需任何转换:
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)
欢迎任何建议!
编辑:瘾,这里有更详细的信息:
0)回溯:
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
此处,最好查看:https://drive.google.com/open?id=0B9hNaVJ3odeGQjNkTGZCRk1zQWs
1)属性如
2)签名
这在API文档中有很好的描述:
签名创建为使用HMAC-SHA256算法加密的消息。它的输入包含一个nonce,客户端ID和公共API密钥,例如signatureInput = nonce + clientId + publicApiKey。然后使用私钥加密此signatureInput。必须将生成的字符串转换为十六进制格式,即64个字符,仅包含数字和数字A到F.
这里实施:
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是从时间戳生成的,如下所示:
def getNonce(self):
import time
self.nonce = int(time.time())
return self.nonce
答案 0 :(得分:0)
编辑: 通过添加的跟踪,我看到问题是数据,而不是reponse_body。
要修复它,请将值转换为字节:
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')
}