【问题标题】:poloniex 403 forbidden using python3.5poloniex 403 禁止使用 python3.5
【发布时间】: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 的示例包装器

API doc

如何获得与 poloniex 的基本经过身份验证的连接?

【问题讨论】:

    标签: python python-3.x api request urllib


    【解决方案1】:

    根据the Official Documentation

    'b' :二进制格式。输出以 2 为底的数字。

    在您的代码中,您将密钥和秘密提供为binary,但它必须是string

    所以请替换:

    APIkey = b'provert-kee'
    secret = b'ceecret'
    

    通过

    APIkey = 'provert-kee' # or s'provert-kee'
    secret = 'ceecret' # or s'ceecret'
    

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      相关资源
      最近更新 更多