【发布时间】:2017-11-28 13:48:38
【问题描述】:
我正在尝试创建与 poloniex 的基本经过身份验证的连接,但我不断收到从他们的 API 返回的 403 禁止错误。
time import time
import urllib.request
import urllib.parse
import hashlib
import hmac
APIkey = b'provert-kee'
secret = b'ceecret'
url = 'https://poloniex.com/tradingApi'
payload = {
'command': 'returnBalances',
'nonce': int(time() * 1000),
}
paybytes = urllib.parse.urlencode(payload).encode('utf8')
print(paybytes)
sign = hmac.new(secret, paybytes, hashlib.sha512).hexdigest()
print(sign)
headers = {
'Key': APIkey,
'Sign': sign,
}
req = urllib.request.Request(url, headers=headers, data=paybytes)
with urllib.request.urlopen(req) as response:
the_page = response.read()
print(the_page)
输出:
#python3 apicalltest3.py
b'command=returnBalances&nonce=1498380606389'
5555555
Traceback (most recent call last):
File "apicalltest3.py", line 29, in <module>
with urllib.request.urlopen(req) as response:
File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.5/urllib/request.py", line 472, in open
response = meth(req, response)
File "/usr/lib/python3.5/urllib/request.py", line 582, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.5/urllib/request.py", line 510, in error
return self._call_chain(*args)
File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
result = func(*args)
File "/usr/lib/python3.5/urllib/request.py", line 590, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
在他们的支持页面上,他们在 python2.7 中也有一个使用 hmac 512 的示例包装器
如何获得与 poloniex 的基本经过身份验证的连接?
【问题讨论】:
标签: python python-3.x api request urllib