2017-12-15 9 views
4

私は経験豊富なコーディングがありますが、Pythonは私の新しい領域です。GDAX/Coinbase API認証プロセス:ハッシュ化する前にUnicodeオブジェクトをエンコードする必要があります

私はCoinbaseExchangeAuthクラスを使用して、GDAX APIのプライベートエンドポイントにアクセスしています。

その後、私は書く - 私はいくつかの簡単なコード...

api_url = 'https://public.sandbox.gdax.com/' 
auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS) 

を(サンドボックスのために、私は正確APIキーが定義されていることに注意して秘密と次の行のコードの前に正しく渡す)書きます

r = requests.get(api_url + 'accounts', auth=auth) 

は、コードを実行し、このエラーを取得:

File "a:\PythonCryptoBot\Bot1.0\CoinbaseExhangeAuth.py", line 16, in call signature = hmac.new(hmackey, message, hashlib.sha256) File "C:\Users\Dylan\AppData\Local\Programs\Python\Python35-32\lib\hmac.py", line 144, in new return HMAC(key, msg, digestmod) File "C:\Users\Dylan\AppData\Local\Programs\Python\Python35-32\lib\hmac.py", line 84, in __init_ self.update(msg) File "C:\Users\Dylan\AppData\Local\Programs\Python\Python35-32\lib\hmac.py", line 93, in update self.inner.update(msg) TypeError: Unicode-objects must be encoded before hashing

はまた、私はAPにしようとしていることに注意してくださいI_KEY.encode( 'utf-8')と他のものと同じです。 - 何もしていないようです。

答えて

3

使用しているコードはPython2用に書かれていますが、そのまま実行することはできません。 Python3互換にするためにいくつかの部分を修正しました。

オリジナルコード:

import json, hmac, hashlib, time, requests, base64 
from requests.auth import AuthBase 

# Create custom authentication for Exchange 
class CoinbaseExchangeAuth(AuthBase): 
    def __init__(self, api_key, secret_key, passphrase): 
     self.api_key = api_key 
     self.secret_key = secret_key 
     self.passphrase = passphrase 

    def __call__(self, request): 
     timestamp = str(time.time()) 
     message = timestamp + request.method + request.path_url + (request.body or '') 
     hmac_key = base64.b64decode(self.secret_key) 
     signature = hmac.new(hmac_key, message, hashlib.sha256) 
     signature_b64 = signature.digest().encode('base64').rstrip('\n') 

     request.headers.update({ 
      'CB-ACCESS-SIGN': signature_b64, 
      'CB-ACCESS-TIMESTAMP': timestamp, 
      'CB-ACCESS-KEY': self.api_key, 
      'CB-ACCESS-PASSPHRASE': self.passphrase, 
      'Content-Type': 'application/json' 
     }) 
     return request 

api_url = 'https://api.gdax.com/' 
auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS) 

# Get accounts 
r = requests.get(api_url + 'accounts', auth=auth) 
print r.json() 
# [{"id": "a1b2c3d4", "balance":... 

# Place an order 
order = { 
    'size': 1.0, 
    'price': 1.0, 
    'side': 'buy', 
    'product_id': 'BTC-USD', 
} 
r = requests.post(api_url + 'orders', json=order, auth=auth) 
print r.json() 

修正コード:私は元のコードを "翻訳" したこと

import json, hmac, hashlib, time, requests, base64 
from requests.auth import AuthBase 

# Create custom authentication for Exchange 
class CoinbaseExchangeAuth(AuthBase): 
    def __init__(self, api_key, secret_key, passphrase): 
     self.api_key = api_key 
     self.secret_key = secret_key 
     self.passphrase = passphrase 

    def __call__(self, request): 
     timestamp = str(time.time()) 
     message = timestamp + request.method + request.path_url + (request.body or b'').decode() 
     hmac_key = base64.b64decode(self.secret_key) 
     signature = hmac.new(hmac_key, message.encode(), hashlib.sha256) 
     signature_b64 = base64.b64encode(signature.digest()).decode() 

     request.headers.update({ 
      'CB-ACCESS-SIGN': signature_b64, 
      'CB-ACCESS-TIMESTAMP': timestamp, 
      'CB-ACCESS-KEY': self.api_key, 
      'CB-ACCESS-PASSPHRASE': self.passphrase, 
      'Content-Type': 'application/json' 
     }) 
     return request 

api_url = 'https://api.gdax.com/' 
auth = CoinbaseExchangeAuth(APIKEY, API_SECRET, API_PASS) 

# Get accounts 
r = requests.get(api_url + 'accounts', auth=auth) 
print(r.json()) 
# [{"id": "a1b2c3d4", "balance":... 

# Place an order 
order = { 
    'size': 1.0, 
    'price': 1.0, 
    'side': 'buy', 
    'product_id': 'BTC-USD', 
} 
r = requests.post(api_url + 'orders', json=order, auth=auth) 
print(r.json()) 

注それはだために、私は保証することはできません機能性またはセキュリティ。

+0

これは間違いなく今働いています、ありがとうございます。しかし、今私はこのエラーが発生します。 トレースバック(最新の最後の呼び出し): ファイル "a:\ PythonCryptoBot \ Bot1.0 \ CoinbaseExhangeAuth.py"、行88、 print(r.json()) –

+0

これは '.json ) 'は応答を解析できませんでした。あなたは 'r.text'と' r.status_code'を印刷できますか? –

+0

print(r.status_code)200を教えてください(それは動作します) –

関連する問題