Python Coinbase Pro API类函数参数不起作用

2024-05-13 20:12:59 发布

您现在位置:Python中文网/ 问答频道 /正文

基于Coinbase Pro API文档,我得到了他们的身份验证类,并且通常能够进行GET调用。但是,我正在尝试编写第二个类,该类将进行身份验证,然后根据URL更改进行API调用(即获取产品ID的24小时统计数据)

当我运行下面的代码时,我收到一个TypeError,它缺少product_id的位置参数,即使它是在代码中定义的。要使调用正常工作,我需要在主代码或CoinbaseManager中更改什么

import json, hmac, hashlib, time, requests, base64, os
from requests.auth import AuthBase

# 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

class CoinbaseManager:

    _apiUrl = 'https://api.pro.coinbase.com/'
    _auth = CoinbaseExchangeAuth(os.getenv('apiKey'), os.getenv('secretKey'),  os.getenv('passphrase'))

    def __init__(self):
        self.data = []

    def get_24hr_stats(self, auth, product_id):
        '''
        get_24hr_stats() -- Get 24 hr stats for the product. volume is in base currency units. 
                            open, high, low are in quote currency units.
        '''

        extension = 'products/{}/stats'.format(product_id)

        return requests.get(self._apiUrl + extension, auth=self._auth)

if __name__ == '__main__':
    crypto_id = 'BTC-USD'
    price = CoinbaseManager.get_24hr_stats(CoinbaseManager._auth, crypto_id)
    print(price.json())

Tags: keyselfauthapiidsecretosrequest