使用cbpro调用get_fills返回'message'字符串无实际内容的问题
AttributeError When Using cbpro's get_fills Method I ran into a similar quirk with the cbpro library not long ago, so I know exactly where you're stuck. Let's break down what's happening and how to fix it.
What's Causing the Error?
The root issue is that for certain products, the Coinbase API returns an error response—but the cbpro library isn’t parsing that response into a proper dictionary (like the official docs say it should). Instead, it passes through the raw JSON string (which looks like the plain string "message" when you print it directly), leading to that AttributeError when you try to call .items() on it.
Common triggers for this API error include:
- You’ve never traded that product before (the API throws an error instead of returning an empty list)
- The product has been delisted from Coinbase
- Your API key lacks permissions to access data for that product
- Temporary API rate limiting or service issues
How to See the Actual Error Message
To fix this and view the real error content, add a check for string responses and manually parse them into JSON. Here's your modified code:
import json all_fills = {} for product in products: all_fills[product] = [] fills = a.get_fills(product) for fill in fills: # Handle error responses that come as raw strings if isinstance(fill, str): try: # Convert the string to a proper JSON object error_dict = json.loads(fill) print(f"Error fetching fills for {product}: {error_dict.get('message', 'Unknown error')}") except json.JSONDecodeError: # Fallback if the string isn't valid JSON print(f"Unexpected response for {product}: {fill}") # Skip to the next entry since this one is an error continue # Process valid fill objects as originally intended for x, y in fill.items(): print(f"\t{x}: {y}") print(fill) all_fills[product].append(fill) print(f"{product} captured.")
Additional Checks to Try
- Validate product IDs: Double-check that the pairs in your
productslist are still active on Coinbase—some older pairs get delisted over time. - Confirm API permissions: Ensure your API key has the
viewpermission enabled (required to fetch fill history). - Update cbpro: Older versions of the library have buggy error handling. Run
pip install --upgrade cbproto grab the latest release.
内容的提问来源于stack exchange,提问作者dv151

