发生异常:对象没有属性 'ClientError'

Exception has occurred: Object has no attribute 'ClientError'

您好,我正在尝试处理 Binance 抛出的异常并从函数输出错误,这样我就可以让用户知道 API 详细信息无效

来源:https://github.com/binance/binance-connector-python

我在尝试时已经收到此错误:

'Spot' object has no attribute 'ClientError'

我不清楚如何处理 Binance 抛出的 ClientError。我尝试了其他组合,例如添加 binance.error.ClientError

下面的代码

import requests
import json
from binance.spot import Spot


def verify_api_key(api_key, api_secret):

   # api key/secret are required for user data endpoints
   client = Spot(key=api_key, secret=api_secret)

   # Get account and balance information

   try:
    return client.api_key_permissions()


   except client.ClientError as e:
    raise e



print(verify_api_key("test","test"))

Binance 抛出的错误是怎样的

raise ClientError(status_code, err["code"], err["msg"], response.headers)
binance.error.ClientError: (400, -2008, 'Invalid Api-Key ID.', {'Content-Type': 'application/json;charset=UTF-8', 'Content-Length': '42', 'Connection': 'keep-alive', 'Date': 'Wed, 16 Mar 2022 20:23:04 GMT', 'Server': 'nginx', 'Strict-Transport-Security': 'max-age=31536000; includeSubdomains', 'X-Frame-Options': 'SAMEORIGIN', 'X-Xss-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'Content-Security-Policy': "default-src 'self'", 'X-Content-Security-Policy': "default-src 'self'", 'X-WebKit-CSP': "default-src 'self'", 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'X-Cache': 'Error from cloudfront', 'Via': '1.1 3ddbbcaacc1ba68ddfab04ef45c3ca98.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'MUC50-P1', 'X-Amz-Cf-Id': 'hBKLvd1lmcWNrI97jwGEnT2PH0jOXlvkMOkdfftTkAhqEkFEx8Xdaw=='})

阅读 binance 文档显示引用 ClientError 的正确方法是 binance.error.ClientError。你在你的问题中说你尝试过这个,但在提供的代码中你只导入了 binance.spot - 这不会包括导入 ClientError.

尝试使用 import binance 导入整个 binance 包,然后再次尝试使用 binance.error.ClientError。这应该适合你。