这是我正在使用的代码,它在 google 云功能上运行并使用 ccxt 库
import ccxt
import json
def get_binance_balance(request):
try:
api_key = 'MY_API_KEY'
secret_key = 'MY_SECRET_KEY'
# Set up Binance client using ccxt
binance = ccxt.binance({
'apiKey': api_key,
'secret': secret_key
})
# Fetch the account balance
balance = binance.fetch_balance()
# Create a dictionary to store the account balance information
account_balance = {}
# Store the account balance in the dictionary
for asset, info in balance['total'].items():
if info > 0:
account_balance[asset] = info
# Return the account balance as a JSON response
return json.dumps(account_balance)
except Exception as e:
error_message = str(e)
return json.dumps({'error': error_message})
except ccxt.BaseError as e:
# Handle specific ccxt errors
print("ccxt error:", e)
except requests.RequestException as e:
# Handle specific requests library errors
print("requests error:", e)
except Exception as e:
# Handle other general exceptions
print("Error:", e)
但是,每次我尝试执行代码时,它都会给出以下链接:
https://api.binance.com/sapi/v1/capital/config/getall
这导致了这个错误:
{"code":-2014,"msg":"API-key format invalid."}
就我在网上看到的情况来看,这似乎是标头问题,但是ccxt应该照顾所有这些,事实上,如果我用ccxt.coinbasepro替换ccxt.binance,这个问题就不会发生。
是否是因为Google Cloud IP无法访问Binance API? (我的函数在苏黎世的服务器上运行)
我的函数也使用VPC,否则币安也会给出此错误:
根据“b.”,在受限位置无法提供服务。 https://www.binance.com/en/terms中的“资格”。如果您认为您收到此消息有误,请联系客服`
如果你能找到这个问题的解决方案,我将非常感激,我已经尝试修复它几个小时了,不是开玩笑。