使用python请求从oandav20restapi流定价

2024-10-01 13:31:12 发布

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

我正在尝试从Oandas的v20restapi中提取一个工具,但是没有成功。我使用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,表示不支持该方法。 我做错什么了?在


Tags: 工具importjsonurlstreamgetmyline
2条回答

步骤0.:验证您的访问凭据:

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

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 iser:继续OANDA支持,以修复使用无效凭据的问题。在

案例正常:继续步骤1。


步骤1:在一个一次性python中复制curl调用语法AS-IS

没有增加,没有排除。应提供与步骤0相同的结果。在

CASE isERR:检查一下您的一次性python代码,以满足1:1的OANDA规范,该规范已经在步骤0中被证明是有效的。在

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

案例正常:继续步骤2。


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

但是,请记住,不要超过OANDA规定的每天请求的最大数量和类似的限制性限制,这些限制应该小心处理。在

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

^{pr2}$

案例没问题:恭喜你,你的定价来源按照规范进行了端到端的工作。在


错误405不是“不支持”服务,而是“不允许”

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'

而不是:

^{pr2}$

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

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

相关问题 更多 >