GDAX / Coinbase API Authentication Process: Unicode Objects Must Be Encoded Before Hashing

I have a lot of coding experience, but Python is a new territory for me.

I am using the CoinbaseExchangeAuth class to access the private endpoints of the GDAX API. I am writing simple code ...

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

(note that I precisely determined the api key, the secret and passed it correctly before these lines of code - for the sandbox)

Then I write:

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

Run the code and get this error:

File "a: \ PythonCryptoBot \ Bot1.0 \ CoinbaseExhangeAuth.py", line 16, in the call signature = hmac.new (hmackey, message, hashlib.sha256) File "C: \ Users \ Dylan \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ hmac.py ", line 144, in a new HMAC return (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

Also note that I tried using API_KEY.encode ('utf-8') and the same with others. - It seems to do nothing.

+4
source share
1

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())

, "" , .

+4

Source: https://habr.com/ru/post/1690823/


All Articles