对于 Binance US API,此请求的签名无效错误
我在连接到需要身份验证的 Binance API 端点时收到签名无效错误。以下链接中有一个类似的查询,但我猜它是特定于币安的,而这个问题是特定于币安美国的。我尝试使用下面链接中的方法,但没有成功。
以下是 Python 代码
import urllib.parse
import hashlib
import hmac
import base64
import requests
api_url = "https://api.binance.us"
# get binanceus signature
def get_binanceus_signature(data, secret):
postdata = urllib.parse.urlencode(data)
message = postdata.encode()
byte_key = bytes(secret, 'UTF-8')
mac = hmac.new(byte_key, message, hashlib.sha256).hexdigest()
return mac
# Attaches auth headers and returns results of a POST request
def binanceus_request(uri_path, data, api_key, api_sec):
headers = {}
headers['X-MBX-APIKEY'] = api_key
signature = get_binanceus_signature(data, api_sec)
params={**data, "signature": signature}
req = requests.get((api_url + uri_path), params=params, headers=headers)
return req.text
api_key = "vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A"
secret_key = "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"
uri_path = "/api/v3/openOrders"
data = {
"symbol": "BTCUSDT",
"timestamp": 1499827319559
}
get_open_order_result = binanceus_request(uri_path, data, api_key, secret_key)
这是完整的 dart 代码。
class BinanceUSRestClient {
final String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
Future<http.Response> getResponse({
required String secret,
required String apiKey,
required String path,
Map<String, dynamic>? queryParams,
}) async {
//Header
Map<String, String> headers = {};
headers['X-MBX-APIKEY'] = apiKey;
//Params
Map<String, dynamic> params = {};
if (queryParams != null) {
params.addAll(queryParams);
}
params['signature'] = createSignature(secret, queryParams);
params['timestamp'] = timestamp;
final Uri uri = Uri.https('api.binance.us', path, params);
http.Response response = await http.get(
uri,
headers: headers,
);
return response;
}
String createSignature(String secret, Map<String, dynamic>? data) {
final String jsonString = jsonEncode(data);
final List<int> message = utf8.encode(jsonString);
final List<int> key = utf8.encode(secret);
final List<int> mac = Hmac(sha256, key).convert(message).bytes;
final String signature = hex.encode(mac);
return signature;
}
}
如果有帮助,请参阅API 文档。有人可以帮助我解决该错误吗?
I am getting a signature not valid error while connecting to Binance API endpoints that require authentication. There is a similar query in following link however it is specific to Binance, I guess and this question is specific to Binance US. I tried to use the methods in below link though and it didn't work.
Following is the Python code
import urllib.parse
import hashlib
import hmac
import base64
import requests
api_url = "https://api.binance.us"
# get binanceus signature
def get_binanceus_signature(data, secret):
postdata = urllib.parse.urlencode(data)
message = postdata.encode()
byte_key = bytes(secret, 'UTF-8')
mac = hmac.new(byte_key, message, hashlib.sha256).hexdigest()
return mac
# Attaches auth headers and returns results of a POST request
def binanceus_request(uri_path, data, api_key, api_sec):
headers = {}
headers['X-MBX-APIKEY'] = api_key
signature = get_binanceus_signature(data, api_sec)
params={**data, "signature": signature}
req = requests.get((api_url + uri_path), params=params, headers=headers)
return req.text
api_key = "vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A"
secret_key = "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j"
uri_path = "/api/v3/openOrders"
data = {
"symbol": "BTCUSDT",
"timestamp": 1499827319559
}
get_open_order_result = binanceus_request(uri_path, data, api_key, secret_key)
Here is the full dart code.
class BinanceUSRestClient {
final String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
Future<http.Response> getResponse({
required String secret,
required String apiKey,
required String path,
Map<String, dynamic>? queryParams,
}) async {
//Header
Map<String, String> headers = {};
headers['X-MBX-APIKEY'] = apiKey;
//Params
Map<String, dynamic> params = {};
if (queryParams != null) {
params.addAll(queryParams);
}
params['signature'] = createSignature(secret, queryParams);
params['timestamp'] = timestamp;
final Uri uri = Uri.https('api.binance.us', path, params);
http.Response response = await http.get(
uri,
headers: headers,
);
return response;
}
String createSignature(String secret, Map<String, dynamic>? data) {
final String jsonString = jsonEncode(data);
final List<int> message = utf8.encode(jsonString);
final List<int> key = utf8.encode(secret);
final List<int> mac = Hmac(sha256, key).convert(message).bytes;
final String signature = hex.encode(mac);
return signature;
}
}
If it helps, here is the API documentation. Could someone help me in resolving the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后!!花了几个小时后,shammy12 的帖子帮助我弄清楚了什么出了问题。一直以来,我一直在尝试基于 json 格式的参数创建签名。尽管币安美国文档明确指出,
在我引用的大多数响应中都忽略了这一点。即使
recvWindow
参数与无效签名错误无关。我调试这个问题所需要做的就是找出一种将查询格式化为的方法,并且这是通过使用以下内容来实现的:
这是完整的调试代码。如果有任何事情请随时告诉我。我已经重命名了一些变量以提高可读性并避免一些可能的混淆。
Finally!! After spending hours, shammy12's post helped me to figure out what went wrong. All this time I have been trying to create a signature based on a parameter in json format. Although Binance US documentation clearly stated,
this was overlooked in most of the responses I referenced. Even
recvWindow
parameter has nothing to do with the invalid signature error. All it took me to debug this issue was figure out a way to format the query as,And, this is achieved by using following:
Here is the full debugged code. Please feel free to let me know if there is anything. I have renamed some variables for better readability and avoid some possible confusions.
在 Dart (Flutter) 中,我如何使用
binance 的 crypto: ^3.0.2 和 http: ^0.13.5 包 Api 类让它工作
Another way in Dart (Flutter) how i got it to work using crypto: ^3.0.2 and http: ^0.13.5 package
Api class of binance