python coinbase pro沙箱认证请求错误

python coinbase pro sandbox authentication request error

尝试使用来自 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 行,在 jprint(r.json())

文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/models.py”,第 897 行,在 json return complexjson.loads(self.text, **kwargs)

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

文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py”,第 355 行,在 raw_decode 从 None 提高 JSONDecodeError("Expecting value", s, err.value) json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 0)

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

要解决这个问题,我必须更改这一行

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

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

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