gpt4 book ai didi

python - 尝试使用 Python 请求在 Oanda 上下订单。获取 JSON 错误

转载 作者:行者123 更新时间:2023-12-04 14:16:27 26 4
gpt4 key购买 nike

我正在尝试使用 Python 和 Oanda api 下 Fx 订单。

from requests import post

headers = {
"Content-Type": "application/json",
"Authorization": "Bearer <auth code>"
}
data = {
"order": {
"units": "100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}

#Practice Account
r = post(
"https://api-fxpractice.oanda.com/v3/accounts/<acct #>/orders",
headers=headers,
data=data
)
print(r.text)

我收到以下错误:
Invalid JSON, ParseErrorCode: 3, Message: Invalid value.

有谁知道错误是什么意思?

这是他们网站上的示例 CURL 代码:
body=$(cat << EOF
{
"order": {
"units": "100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
EOF
)

curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <AUTHENTICATION TOKEN>" \
-d "$body" \
"https://api-fxtrade.oanda.com/v3/accounts/<ACCOUNT>/orders"

最佳答案

您必须使用 json.dumps 对字典进行编码.我还从值中删除了引号。

这是代码:

from requests import post
import json

headers = {
"Content-Type": "application/json",
"Authorization": "Bearer <auth code>"
}
data = {
"order": {
"units": 10,
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
data = json.dumps(data)
#Practice Account
r = post(
"https://api-fxpractice.oanda.com/v3/accounts/<acct #>/orders",
headers=headers,
data=data
)
print(r.text)

关于python - 尝试使用 Python 请求在 Oanda 上下订单。获取 JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59654479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com