Skip to content Skip to sidebar Skip to footer

Gdax / Coinbase Api Authentication Process: Unicode-objects Must Be Encoded Before Hashing

I have a lot of experience coding, but Python is new territory for me. I'm using the CoinbaseExchangeAuth class to access the private endpoints of the GDAX API. I write some simple

Solution 1:

The code you're using is written for Python2, you can't expect it to run as it is. I've modified some parts to make it Python3 compatible.

Original code:

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

# Create custom authentication for ExchangeclassCoinbaseExchangeAuth(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()

Modified code:

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

# Create custom authentication for ExchangeclassCoinbaseExchangeAuth(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 orb'').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())

Note that i've only "translated" the original code, i can't guarantee for it's functionality or security.

Solution 2:

The modified code you posted didn't quite work for me, but this did!

import hmac, hashlib, time, requests, os
from requests.auth import AuthBase


API_KEY = os.environ.get('API_KEY')
API_SECRET = os.environ.get('API_SECRET')


# Create custom authentication for Coinbase APIclassCoinbaseWalletAuth(AuthBase):
    def__init__(self, api_key, secret_key):
        self.api_key = api_key
        self.secret_key = secret_key

    def__call__(self, request):
        timestamp = str(int(time.time()))
        message = timestamp + request.method + request.path_url + (request.body orb'').decode()
        signature = hmac.new(bytes(self.secret_key,'utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest()

        request.headers.update({
            'CB-ACCESS-SIGN': signature,
            'CB-ACCESS-TIMESTAMP': timestamp,
            'CB-ACCESS-KEY': self.api_key,
            'CB-VERSION': '2019-11-15'
        })
        return request


api_url = 'https://api.coinbase.com/v2/'
auth = CoinbaseWalletAuth(API_KEY, API_SECRET)

# Get current user
r = requests.get(api_url + 'accounts', auth=auth)
print(r.json())

Post a Comment for "Gdax / Coinbase Api Authentication Process: Unicode-objects Must Be Encoded Before Hashing"