happydasch / btoandav20

Support for Oanda-V20 API in backtrader
Apache License 2.0
131 stars 52 forks source link

Insufficient authorization to perform request #37

Closed uelel closed 5 years ago

uelel commented 5 years ago
import pytz
import btoandav20
import v20

with open('token', 'r') as tokenFile:
    token = tokenFile.read().replace('\n', '')
oandastore = btoandav20.stores.OandaV20Store(token=token, account='001-004-3121847-001', practice=True)
print(oandastore.getbroker())

It is not possible to authenticate via OandaV20Store object. The output is: GET https://api-fxpractice.oanda.com:443/v3/accounts/001-004-3121847-001/summary expected status 200, got 401 (Unauthorized) Insufficient authorization to perform request.

The debug log of this snippet is here.

The authentication to Oanda API is possible (according to this page) thus the token is correct.

btoandav20 is installed together with pyyaml 5.1.2, v20 3.0.25.0, backtrader 1.9.74.123

Where should I look for the problem?

happydasch commented 5 years ago

It seems most likely that your processed token from file is incorrect. Check if your code works with providing the token as a string instead of reading it from file. The authentication itself works, so I close this issue.

uelel commented 5 years ago

The error still persists when provided the token as a string. Can you please hint the code line in boandav20 that controls creating GET request for https://api-fxpractice.oanda.com:443/v3/accounts/{accountNo}/summary (as stated in my error message?)?

happydasch commented 5 years ago

https://github.com/ftomassetti/backtrader-oandav20/blob/621ce2c7b1f056246f74301006ec2f5c496f8b62/btoandav20/stores/oandav20store.py#L466

Here is the line

happydasch commented 5 years ago

You may also try to generate a new Token and give it a try. Also ensure you provide the correct credentials for your practice account (since the example you provided is using a practice account)

uelel commented 5 years ago

Indeed, a wrong account has been provided (fxpractice). fxpractice URL should be called in case of demo account and fxtrade in case of trade account. Thank you for your help!

happydasch commented 5 years ago

I am glad you worked it out!

For a practice account you would set practice=True, for a live account practice=False. No need to set any urls, since the endpoints are hardcoded already.

Practice Account oandastore = btoandav20.stores.OandaV20Store(token=YYY, account=XXX, practice=True)

Live Account oandastore = btoandav20.stores.OandaV20Store(token=YYY, account=XXX, practice=False)