【发布时间】:2017-12-25 03:58:31
【问题描述】:
我正在用 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
这是我的功能: 导入请求 导入json 进口时间 导入base64 导入 hmac 导入哈希库
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 库的新手。提前感谢您的帮助。
【问题讨论】:
标签: python-3.x hmac algorithmic-trading hashlib