1

Trying to use the example python from the coinbase pro api to do an authenticated request... it doesn't work going to the sandbox. I get the following error. Isn't there a simple request example that works on the web somewhere?

import json, hmac, hashlib, time, requests, base64 from requests.auth import AuthBase #sandbox api keys and url API_KEY='XX...........' API_PASS='XX.........' API_SECRET='XX.............' api_url='https://public.sandbox.pro.coinbase.com/' def jprint(obj): # create a formatted string of the Python JSON object text = json.dumps(obj, sort_keys=True, indent=4) print(text) # 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 b'').decode() hmac_key = base64.b64decode(self.secret_key) signature = hmac.new(hmac_key, message.encode(), hashlib.sha256) signature_b64 = base64.b64encode(signature.digest()).decode() 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.pro.coinbase.com/' #sandbox url api_url='https://public.sandbox.pro.coinbase.com/' auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS) # Get accounts r = requests.get(api_url + 'accounts', auth=auth) print(r.status_code) jprint(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.status_code) jprint(r.json()) 

Here is what I get back....

200

Traceback (most recent call last):

File "/Users/rockie12_us/PythonScriptsDeanO/SandBoxClientTest/test9.py", line 46, in jprint(r.json())

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/models.py", line 897, in json return complexjson.loads(self.text, **kwargs)

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/init.py", line 346, in loads return _default_decoder.decode(s) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

[Finished in 0.6s with exit code 1]

0

1 Answer 1

1

To fix this I had to change this line

API_SECRET='XX.............' 

to

API_SECRET= b'XX.............' 

nothing in the api mentioned this needed to be binary string, that would have helped greatly.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.