2017-04-02 6 views
0

私はpythonで新しいです。ほんの数時間それを学ぶ。Pythonでエラーが発生したBlinktrade Rest APIを使用しています

私はここに...いくつかのアカウントに関する情報を取得するために、REST APIを消費する

をしようとしているが、私のリクエストです:

def getAccountData(): 
    nonce = int(datetime.datetime.now().timestamp()) 
    signature = hmac.new(b'TDDh8djV3NwXt53gSrScDul6o6w3HnnZsHuh6HTF9SA', msg=nonce, digestmod=hashlib.sha256).digest() 
    print(signature) 
    headers = { 
      'content-type': 'application/json', 
      'APIKey':conf['API_KEY'], 
      'Nonce':str(nonce), 
     } 
    data = { 
      "MsgType": "U2", 
      "BalanceReqID": 1 
     } 
    r = requests.post('https://api.blinktrade.com/tapi/v1/message', data=data, headers=headers) 
    print(r.json()) 

そして、ここではエラーです:

Traceback (most recent call last): File "foxbit.py", line 51, in getAccountData() File "foxbit.py", line 30, in getAccountData signature = hmac.new(b'TDDh8djV3NwXt53gSrScDul6o6w3HnnZsHuh6HTF9SA', msg=nonce, digestmod=hashlib.sha256).digest() File "C:\Python\Python35\lib\hmac.py", line 144, in new return HMAC(key, msg, digestmod) File "C:\Python\Python35\lib\hmac.py", line 84, in init self.update(msg) File "C:\Python\Python35\lib\hmac.py", line 93, in update self.inner.update(msg) TypeError: object supporting the buffer API required

私はこのAPIを消費しようとしています: https://blinktrade.com/docs/?shell#balance on balanceメソッド

成功しません。

私はBitcoinの開かれた注文を見るために、Pythonアプリケーションを作成したいと思います。 このエラーは何が起こっていましたか?ドキュメントでは、私は仕事にこれを行う必要があると言います:

{ 
    "MsgType": "U2", 
    "BalanceReqID": 1 
} 
message='{ "MsgType": "U2", "BalanceReqID": 1 }' 

api_url='API_URL_REST_ENDPOINT' 
api_key='YOUR_API_KEY_GENERATED_IN_API_MODULE' 
api_secret='YOUR_SECRET_KEY_GENERATED_IN_API_MODULE' 

nonce=`date +%s` 
signature=`echo -n "$nonce" | openssl dgst -sha256 -hex -hmac "$api_secret" | cut -d ' ' -f 2` 

curl -X POST "$api_url"    \ 
    -H "APIKey:$api_key"    \ 
    -H "Nonce:$nonce"     \ 
    -H "Signature:$signature"   \ 
    -H "Content-Type:application/json" \ 
    -d "$message" 

3時間試してみてください! hehe 私はここでいくつかの助けが必要です。

答えて

0

重要な問題は、署名を生成するときにナンスをbytearray形式として渡していないことです。

これは予想通りナンスのbytearrayを渡すと、動作するはずのpython 3.4

hmac now accepts bytearray as well as bytes for the key argument to the new() function, and the msg parameter to both the new() function and the update() method now accepts any type supported by the hashlib module. (Contributed by Jonas Borgström in bpo-18240.)

https://docs.python.org/3/whatsnew/3.4.html#hmac

後の変更です。

signature = hmac.new(b'SECRET', msg=bytearray(nonce), digestmod=hashlib.sha256).digest() 

また、Signatureヘッダーを忘れています。

非常に有用なPythonの例を持つこの要点に従うことができますが、実際にはpython2上に構築されていますが、引き続きそれを実行することができます。 https://gist.github.com/pinhopro/60b1fd213b36d576505e

BlinkTradeの従業員は、私に何か質問をすることはありません。

関連する問題