GDAX/Coinbase API身份验证过程:必须在散列之前对Unicode对象进行编码

Dyl*_*ite 5 python api unicode coinbase-api gdax-api

我有很多编码经验,但Python对我来说是新的领域.

我正在使用CoinbaseExchangeAuth类来访问GDAX API的专用端点.我写了一些简单的代码......

api_url = 'https://public.sandbox.gdax.com/'
auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS)
Run Code Online (Sandbox Code Playgroud)

(注意我已经准确地定义了api密钥,秘密并在这些代码行之前正确传递 - 对于沙箱)

然后我写道:

r = requests.get(api_url + 'accounts', auth=auth)
Run Code Online (Sandbox Code Playgroud)

运行代码并得到此错误:

文件"a:\ PythonCryptoBot\Bot1.0\CoinbaseExhangeAuth.py",第16行,在call signature = hmac.new(hmackey,message,hashlib.sha256)文件"C:\ Users\Dylan\AppData\Local\Programs\Python\Python35-32\lib\hmac.py",第144行,在新的返回HMAC(key,msg,digestmod)文件"C:\ Users\Dylan\AppData\Local\Programs\Python\Python35-32\lib\hmac.py",第84行,在__init_ self.update(msg)文件"C:\ Users\Dylan\AppData\Local\Programs\Python\Python35-32\lib\hmac.py",第93行,更新自我.inner.update(msg)TypeError:必须在散列之前对Unicode对象进行编码

另请注意,我尝试过API_KEY.encode('utf-8')和其他人一样. - 似乎没有做任何事情.

t.m*_*dam 8

您正在使用的代码是为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()
Run Code Online (Sandbox Code Playgroud)

修改后的代码

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())
Run Code Online (Sandbox Code Playgroud)

请注意,我只是"翻译"了原始代码,我不能保证它的功能或安全性.