GDAX/Coinbase API 认证过程:Unicode 对象必须在散列之前编码 [英] GDAX / Coinbase API authentication process: Unicode-objects must be encoded before hashing

查看:45
本文介绍了GDAX/Coinbase API 认证过程:Unicode 对象必须在散列之前编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I'm using the CoinbaseExchangeAuth class to access the private endpoints of the GDAX API. I write some simple code...

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

(注意,我在这些代码行之前准确定义了 api 密钥、秘密和正确传递 - 用于沙箱)

(note that I have accurately defined the api key, secret and pass correctly before these lines of code - for the sandbox)

然后我写:

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

运行代码并得到这个错误:

Run the code and get this error:

文件a:\PythonCryptoBot\Bot1.0\CoinbaseExhangeAuth.py",第 16 行,调用签名 = hmac.new(hmackey, message, hashlib.sha256) 文件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",第 84 行,在 __init_ self.update(msg) 文件C:\Users\Dylan\AppData\Local\Programs\Python\Python35-32\lib\hmac.py",第 93 行,在更新 self.inner.update(msg) TypeError: Unicode-objects must be encoding before hashing

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

另请注意,我已经尝试过 API_KEY.encode('utf-8') 并且与其他人相同.- 似乎没有做任何事情.

Also note that I have tried to API_KEY.encode('utf-8') and same with others. - doesn't seem to do anything.

推荐答案

您使用的代码是为 Python2 编写的,您不能指望它按原样运行.我修改了一些部分以使其与 Python3 兼容.

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.

原始代码:

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

<小时>

修改代码:


Modified code:

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

<小时>

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


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

这篇关于GDAX/Coinbase API 认证过程:Unicode 对象必须在散列之前编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆