调用Binance期货API返回-2015错误:Invalid API-key, IP, or permissions for action的排查求助
调用Binance期货API返回-2015错误:Invalid API-key, IP, or permissions for action的排查求助
我现在碰到了Binance期货API的权限问题,返回错误{'code': -2015, 'msg': 'Invalid API-key, IP, or permissions for action, request ip: ...'},想请大家帮忙排查下问题所在。
我是按照Binance官方的签名示例代码来实现的,已经确认过几个关键点:
- API密钥和密钥是正确复制的,没有输入错误
- 当前请求的IP地址已经正确添加到API密钥的白名单中
- 这个API密钥是在我开通期货账户之后才创建的
以下是我的完整代码:
import hashlib import hmac import time from urllib.parse import urlencode import requests import cred KEY = cred.KEY SECRET = cred.SECRET BASE_URL = "https://fapi.binance.com" # production base url # BASE_URL = "https://testnet.binancefuture.com" # testnet base url def hashing(query_string): return hmac.new( SECRET.encode("utf-8"), query_string.encode("utf-8"), hashlib.sha256 ).hexdigest() def get_timestamp(): return int(time.time() * 1000) def dispatch_request(http_method): session = requests.Session() session.headers.update( {"Content-Type": "application/json;charset=utf-8", "X-MBX-APIKEY": KEY} ) return { "GET": session.get, "DELETE": session.delete, "PUT": session.put, "POST": session.post, }.get(http_method, "GET") # used for sending request requires the signature def send_signed_request(http_method, url_path, payload={}): query_string = urlencode(payload) # replace single quote to double quote query_string = query_string.replace("%27", "%22") if query_string: query_string = "{}×tamp={}".format(query_string, get_timestamp()) else: query_string = "timestamp={}".format(get_timestamp()) url = ( BASE_URL + url_path + "?" + query_string + "&signature=" + hashing(query_string) ) print("{} {}".format(http_method, url)) params = {"url": url, "params": {}} response = dispatch_request(http_method)(**params) return response.json() # get account informtion # if you can see the account details, then the API key/secret is correct response = send_signed_request("GET", "/fapi/v2/account") print(response)
我实在找不出哪里出问题了,有没有朋友能帮我分析下可能的原因?
备注:内容来源于stack exchange,提问作者advortsov
相关产品推荐
相关产品推荐

