fhqvst / avanza

A JavaScript client for the unofficial Avanza API
MIT License
229 stars 59 forks source link

Is therer anyway toclose avanza connection? #62

Closed anteg closed 3 years ago

anteg commented 3 years ago

Hi

It feels like I am doing something wrong here. When I for example run my script to check last price of certain stock by setting up connection to avanza then requesting the lastPrice everything works fine and I get the lastPrice. BUT when I run it to frequent, say 5 seconds after first one, I get error and have to wait some time

`Traceback (most recent call last): File "StockPriceCopy.py", line 4, in avanza = Avanza({

File "/lib/python3.9/site-packages/avanza/avanza.py", line 24, in init response_body, credentials = self.__authenticate(credentials)

File "/lib/python3.9/site-packages/avanza/avanza.py", line 66, in authenticate return self.validate_2fa(credentials)

File "/lib/python3.9/site-packages/avanza/avanza.py", line 83, in __validate_2fa response.raise_for_status()

File "/usr/lib/python3.9/site-packages/requests/models.py", line 943, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://www.avanza.se/_api/authentication/sessions/totp `

There for my question if there is possible to like close current connection to avanza and by that maybe eliminating this issue? Anyone else having problem with getting connection to avanza when connecting to frequent?

fhqvst commented 3 years ago

Not sure you've ended up in the right place. This repo is for an unofficial node.js module and has nothing to do with Python.

Either way, they have a security mechanism on their side which prevents a user from trying to log in too frequently. Instead, you should log in once, save the auth token, and re-use it on each sequential call to the API.

anteg commented 3 years ago

Oooh sorry for lost my way here =) But thanks alot for the answer. How do you mean by saving the auth token? Do you mind explaining that a bit for me? Would very much appriciate it.

fhqvst commented 3 years ago

Don't really have time to go into detail here, but have a look at our auth flow, hopefully it's pretty easy to understand what's going on: https://github.com/fhqvst/avanza/blob/master/lib/index.js#L479-L499