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

发布于 2025-01-14 23:56:25 字数 1782 浏览 0 评论 0原文

您好,我正在尝试处理 Binance 抛出的异常并从函数输出错误,以便我可以让用户知道 API 详细信息不起作用

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

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

'Spot' object has no attribute 'ClientError'

我不清楚如何处理币安抛出的 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=='})

Hi I'm trying to handle an exception thrown by Binance and output from the function the error so I can let the user know the API details didn't work

Source: https://github.com/binance/binance-connector-python

I get this error already on the line for try:

'Spot' object has no attribute 'ClientError'

Unclear for me how to handle the ClientError thrown by Binance. I tried other combinations like adding binance.error.ClientError

Code below

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"))

How the Error thrown by Binance looks like

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=='})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2025-01-21 23:56:25

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

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

Reading the binance documentation it shows the proper way to reference the ClientError is binance.error.ClientError. You said in your question that you tried this, but in the provided code you only imported binance.spot - that isnt going to include importing ClientError.

Try importing the entire binance package with import binance and then try using binance.error.ClientError again. This should work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文