0

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.

4
  • 1
    Maybe you could also share the errors which you get?
    – pagid
    Commented Apr 15, 2015 at 19:52
  • Don't whinge. Elide unnecessary context. Also, you should add in exact error messages you're receiving. Commented Apr 15, 2015 at 22:57
  • @Nathaniel Ford Thanks buddy for pointing out my excessive whinging, didn't realize I was being so bothersome. I will try and post my previous errors as well..
    – MacD
    Commented Apr 16, 2015 at 0:16
  • @user2883183 Apologies for being brusque. This post ended up in the edit queue. Understand that the best posts only include the concise details of the problem at hand, and elide extraneous context that doesn't aid in solving the problem. For instance, saying 'I edited the code and left out the error messages' explains details about how you came to post the problem, but neither helps SO users help you solve it now, or other users with the same problem in the future. If we all write better posts, SO will become even more useful. Commented Apr 16, 2015 at 4:47

2 Answers 2

1

The url is invalid, I'd suggest checking whether the API has recently changed, as a search on it reveals that other people have tried using it in the past. When trying to access the url in chrome I receive the following information:

The server at api-practice.oanda.com can't be found, because the DNS lookup failed. DNS is the network service that translates a website's name to its Internet address. This error is most often caused by having no connection to the Internet or a misconfigured network. It can also be caused by an unresponsive DNS server or a firewall preventing Google Chrome from accessing the network.

Error code: DNS_PROBE_FINISHED_NXDOMAIN

Also, I would stick to using the requests library or urllib2. The request at the bottom of your question can be 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 to 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"

7
  • I'm sorry, that url is api-fxpractice.oanda.com. After I add you revision I get this error - u'This request requires authorization', u'code': 3} - any thoughts? Ill update the url as well
    – MacD
    Commented Apr 14, 2015 at 18:55
  • Have you got a user access token for oanda? You need to pass that in the request. Commented Apr 14, 2015 at 19:11
  • Yes I do, I just took it out. I can add it back if someone needs it though?
    – MacD
    Commented Apr 14, 2015 at 19:16
  • Never share a personal access token, think of it like a password. Please don't over edit your answer. It's now unclear how my response is related to the question, and will not be of use to future StackOverflow users. Commented Apr 14, 2015 at 19:19
  • Include an edit specify the new problems. e.g. EDIT: Now I receive an error .... (need to keep the question and answer easy to follow for other users) Commented Apr 14, 2015 at 19:43
0

Please do an ipconfig /flushdns from command prompt of the server and you are most likely to get the result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.