2012-12-15 5 views
5

私はPythonを勉強しています。練習として、ビットコイン市場でトランザクションを行うプログラムを作ろうとしました:https://bitcurex.com。 APIリファレンスはhttps://bitcurex.com/reading-room/APIです。そこPHPクライアントの例があるので、私は、Pythonにそれを翻訳してみましたので、私は持っている:これらのAPIキーが動作している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==') 

- あなただけの彼らと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」の兆候を格納するためのヘッダの値をしたいですか?

'Dd1WBn2T5SYTbqMMohOxr46IaLDrkelgH7AgkrrB0mT0PxKfv15vSJ3b6xNdc5PO2Yz9cDpu0u/H\nWIc7bH56sQ==' 

注途中で\n:これはどのようなエンコード(「base64」の)を返す---あなたの例では、この文字列です。私は確信していますが、おそらくすべてを削除する\n兆候は、あなたが必要なものを提供します。

+0

IT Worked!ありがとう! – mpestkow

+0

私のために働いたが、everytimg私は403応答を得る。自分の鍵/秘密の値を使用しましたが、ログインに失敗した場合でも –

関連する問題