I am attempting to get a simple JSON currency rate response from an API (oanda).
but receiving various error codes, such as 'invalid syntax'.
Here is my updated code:
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 = 'xxxxxxxxxxxxxxxx'
account_id = 'xxxxxxxxx'
instruments = "EUR_USD"
try:
s = requests.Session()
url = "https://" + domain + "/v1/prices"
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)
response = urllib2.urlopen("https://api-fxpractice.oanda.com/v1/prices?instruments=EUR_USD")
data = json.load(response)
print data
Sorry, I edited the code and left out the error messages.. I was, however, able to solve the problem by using Oanda's python wrapper here https://github.com/oanda/oandapy.