【问题标题】:Python3 Endpoint Mismatch on Gemini Sandbox APIGemini Sandbox API 上的 Python3 端点不匹配
【发布时间】: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


    【解决方案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'
    }]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 2021-08-30
      • 2013-12-22
      • 2021-06-02
      • 1970-01-01
      • 2021-09-06
      相关资源
      最近更新 更多