Python Poloniex API 调用
Python Poloniex API Call
我有以下代码,我正在尝试根据 their instructions
对 Poloniex 进行 API 调用
import urllib
import urllib.request
import json
import time
import hashlib
import codecs
import hmac
import time
Key = "whatever your key is"
Sign = "whatever your secret is"
def returnBalances(balances):
nonce = int(round(time.time()-599900000)*10)
parms = {"returnBalances":balances,
"nonce":nonce}
parms = urllib.parse.urlencode(parms)
hashed = hmac.new(b'Sign',digestmod=hashlib.sha512)
signature = hashed.hexdigest()
headers = {"Content-type":"application/x-www-form-urlencoded",
"Key":Key,
"Sign":signature}
conn = urllib.request.urlopen("https://poloniex.com")
conn.request("POST","/tradingApi",parms,headers)
response = conn.getresponse()
print(response.status,response.reason)
returnBalances('balances')
当我 运行 时,我收到此错误消息
HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
有人可以帮忙吗?
- 您可以使用
urllib.error.HTTPError
捕获 HTTP 错误
- POST 数据应该是字节,所以你必须编码
parms
urllib.request.urlopen
returns一个HTTPResponse
object,没有request
方法。
如果你想设置 headers 和其他参数你应该使用 urllib.request.Request
- 根据 api docs,post 参数应为 'nonce' 和 'command',因此我修改了您的函数以接受 'returnBalances' 作为参数,并且在
parms["command"]
中使用
def api_call(command):
nonce = int(round(time.time()-599900000)*10)
parms = {"command":command, "nonce":nonce}
parms = urllib.parse.urlencode(parms).encode()
hashed = hmac.new(Sign.encode(), parms, digestmod=hashlib.sha512)
signature = hashed.hexdigest()
headers = {"Key":Key, "Sign":signature}
req = urllib.request.Request("https://poloniex.com/tradingApi", headers=headers)
try:
conn = urllib.request.urlopen(req, data=parms)
except urllib.error.HTTPError as e:
conn = e
print(conn.status,conn.reason)
return json.loads(conn.read().decode())
balances = api_call("returnBalances")
我有以下代码,我正在尝试根据 their instructions
对 Poloniex 进行 API 调用import urllib
import urllib.request
import json
import time
import hashlib
import codecs
import hmac
import time
Key = "whatever your key is"
Sign = "whatever your secret is"
def returnBalances(balances):
nonce = int(round(time.time()-599900000)*10)
parms = {"returnBalances":balances,
"nonce":nonce}
parms = urllib.parse.urlencode(parms)
hashed = hmac.new(b'Sign',digestmod=hashlib.sha512)
signature = hashed.hexdigest()
headers = {"Content-type":"application/x-www-form-urlencoded",
"Key":Key,
"Sign":signature}
conn = urllib.request.urlopen("https://poloniex.com")
conn.request("POST","/tradingApi",parms,headers)
response = conn.getresponse()
print(response.status,response.reason)
returnBalances('balances')
当我 运行 时,我收到此错误消息
HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
有人可以帮忙吗?
- 您可以使用
urllib.error.HTTPError
捕获 HTTP 错误
- POST 数据应该是字节,所以你必须编码
parms
urllib.request.urlopen
returns一个HTTPResponse
object,没有request
方法。
如果你想设置 headers 和其他参数你应该使用urllib.request.Request
- 根据 api docs,post 参数应为 'nonce' 和 'command',因此我修改了您的函数以接受 'returnBalances' 作为参数,并且在
parms["command"]
中使用
def api_call(command):
nonce = int(round(time.time()-599900000)*10)
parms = {"command":command, "nonce":nonce}
parms = urllib.parse.urlencode(parms).encode()
hashed = hmac.new(Sign.encode(), parms, digestmod=hashlib.sha512)
signature = hashed.hexdigest()
headers = {"Key":Key, "Sign":signature}
req = urllib.request.Request("https://poloniex.com/tradingApi", headers=headers)
try:
conn = urllib.request.urlopen(req, data=parms)
except urllib.error.HTTPError as e:
conn = e
print(conn.status,conn.reason)
return json.loads(conn.read().decode())
balances = api_call("returnBalances")