使用 python 请求从 oanda V20 rest api 流定价

Stream pricing from oanda V20 rest api using python requests

我正在尝试 stream the price Oandas 的 V20 rest api 乐器,但收效甚微。我正在使用 python 请求,因为它适用于常规 get 请求。这是我必须去的地方:

import requests
url = 'https://stream-fxpractice.oanda.com/v3/accounts/MY_ACCOUNT_ID/pricing?instruments=EUR_USD'
head = {'Content-type':"application/json",
        'Accept-Datetime-Format':"RFC3339",
        'Authorization':"Bearer MY_ACCESS8TOKEN"}


r = requests.get(url, headers=head, stream=True)
print(r)

for line in r.iter_lines():

    if line:
        decoded_line = line.decode('utf-8')
        print(json.loads(decoded_line))

响应错误代码为405,表示不支持该方法。 我做错了什么?

第 0 步:验证您的访问凭据:

使用默认的 OANDA curl 示例,验证您的访问凭据:

curl \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <AUTHENTICATION TOKEN>" \
  "https://api-fxtrade.oanda.com/v3/accounts/<ACCOUNT>/pricing?instruments=EUR_USD%2CUSD_CAD"

CASE isERR: 继续使用 OANDA 支持解决您的凭据无效问题。

CASE 正常: 继续步骤 1。


第 1 步:一次性复制 curl 调用语法 python

没有添加,没有排除。应该提供与步骤 0 中相同的结果。

CASE isERR: 查看您的一次性 python 代码,以满足 1:1 工作 OANDA 规范,已被证明是有效的在第 0 步中。

  • 查看 OANDA 服务状态,使用下面的 curl
    curl http://api-status.oanda.com/api/v1/services
  • 查看报告的错误详细信息。

CASE 正常: 继续步骤 2。


第 2 步:扩展 python 代码以请求和处理响应

然而,请记住,不要超出 OANDA 规定的每天最大请求数和类似限制,应谨慎处理。

CASE isERR: 查看 OANDA 服务状态,使用下面的 curl 和可能报告的错误详细信息:

curl http://api-status.oanda.com/api/v1/services

CASE 正常:恭喜,您的定价源端到端工作符合规范。


错误 405 不是 "not supported" 服务而是 "not allowed"

405 Method Not Allowed

A “405 Method Not Allowed” response may be returned from the v20 REST API when the client is attempting to access an API endpoint with an HTTP method that is not supported. The response Content-Type will be application/json and has the following schema:

{
    # 
    # The code of the error that has occurred.  This field may not be returned
    # for some errors.
    # 
    errorCode : (string),

    # 
    # The human-readable description of the error that has occurred.
    # 
    errorMessage : (string, required)
}

您的URL无效(参见developer.oanda.com/rest-live-v20/pricing-ep/),
应该是:

url_OK = 'https://stream-fxpractice.oanda.com/v3/accounts/MY_ACCOUNT_ID/pricing/stream?instruments=EUR_USD'

而不是:

urlNOT = 'https://stream-fxpractice.oanda.com/v3/accounts/MY_ACCOUNT_ID/pricing?instruments=EUR_USD'
//        |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||vvvvvvvv
// _OK = 'https://stream-fxpractice.oanda.com/v3/accounts/MY_ACCOUNT_ID/pricing/stream?instruments=EUR_USD'

如果您不想要构建 URL 的麻烦, 您可以使用 V20 绑定之一: https://github.com/search?utf8=%E2%9C%93&q=v20&type=

例如检查这些存储库中的示例代码,例如: https://github.com/hootnot/oandapyV20-examples