python coinbase pro沙箱身份验证请求错误



正在尝试使用coinbase pro api中的示例python来进行身份验证请求。。。去沙箱是行不通的。我得到以下错误。难道没有一个简单的请求示例可以在网络上运行吗?

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

这是我得到的。。。。

200

追踪(最近一次通话(:

文件"/Users/rockie12_us/PythonScriptsDeanO/SandBoxClientTest/test9.py";,第46行,injprint(r.json(((

文件"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site packages/requests/models.py";,第897行,jsonreturn complexjson.loads(self.text,**kwargs(

文件"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/init.py";,线346,负载return _default_decoder.decode文件"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/delector.py";,第337行,解码obj,end=self.raw_decode(s,idx=_w(s,0(.end(((

文件"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/delector.py";,第355行,在raw_decode中从None引发JSONDecodeError("期望值",s,err.value(json.decoder.JSONDecodeError:应为值:第1行第1列(字符0(

[在0.6秒内完成,退出代码为1]

为了解决这个问题,我不得不更改这行

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

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

api中没有提到这需要是二进制字符串,这会有很大帮助。

最新更新