Python base64编码错误

时间:2018-08-10 18:13:14

标签: python base64 gdax-api

我正在使用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

2 个答案:

答案 0 :(得分:1)

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

hmac_key = base64.b64decode(self.secret_key)

但是您的密钥没有经过base64编码,因此解码失败。

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

答案 1 :(得分:0)

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

希望这会有所帮助