json - Issues with Curl request in python -
i attempting simple json currency rate response api (oanda).
receiving various error codes, such 'invalid syntax'.
here updated code:
import requests import json 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 following variables 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 e: s.close() print "caught exception when connecting stream\n" + str(e) response = urllib2.urlopen("https://api-fxpractice.oanda.com/v1/prices?instruments=eur_usd") data = json.load(response) print data
sorry, edited code , left out error messages.. was, however, able solve problem using oanda's python wrapper here https://github.com/oanda/oandapy.
the url invalid, i'd suggest checking whether api has changed, search on reveals other people have tried using in past. when trying access url in chrome receive following information:
the server @ api-practice.oanda.com can't found, because dns lookup failed. dns network service translates website's name internet address. error caused having no connection internet or misconfigured network. can caused unresponsive dns server or firewall preventing google chrome accessing network.
error code: dns_probe_finished_nxdomain
also, stick using requests library or urllib2. request @ bottom of question can rewritten from:
response = urllib2.urlopen("https://api-fxpractice.oanda.com/v1/prices?instruments=eur_usd") data = json.load(response) print data
to:
r = requests.get("https://api-fxpractice.oanda.com/v1/prices?instruments=eur_usd") data = r.json() print data
edit: url updated match edit in question.
https://api-practice.oanda.com/v1/prices?instruments=eur_usd
to
"https://api-fxpractice.oanda.com/v1/prices?instruments=eur_usd"
Comments
Post a Comment