使用Robinhood nummus API放置加密订单

发布时间:2020-07-06 19:53

我正在尝试扩展this repo以支持使用Python进行的加密货币交易(完成后将创建PR)。

除了实际进行交易外,我拥有所有API方法。

下达加密订单的端点为https://nummus.robinhood.com/orders/

此终结点希望以JSON格式的主体以及以下标头发出POST请求:

"Accept": "application/json",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5",
"Content-Type": "application/json",
"X-Robinhood-API-Version": "1.0.0",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
"Origin": "https://robinhood.com",
"Authorization": "Bearer <access_token>"

我发送的有效负载如下:

{   
    'account_id': <account id>,
    'currency_pair_id': '3d961844-d360-45fc-989b-f6fca761d511', // this is BTC
    'price': <BTC price derived using quotes API>,
    'quantity': <BTC quantity>,
    'ref_id': str(uuid.uuid4()), // Im not sure why this is needed but I saw someone else use [the uuid library][2] to derive this value like this
    'side': 'buy',
    'time_in_force': 'gtc',
    'type': 'market'
}

我得到的答复如下: 400 Client Error: Bad Request for url: https://nummus.robinhood.com/orders/

我可以确认自己能够成功进行身份验证,因为我可以使用https://nummus.robinhood.com/accounts/https://nummus.robinhood.com/holdings/端点来查看我的帐户数据和持股。

我也相信Authentication标头中的access_token是正确的,因为如果我将其设置为某个随机值(例如,Bearer abc123),则会收到401 Client Error: Unauthorized响应。

我认为问题与有效负载有关,但是我找不到nummus.robinhood.com API的良好文档。

有人看到我的请求有效载荷格式如何/是否错误,和/或可以向我指出nummus.robinhood.com/orders端点文档的正确方向?

回答1

您需要在请求后调用中将json有效内容作为值传递给参数json

import requests

json_payload = {   
    'account_id': <account id>,
    'currency_pair_id': '3d961844-d360-45fc-989b-f6fca761d511', // this is BTC
    'price': <BTC price derived using quotes API>,
    'quantity': <BTC quantity>,
    'ref_id': str(uuid.uuid4()), // Im not sure why this is needed but I saw someone else use [the uuid library][2] to derive this value like this
    'side': 'buy',
    'time_in_force': 'gtc',
    'type': 'market'
}

headers = {
    "Accept": "application/json",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5",
    "Content-Type": "application/json",
    "X-Robinhood-API-Version": "1.0.0",
    "Connection": "keep-alive",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
    "Origin": "https://robinhood.com",
    "Authorization": "Bearer <access_token>"
}

url = "https://nummus.robinhood.com/orders/"

s = requests.Session()
res = s.request("post", url, json=json_payload, timeout=10, headers=headers)
print(res.status_code)
print(res.text)