我试着用Poloniex API。我尝试通过贸易API方法获得平衡。我试着用请求库来实现它,如下所示:
import requests
import hmac
import hashlib
import time
import urllib
def setPrivateCommand(self):
poloniex_data = {'command': 'returnBalances', 'nonce': int(time.time() * 1000)}
post_data = urllib.parse.urlencode(poloniex_data).encode()
sig = hmac.new(str.encode(app.config['HMAC_KEYS']['Poloniex_Secret']), post_data, hashlib.sha512).hexdigest()
headers = {'Sign': sig, 'Key': app.config['HMAC_KEYS']['Poloniex_APIKey']}
polo_request = requests.post('https://poloniex.com/tradingApi', data=post_data, headers=headers, timeout=20)
polo_request = polo_request.json()
print('Request: {0}'.format(polo_request))
return polo_request在这段代码中,我总是收到错误消息:“请求:{' error ':‘无效命令.’}”。我做错了什么?
从另一边代码下面返回数据,没有任何问题!请看这个:
import requests
import hmac
import hashlib
import json
import time
import urllib
def setPrivateCommand(self):
poloniex_data = {'command': 'returnBalances', 'nonce': int(time.time() * 1000)}
post_data = urllib.parse.urlencode(poloniex_data).encode()
sig = hmac.new(str.encode(app.config['HMAC_KEYS']['Poloniex_Secret']), post_data, hashlib.sha512).hexdigest()
headers = {'Sign': sig, 'Key': app.config['HMAC_KEYS']['Poloniex_APIKey']}
req = urllib.request.Request('https://poloniex.com/tradingApi', data=post_data, headers=headers)
res = urllib.request.urlopen(req, timeout=20)
Ret_data = json.loads(res.read().decode('utf-8'))
print('Request: {0}'.format(Ret_data))
return Ret_data我使用Python 3.6
发布于 2017-12-18 01:31:07
最好让requests处理post数据,因为它创建了适当的标头。除此之外,我看不出你的代码有什么问题。
def setPrivateCommand(self):
poloniex_data = {'command': 'returnBalances', 'nonce': int(time.time() * 1000)}
post_data = urllib.parse.urlencode(poloniex_data).encode()
sig = hmac.new(
str.encode(app.config['HMAC_KEYS']['Poloniex_Secret']), post_data, hashlib.sha512
).hexdigest()
headers = {'Sign': sig, 'Key': app.config['HMAC_KEYS']['Poloniex_APIKey']}
polo_request = requests.post(
'https://poloniex.com/tradingApi', data=poloniex_data, headers=headers, timeout=20
)
polo_request = polo_request.json()
print('Request: {0}'.format(polo_request))
return polo_request 或者您可以在headers中指定“Content”,例如,如果您希望在data中有一个字符串,
headers = {
'Sign': sig, 'Key': app.config['HMAC_KEYS']['Poloniex_APIKey'],
'Content-Type': 'application/x-www-form-urlencoded'
}
polo_request = requests.post(
'http://httpbin.org/anything', data=post_data, headers=headers, timeout=20
)https://stackoverflow.com/questions/47859245
复制相似问题