Reputation: 21
I am working on 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.
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
Upvotes: 0
Views: 449
Reputation: 2116
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
Upvotes: 0
Reputation: 55913
In your code, you are decoding your secret key from base64:
hmac_key = base64.b64decode(self.secret_key)
However your secret key is not base64-encoded, so decoding fails.
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.
Upvotes: 1