使用 Robinhood nummus 下加密订单 API

Placing a crypto order with Robinhood nummus API

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

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

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

此端点期望使用 JSON 格式的 body 以及以下 headers:

发出 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/ 端点来查看我的帐户数据和持有量。

我也相信我在身份验证 header 中的 access_token 是正确的,因为如果我将它设置为某个随机值(例如 Bearer abc123,我会得到 401 Client Error: Unauthorized响应。

我认为问题与负载有关,但我无法找到 nummus.robinhood.com API.

的良好文档

有人看到 how/whether 我的请求负载格式不正确 and/or 可以为我指明 nummus.robinhood.com/orders 端点文档的正确方向吗?

您需要将 json 负载作为值传递给请求 post 调用中的参数 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)