Gemini Sandbox API上的Python3端点不匹配

时间:2017-12-25 03:58:31

标签: python-3.x hmac algorithmic-trading hashlib

我正在使用Python3构建一个用于货币交易的算法交易应用程序。我试图在Python3中调用Gemini Exchange Sandbox API来获取当前余额。每次我发送我的帖子请求时,我都会收到如下错误:

{
 "result":"error",
 "reason":"EndpointMisatch",
 "message":"EndpointMisatch"
}

为此,我将我的端点更改为“https://api.gemini.com/v1/balances”的生产网址,这会导致InvalidSignature错误。

我已经从沙盒中删除了我的API,并创建了一个新的API,确保我有基金经理和交易员访问无论是否有心跳都无济于事。文档在这里:https://docs.sandbox.gemini.com/rest-api/?python#error-codes

这是我的功能:     导入请求     导入json     进口时间     import base64     导入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库。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

经过多次测试,我发现了问题。我的有效负载数据字典的端点列为“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'
}]