需要连接到Coinbase Rest api

问题描述 投票:0回答:1

我正在尝试连接到硬币基础 API,但由于某种原因我无法获取 1. 未经授权 2. 无数据 3 此错误消息:

raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not builtin_function_or_method 

这是该函数的代码:

def getMarketTrades(product_id, SECRET_KEY, API_KEY, limit):
    
        api_key = API_KEY
        api_secret = SECRET_KEY
        api_version = '2022-03-23'
        timestamp = str(int(time.time()))
        method = "GET"
        path = f"/v3/brokerage/products/{product_id}/ticker"
        message = timestamp + method + path + ''
        signature = hmac.new(api_secret.encode('utf-8'), message.encode('utf-8'), digestmod=hashlib.sha256).digest().hex()
        conn = http.client.HTTPSConnection("api.coinbase.com")
        payload = ''
        headers = {'CB-ACCESS-KEY': api_key,
                   'CB-ACCESS-SIGN': signature,
                   'CB-ACCESS-TIMESTAMP': timestamp,
                   'Content-Type': 'json',
                   'CB-VERSION': api_version}
        conn.request(method=method, url=f"/api/v3/brokerage/products/{product_id}/ticker?limit={limit}", body=payload,
                     headers = headers)
        res = conn.getresponse()
        data = res.read()
        data = data.decode
        print("Response Data 1:", data)
        data = json.loads(data)
        print("Response Data 2:", data)
        conn.close()
        return  data

我已经尝试了通过论坛查看的所有内容,检查了 api,并且使用了 chatgpt,但没有任何效果。我只需要返回的数据,你能告诉我出了什么问题吗?

python coinbase-api
1个回答
0
投票

你的线路

data = data.decode

data
变量设置为指向
decode()
对象的
data
方法,然后您尝试将该方法作为 JSON 对象加载。我相当确定您打算做的是将解码后的
data
对象加载为 JSON,在这种情况下,您只需要实际调用该
decode
方法。 改变

data = data.decode

data = data.decode()
© www.soinside.com 2019 - 2024. All rights reserved.