首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用GDAX上的python中的base64对摘要输出进行编码

如何用GDAX上的python中的base64对摘要输出进行编码
EN

Stack Overflow用户
提问于 2017-12-04 10:38:43
回答 0查看 1.1K关注 0票数 2

我正在尝试使用GDAX交换上的api。在他们的网站上,他们给出了这样的代码:

代码语言:javascript
复制
# Requires python-requests. Install with pip:
#
#   pip install requests
#
# or, with easy-install:
#
#   easy_install requests

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


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

他们还说:“记住在将字母数字秘密字符串用作HMAC的密钥之前,首先对其进行base64解码(结果为64字节)。此外,在发送报头之前,应对摘要输出进行base64编码。”

我相信我已经修复了第一部分:

代码语言:javascript
复制
API_SECRET = base64.b64decode(b'{secret}')

然而,我不明白第二部分是什么意思。我得到了错误消息:

代码语言:javascript
复制
TypeError: Unicode-objects must be encoded before hashing
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47625760

复制
相关文章

相似问题

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