I'm trying to make a private API call to Coinsbit using Python, but I'm having some trouble. I have already successfully implemented the public API calls, but I'm struggling with the authentication required for the private endpoints.
我正尝试使用Python对Coinsbit进行私有API调用,但遇到了一些问题。我已经成功地实现了公共API调用,但我正在努力解决私有端点所需的身份验证问题。
Here's what I have so far:
以下是我目前掌握的情况:
import requests
import json
import time
import hashlib
import hmac
import base64
def call_api():
api_key = 'API_KEY'
api_secret = 'SECRET_KEY'
request = '/api/v1/account/order_history' # /api/v1/order/new
base_url = 'https://api.coinsbit.io/'
data = {
'request': request,
'nonce': str(int(time.time())),
}
complete_url = base_url + request
data_json_str = json.dumps(data, separators=(',', ':'), ensure_ascii=False)
payload = base64.b64encode(data_json_str.encode('utf-8')).decode('utf-8')
signature = hmac.new(api_secret.encode('utf-8'), payload.encode('utf-8'), hashlib.sha512).hexdigest()
headers = {
'Content-type': 'application/json',
'X-TXC-APIKEY': api_key,
'X-TXC-PAYLOAD': payload,
'X-TXC-SIGNATURE': signature
}
try:
res = requests.post(complete_url, headers=headers, data=data_json_str)
res.raise_for_status()
except requests.exceptions.RequestException as e:
return {'error': str(e)}
return {'result': res.json()}
print(call_api())
When I execute the code, I'm getting an error. I have double-checked my API key and secret key, and they are correct. I suspect there might be an issue with how I'm generating the signature or setting the headers.
当我执行代码时,我收到一个错误。我已经重新检查了我的API密钥和密钥,它们都是正确的。我怀疑我生成签名或设置标头的方式可能有问题。
I am seeking insights on what I might be doing wrong or how I can troubleshoot this issue. Additionally, are there any specific requirements or headers that I need to set for the Coinsbit private API calls?
我正在寻求关于我可能做错了什么或者我如何解决这个问题的见解。此外,是否需要为Coinsbit私有API调用设置特定的要求或标头?
更多回答
What kind of message do you receive when execute the code?
当执行代码时,您会收到什么样的消息?
When u merge base_url
and request
your complate_url becomes https://api.coinsbit.io//api/v1/account/order_history
with double /
当u合并base_url并请求时,您的Complate_url将变为带有Double/的https://api.coinsbit.io//api/v1/account/order_history
优秀答案推荐
Here is the working one:
以下是一个有效的解决方案:
import base64
import hashlib
import hmac
import json
import time
import requests
def call_api():
api_key = "API_KEY"
api_secret = "API_SECRET"
request = "/api/v1/account/order_history"
base_url = "https://api.coinsbit.io" # "/" removed
data = {
"request": request,
"nonce": str(int(time.time() * 1000)), # * 1000 added
}
complete_url = base_url + request
data_json_str = json.dumps(data, separators=(",", ":"), ensure_ascii=False)
payload = base64.b64encode(data_json_str.encode("utf-8")).decode("utf-8")
signature = hmac.new(
api_secret.encode("utf-8"), payload.encode("utf-8"), hashlib.sha512
).hexdigest()
headers = {
"Content-type": "application/json",
"X-TXC-APIKEY": api_key,
"X-TXC-PAYLOAD": payload,
"X-TXC-SIGNATURE": signature,
}
try:
res = requests.post(complete_url, headers=headers, data=data_json_str)
res.raise_for_status()
except requests.exceptions.RequestException as e:
return {"error": str(e)}
return {"result": res.json()}
print(call_api())
更多回答
我是一名优秀的程序员,十分优秀!