[英]Python base64-encode Error
I am working on GDAX API. 我正在使用GDAX API。 when i try to connect with API with authentications then i am getting the error.i am just not able to successfully connect with API.i want to create a trading bot for GDAX using their 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())
ERROR: 错误:
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
In your code, you are decoding your secret key from base64: 在您的代码中,您正在从base64解码您的密钥:
hmac_key = base64.b64decode(self.secret_key)
However your secret key is not base64-encoded, so decoding fails. 但是,您的密钥不是base64编码的,因此解码失败。
You need to either convert the secret key to bytes and base64-encode in your __init__
method, or remove the decoding step in your __call__
method. 您需要在
__init__
方法中将密钥转换为字节并进行base64编码,或者在__call__
方法中删除解码步骤。
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())
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.