我正在用Python3构建一个用于货币交易的算法交易应用程序。我正在尝试调用Python3中的Gemini Exchange Sandbox API来获取当前余额。每次我发送post请求时,都会返回如下错误:
{
"result":"error",
"reason":"EndpointMisatch",
"message":"EndpointMisatch"
}作为响应,我将端点更改为'https://api.gemini.com/v1/balances‘的生产url,这会导致InvalidSignature错误。
我已经从沙箱中删除了我的API,并创建了一个新的API,以确保我在有和没有心跳的情况下都可以访问基金经理和交易员。文档在这里:https://docs.sandbox.gemini.com/rest-api/?python#error-codes
以下是我的函数: import request import json import time import base64 import hmac import hashlib
def checkBalance(self):
'''
function calls private gemini method
to return account balances. Update with
production or sandbox keys/secrets depending on
environment running.
'''
#set increment for unique session
nonce = int(round(time.time()*1000))
#sandbox api endpoint
url = 'https://api.sandbox.gemini.com/v1/balances'
#build the dict payload object
payload = {
'request':'v1/balances',
'nonce': nonce
}
#endcode payload as a json object for hashing
payload = str.encode(json.dumps(payload))
#base64 encode the payload
b64 = base64.b64encode(payload)
#create the signature using sandbox secret and encoded payload in sha384 hash
signature = hmac.new(str.encode(self.s_secret), b64, hashlib.sha384).hexdigest()
#build headers as required for contacting api endpoint
headers = {
'Content-Type':'text/plain',
'X-GEMINI-APIKEY': self.s_key,
'X-GEMINI-PAYLOAD': b64,
'X-GEMINI-SIGNATURE': signature
}
#retrieve data from POST request as response
response = requests.request("POST", url, headers=headers)
#return text of response
return response.text我刚开始使用b64、hmac和hashlib库。提前感谢您的帮助。
发布于 2017-12-26 08:26:48
经过多次测试,我发现了问题所在。我的有效负载数据字典将端点列为“v1/balances”,而不是“/v1/balances”。我的新负载数据如下所示(创建了一个函数来处理nonce):
payload = dict(request = '/v1/balances', nonce = str(self.nonce()))当我使用此信息更新有效负载并发布数据时,我的沙箱响应显示如下:
[{
'type': 'exchange',
'currency': 'BTC',
'amount': '1000',
'available': '1000',
'availableForWithdrawal': '1000'
},
{
'type': 'exchange',
'currency': 'USD',
'amount': '100000.00',
'available': '100000.00',
'availableForWithdrawal': '100000.00'
},
{
'type': 'exchange',
'currency': 'ETH',
'amount': '20000',
'available': '20000',
'availableForWithdrawal': '20000'
}]https://stackoverflow.com/questions/47965593
复制相似问题