2017-09-12 8 views
0

認証しようと、私はこのエラーが表示されます。 KeyError例外:私が使用する「mykeystring」Python 3.xでBitfinex REST APIへの認証されたアクセスには何が間違っていますか?

キーは、私はそれでモバイルアプリへのアクセスを得ることができるよう、正しいです。このクライアントを認証しようとする前に、モバイルアプリから切断します。このコードは、API v2 docsとほぼ同じですが、Python 2.xのために書かれているようですが、一番下のprint関数にかっこを付け加えました。 Python 3との互換性のために必要な変更はありますか?

import json 
import base64 
import requests 
import datetime 
import hashlib 
import hmac 
import time 
import os 

class BitfClient(object): 
    BASE_URL = "https://api.bitfinex.com/ 
    BFX_KEY = 'mykeystring' 
    BFX_SECRET = 'mysecretstring' 

    KEY = os.environ[BFX_KEY] 
    SECRET = os.environ[BFX_SECRET] 

    def _nonce(self): 
     return str(int(round(time.time() * 10000))) 

    def _headers(self, path, nonce, body): 
     signature = "/api/" + path + nonce + body 
     h = hmac.new(self.SECRET, signature, hashlib.sha384) 
     signature = h.hexdigest() 
     return { 
      "bfx-nonce": nonce, 
      "bfx-apikey": self.KEY, 
      "bfx-signature": signature, 
      "content-type": "application/json" 
     } 

    def req(self, path, params={}): 
     nonce = self._nonce() 
     body = params 
     rawBody = json.dumps(body) 
     headers = self._headers(path, nonce, rawBody) 
     url = self.BASE_URL + path 
     resp = requests.post(url, headers=headers, data=rawBody, verify=True) 
     return resp 

    def active_orders(self): 
     """ 
     Fetch active orders 
     """ 
     response = self.req("v2/auth/r/orders") 
     if response.status_code == 200: 
      return response.json() 
     else: 
      print(response.status_code) 
      print(response) 
      return '' 

client = BitfClient() 
print(client.active_orders()) 

答えて

0

より多くの研究を行った後、私は答えに出くわしましたし、それが自分自身の目的のためのAPIドキュメントのコードを適応させるのに苦労して他の誰を助けることができる場合には、ここで解決策を掲載します。

KEY = os.environ[BFX_KEY] 
SECRET = os.environ[BFX_SECRET] 

これらの行は、スクリプトには不要で、シェルやコマンドラインからアカウントにアクセスするための行です。 Python 3.xの場合、_headers関数を次のように変更すると、クライアントが動作します。 (this投稿を参照)

h = hmac.new(str(self.SECRET).encode('utf-8'), signature.encode('utf-8'), hashlib.sha384) 
関連する問題