首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GDAX / Coinbase API身份验证过程:在散列之前必须对Unicode对象进行编码。

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

Stack Overflow用户
提问于 2017-12-15 02:02:02
回答 2查看 2.6K关注 0票数 7

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

我使用CoinbaseExchangeAuth类访问GDAX的私有端点。我写了一些简单的代码。

代码语言:javascript
运行
复制
api_url = 'https://public.sandbox.gdax.com/'
auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS)

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

然后我写道:

代码语言:javascript
运行
复制
r = requests.get(api_url + 'accounts', auth=auth)

运行代码并获取以下错误:

文件"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",第144行中,在新的返回HMAC(key,msg,digestmod)文件"C:\Users\Dylan\AppData\Local\Programs\Python\Python35-32\lib\hmac.py",行84,在"C:\Users\Dylan\AppData\Local\Programs\Python\Python35-32\lib\hmac.py",__init_ self.update(msg)文件第93行中,在update self.inner.update(msg) TypeError: Unicode对象必须在散列之前进行编码。

还请注意,我尝试过API_KEY.encode('utf-8'),并与其他人一样。-似乎什么也没做

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-15 02:57:42

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

原始代码:

代码语言:javascript
运行
复制
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()

修改后的代码:

代码语言:javascript
运行
复制
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())

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

票数 20
EN

Stack Overflow用户

发布于 2020-09-21 06:25:17

你发布的修改代码对我来说不太管用,但这确实有效!

代码语言:javascript
运行
复制
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 API
class CoinbaseWalletAuth(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 or b'').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())
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47824793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档