【问题标题】:Python base64-encode ErrorPython base64 编码错误
【发布时间】:2019-01-18 09:25:31
【问题描述】:

我正在研究 GDAX API。当我尝试通过身份验证与 API 连接时,我收到错误消息。我只是无法成功连接 API。我想使用他们的 API 为 GDAX 创建一个交易机器人。

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

api_base = 'https://api-public.sandbox.gdax.com'


class GDAXRequestAuth(AuthBase):
 def __init__(self, api_key, secret_key, passphrase):
    self.api_key = api_key
    self.secret_key = secret_key
    self.secret_key += "000"
    print(self.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.encode('utf-8'), hashlib.sha256)
    signature_b64 = base64.b64encode(signature.digest())
    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


auth = GDAXRequestAuth('f9fc5ceab1d0f7652cb72cc1f25c317e',
                   '0000', '7iaxxywyy6e')
order_url = api_base + '/orders'
order_data = {
 'type': 'market',
 'side': 'buy',
 'product_id': 'BTC-USD',
 'size': '0.01'
}
response = requests.post(order_url, data=json.dumps(order_data), auth=auth)
print(response.json())

错误:

File "C:\Users\aarsh\AppData\Local\Programs\Python\Python36-32\lib\base64.py", line 87, in b64decode
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

Process finished with exit code 1

【问题讨论】:

    标签: python base64 gdax-api


    【解决方案1】:
    class GDAXRequestAuth(AuthBase):
        def __init__(self, api_key, secret_key, passphrase):
            self.api_key = api_key
            self.secret_key = secret_key
            self.secret_key += "000"
            self.passphrase = passphrase
    
        def __call__(self, request):
            timestamp = str(time.time())
            message = timestamp + request.method + request.path_url + (request.body or '')
            hmac_key = base64.b64encode(self.secret_key) # your string is not base64 encoded yet
    
            signature = hmac.new(hmac_key, message.encode('utf-8'), hashlib.sha256)
            signature_b64 = base64.b64encode(signature.digest())
            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
    
    auth = GDAXRequestAuth('f9fc5ceab1d0f7652cb72cc1f25c317e',
                    '0000', '7iaxxywyy6e')
    order_url = api_base + '/orders'
    order_data = {
    'type': 'market',
    'side': 'buy',
    'product_id': 'BTC-USD',
    'size': '0.01'
    }
    response = requests.post(order_url, data=json.dumps(order_data), auth=auth)
    print(response.json())
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      在您的代码中,您正在从 base64 解码您的密钥:

      hmac_key = base64.b64decode(self.secret_key)
      

      但是您的密钥不是 base64 编码的,因此解码失败。

      您需要在 __init__ 方法中将密钥转换为字节和 base64 编码,或者在 __call__ 方法中删除解码步骤。

      【讨论】:

        猜你喜欢
        • 2015-01-13
        • 1970-01-01
        • 1970-01-01
        • 2018-07-04
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-04
        相关资源
        最近更新 更多