hootnot / oanda-api-v20

OANDA REST-V20 API wrapper. Easy access to OANDA's REST v20 API with oandapyV20 package. Checkout the Jupyter notebooks!
MIT License
402 stars 107 forks source link

How to use oanda-api-v20 via proxy #176

Closed Warlib1975 closed 3 years ago

Warlib1975 commented 3 years ago

Hello, colleagues,

I need to connect to OANDA using a proxy server with authorization to follow the corporate security. Is it possible to do this when the request API?

hootnot commented 3 years ago

Hi,

oandapyV20 uses the requests library: https://github.com/psf/requests, the docs https://requests.readthedocs.io/en/master/user/advanced/ explain use of proxy's.

I think you should test the functionality you want by setting up some simple request for instance to get account information, like:

#!/usr/bin/env python

import requests
import json

TOKEN = "9a..............................-..............................d1"

headers = {}
headers['Authorization'] = 'Bearer ' + TOKEN

client = requests.session()
client.headers.update(headers)

rv = client.get("https://api-fxpractice.oanda.com/v3/accounts")

drv = rv.content.decode('utf-8')
print(json.loads(drv))

Running this will give you a list of accounts for the token:


{"accounts": [
   {
     "id": "111...",
     "tags": []
  }
 ]}

https://requests.readthedocs.io/en/master/user/advanced/ shows information on how to deal with proxy's. If you manage to get this example working via the proxy, then you should be able to configure oandapyV20 to work via the proxy by setting the additional config.

Additional headers are already possible.

So, if you have the example working via the proxy please let me know what you need if it is not already covered.

For now I will close this issue since it is not a specific oandapyV20 related thing.

Warlib1975 commented 3 years ago

Thank you very much for your response.

I try to send a more complicated request:

headers = {}
headers['Authorization'] = 'Bearer ' + token
headers['Content-Type'] = 'application/json'

client = requests.session()
client.headers.update(headers)

#rv = client.get("https://api-fxpractice.oanda.com/v3/accounts/" + accountID + "/instruments")
rv = client.get("https://api-fxtrade.oanda.com/v3/instruments/USD_JPY/orderBook")

drv = rv.content.decode('utf-8')
print(json.loads(drv))

And got error: {'errorMessage': 'Insufficient authorization to perform request.'}

I double-checked the OANDA docs, but couldn't understand, what's wrong with the request.

The equivalent API code works ok:

api = oandapyV20.API(access_token=token)
r = instruments.InstrumentsOrderBook(instrument=instrument)
rv = api.request(r)
print(rv)

According to the example from https://developer.oanda.com/rest-live-v20/instrument-ep/, the request should work without any issues.

The curl request: curl -i -H "Authorization: Bearer e..........e" https://api-fxtrade.oanda.com/v3/instruments/GBP_USD/orderBook

gives the same response:

Content-Length: 65
Content-Type: application/json

{"errorMessage":"Insufficient authorization to perform request."}
hootnot commented 3 years ago

Hi,

That is almost 100% a token issue. Revoke it and generate a new one. I have experienced this myself several years back. But I know some others experienced it also.

Also note that you need different tokens for live / practice environment. So, in the code above you need the token for the practice environment if you use api-fxpractice.oanda.com and a live token if you use api-fxtrade.

I would advise you to set things up for practice first.

oeyvindds commented 3 years ago

Have you specified if you are connecting to "Live" or "Practice" ? The token is only valid for one of the two environments, and will give that error if you mix them

hootnot commented 3 years ago

@oeyvindds: the code above is not oandapyV20 related but a direct connection with oanda. A token generated from a live account can only be used on api-fxtrade ... and a token generated from a test account can only be used on the api-fxpractice.

When using the oandapyV20 library you can distinguish between the environments by specifying the environment to "live" along with a token. It defaults to "practice", so if a token for practice is passed there is no need to specify the environment

Warlib1975 commented 3 years ago

@hootnot thank you! I have changed the link from api-fxtrade.oanda.com to api-fxpractice.oanda.com and everything is ok now:

curl -i -H "Authorization: Bearer e..........e" https://api-fxpractice.oanda.com/v3/instruments/GBP_USD/orderBook

Completely forgot that links for live and practice are different.