2012-12-15 13 views
5

Sto imparando Python e come esercizio ho provato a creare un programma per effettuare transazioni sul mercato dei bitcoin: https://bitcurex.com. Ecco un riferimento API: https://bitcurex.com/reading-room/API. C'è un esempio di client PHP, così ho cercato di tradurlo a Python, così ho ottenuto:Traduzione di PHP in Python (connessione 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==') 

Queste chiavi API stanno lavorando - è possibile effettuare solo interrogazione getFunds con loro.

Continua a restituire l'errore "Mi deve accedere". Ho provato a cercare su tale domanda attraverso Fiddler Proxy Debugger, e qui si hanno le intestazioni di quel tentativo:

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 

Fiddler mi sta mostrando un errore:

Incorrectly formed request headers. 
Missing colon in header #3, WIc7bH56sQ== 

tutte le idee? Sembra che il mio Rest-Sign sia troppo lungo o qualcosa del genere. Penso che il mio codice dovrebbe fare esattamente lo stesso dell'esempio PHP. Cosa sto facendo di sbagliato?

risposta

2

Questa linea è sospetto:

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

cosa si vuole veramente il valore di un colpo di testa di contenere letterale '\ n' segni? Questo è ciò che codificano i rendimenti ('base64') --- nel tuo esempio, questa stringa:

'Dd1WBn2T5SYTbqMMohOxr46IaLDrkelgH7AgkrrB0mT0PxKfv15vSJ3b6xNdc5PO2Yz9cDpu0u/H\nWIc7bH56sQ==' 

Nota la \n nel mezzo. Non sono sicuro, ma probabilmente rimuovere tutti i segni \n ti dà quello che ti serve.

+0

IT ha funzionato! Grazie! – mpestkow

+0

Ha funzionato anche per me, ma ogni volta ottengo 403 risposte. Ho usato la mia chiave/valore segreto ma ancora se non riesce ad accedere –