如何在Python中访问Oanda的API

时间:2015-04-14 23:13:09

标签: python json

我希望从Oanda的API获得当前的费率。我怎么能在python中这样做?我有以下凭据,到目前为止我所尝试的一切都是破产。流速率不起作用,因为我无法正确解析它。非常感谢这里的帮助。

curl -X GET "https://api-fxpractice.oanda.com/v1/instruments?accountId=12345&instruments=EUR_USD"

在正常情况下,我可以让它工作,但是,它们需要一个"令牌"通过电话传递,我不知道如何实现。以下是他们在文档中的内容:

curl -H "Authorization: Bearer 12345678900987654321-abc34135acde13f13530" https://api-fxtrade.oanda.com/v1/accounts  

(顺便说一下,不是我的访问令牌,它在他们的网站上)

任何帮助都会很棒!

此外,还有一些我试图改编的流媒体代码:

import requests
import json

from optparse import OptionParser

def connect_to_stream():
    """
    Environment           <Domain>
    fxTrade               stream-fxtrade.oanda.com
    fxTrade Practice      stream-fxpractice.oanda.com
    sandbox               stream-sandbox.oanda.com
    """

    # Replace the following variables with your personal ones
    domain = 'stream-fxpractice.oanda.com'
    access_token = 'xxxxxxxxx'
    account_id = 'xxxxxx'
    instruments = "EUR_USD"

    try:
        s = requests.Session()
        url = "https://" + domain + "/v1/prices?instruments=EUR_USD"
        headers = {'Authorization' : 'Bearer ' + access_token,
                   # 'X-Accept-Datetime-Format' : 'unix'
                  }
        params = {'instruments' : instruments, 'accountId' : account_id}
        req = requests.Request('GET', url, headers = headers, params = params)
        pre = req.prepare()
        resp = s.send(pre, stream = True, verify = False)
        return resp
    except Exception as e:
        s.close()
        print "Caught exception when connecting to stream\n" + str(e) 
print resp

1 个答案:

答案 0 :(得分:0)

对于那些在将来寻找这个答案的人,我发现我可以使用Oanda的Python包装器来访问API。