如何使用python请求将经过身份验证的POST请求编码到API?

问题描述 投票:0回答:2

我对Python编码和一般编码很新,我试图对Binance API进行经过身份验证的购买功能。

从我收集的内容来看,这通常是在Linux上使用curl方法完成的,我尝试在Python库中使用requests,并且我不断从API中获取错误400代码,所以显然我正在做某事这里非常错误。

我会使用python-binance包装器来获取灵感,但遗憾的是我几乎无法掌握该代码的功能。

谁能解释我在这里做错了什么?

以下是binance API文档的链接: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md

def makeBuy(side, symbol, quantity, price):
    timestamp = int(round(time.time() * 1000))
    queryS = "symbol=" + symbol + "&side=" + side + "&type=LIMIT&timeInForce=GTC&quantity=" + \
         str(quantity) + "&price=" + str(price) + "&timestamp=" + str(timestamp)
    m = hmac.new(api_secret.encode('utf-8'), queryS.encode('utf-8'), hashlib.sha256)
    url = 'https://api.binance.com/api/v3/order?' + queryS + '&signature=' + str(m.hexdigest())

    response2 = requests.get(url, headers={'X-MBX-APIKEY': api_key})
    print(response2)
python python-3.x api python-requests
2个回答
0
投票

我建议你通过网络浏览器访问api并使用浏览器的网络高级选项(最好是chrome)来查看get请求的确切cookie和结构,并尝试使用python实现相同的功能。我上周遇到了类似的问题,这就是我解决它的方法。


0
投票

我已经弄清楚如何通过反复试验来做到这一点:

     def makeBuy(side, symbol, quantity, price):
        timestamp = int(round(time.time() * 1000))
        queryS = "symbol=" + symbol + "&side=" + side + "&type=LIMIT&timeInForce=GTC&quantity=" + \
    str(quantity) + "&price=" + str(price) + "&timestamp=" + str(timestamp)
        m = hmac.new(api_secret.encode('utf-8'), queryS.encode('utf-8'), hashlib.sha256)
        header = {'X-MBX-APIKEY' : api_key}
        url = 'https://api.binance.com/api/v3/order' + '?' + queryS + '&signature=' + m.hexdigest()
        response2 = requests.post(url, headers=header, timeout=30, verify=True)
        print(response2)
© www.soinside.com 2019 - 2024. All rights reserved.