使用Robinhood nummus API下加密订单

2024-06-01 20:40:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python扩展this repo以支持加密货币交易(完成后将创建一个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/端点查看我的帐户数据和持有量

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

我认为问题与有效负载有关,但我无法找到nummus.robinhood.comAPI的良好文档

有人知道我的请求负载是如何/是否格式不正确的和/或可以为我指出nummus.robinhood.com/orders端点文档的正确方向吗


Tags: httpscomapiidjsonapplication格式交易
1条回答
网友
1楼 · 发布于 2024-06-01 20:40:26

您需要将json负载作为值传递给requests 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)

相关问题 更多 >