2017-04-02 62 views
0

我是新的python。只需幾個小時的時間來學習消費Blinktrade Rest API與Python給出的錯誤

我試圖消耗的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方法

沒有成功。

我想創建和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小時嘗試它,沒有什麼!呵呵 我需要一些幫助。

回答

0

主要問題是您在生成簽名時沒有將隨機數作爲bytearray格式傳遞。

這是蟒蛇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

薪火隨機數確定bytearray應該按預期工作後的變化。

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

另外,您忘記了Signature標題。

你可以按照這個有非常有用的python例子的要點,它實際上建立在python2上,但你仍然可以關注它,並且它很容易開始。 https://gist.github.com/pinhopro/60b1fd213b36d576505e

作爲BlinkTrade的員工,請不要問我任何問題。