5
我正在学习Python,并将其作为一个练习,我试图让一个程序在比特币市场进行交易:https://bitcurex.com。这里是一个API参考:https://bitcurex.com/reading-room/API。有一个PHP客户端的例子,所以我试图把它翻译到Python,所以我有:将PHP翻译成Python(Rest-API连接)
import math
import time
import simplejson
import urllib
import urllib2
import hmac,hashlib
def microtime():
return '%f %d' % math.modf(time.time())
def query(path, key, secret, data={}):
mt = microtime().split()
nonce = mt[1] + mt[0][2:]
data['nonce'] = nonce
post_data = urllib.urlencode(data)
sign = hmac.new(secret.decode('base64'), post_data, hashlib.sha512).digest()
headers = {'Rest-Key' : key,
'Rest-Sign': sign.encode('base64').strip(),
'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
'Content-type': 'application/x-www-form-urlencoded'}
print headers
url = 'https://bitcurex.com/api/0/' + path
req = urllib2.Request(url, post_data, headers)
response = urllib2.urlopen(req)
return simplejson.loads(response.read())
print query('getFunds', '29a28e8fe234537056a8b256c0df50413f50da9c49ca61991ea8b8f108a88e09', 'y2NDxKGa/xvhtXrDP+3oscbBUFSac9+T8jzu2nRmt0vBdHbbl8NRqdmxKFr2IwwY5LAskTQZGyy2XONaNN6Jrg==')
这些API密钥的工作 - 你只能让getFunds查询他们。
它一直返回错误“我必须登录”。我想看看通过提琴手代理调试这一请求,并在这里你有尝试的标题:
POST /api/0/getFunds HTTP/1.1
Accept-Encoding: identity
Rest-Sign: Dd1WBn2T5SYTbqMMohOxr46IaLDrkelgH7AgkrrB0mT0PxKfv15vSJ3b6xNdc5PO2Yz9cDpu0u/H
WIc7bH56sQ==:
Content-Length: 22
Rest-Key: 29a28e8fe234537056a8b256c0df50413f50da9c49ca61991ea8b8f108a88e09
Connection: close
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)
Host: bitcurex.com
Content-Type: application/x-www-form-urlencoded
提琴手是显示我的错误:
Incorrectly formed request headers.
Missing colon in header #3, WIc7bH56sQ==
什么想法?看起来像我的休息标志太长或类似的东西。我认为我的代码应该和PHP例子完全一样。我做错了什么?
IT Worked!谢谢! – mpestkow
也为我工作,但everytimg我得到403响应。我使用了我自己的密钥/秘密值,但仍然无法登录 –