简体   繁体   English

Python Coinbase Pro API Class Function 参数不工作

[英]Python Coinbase Pro API Class Function Parameters Not Working

Based on the Coinbase Pro API Documentation I got their authentication class working and have generally been able to make GET calls.基于 Coinbase Pro API 文档,我得到了他们的身份验证 class 工作并且通常能够进行 GET 调用。 However, I'm trying to write a second class that would authenticate and then make the API calls based on the URL changes (ie, GET 24hr stats for a product ID).但是,我正在尝试编写第二个 class 来进行身份验证,然后根据 URL 更改进行 API 调用(即获取产品 ID 的 24 小时统计信息)。

When I run the code below, I'm receiving a TypeError missing the positional argument for product_id even though it's defined in the code.当我运行下面的代码时,我收到一个缺少 product_id 位置参数的 TypeError,即使它是在代码中定义的。 What would I have to change in the main code or for the CoinbaseManager to get the call to work correctly?我必须在主要代码中或 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())

it should be它应该是

    price = CoinbaseManager().get_24hr_stats(CoinbaseManager._auth, crypto_id)

you are failing to create a CoinbaseManager object.您无法创建 CoinbaseManager object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM