2012-12-15 87 views
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例子完全一樣。我做錯了什麼?

回答

2

此行是可疑的:

'Rest-Sign': sign.encode('base64').strip() 

你真的想要一個頭的價值,包含文字「\ n」的招牌?這就是編碼(「的base64」)的回報---在你的榜樣,這個字符串:

'Dd1WBn2T5SYTbqMMohOxr46IaLDrkelgH7AgkrrB0mT0PxKfv15vSJ3b6xNdc5PO2Yz9cDpu0u/H\nWIc7bH56sQ==' 

注意\n在中間。我不確定,但可能會刪除所有\n標誌給你你需要的東西。信息已發送!

+0

IT Worked!謝謝! – mpestkow

+0

也爲我工作,但everytimg我得到403響應。我使用了我自己的密鑰/祕密值,但仍然無法登錄 –