Skip to content

Login failure with TOTP-enabled procedure #393

Open

Description

@tma-fritz

Description of the bug
pytr fails to login if TOTP ("Authenticator app") was enabled in the TR app.

Reproducer with log

$ uvx --with git+https://github.com/pytr-org/pytr pytr dl_docs -s --last_days 30 --v2 export
10:08:30 Using credentials from file /home/u1000/.pytr/credentials. Phone: +4917********, PIN: ****
10:08:30 Trying to resume websession...
10:08:30 Resuming websession failed.
10:08:30 Initiating web login...
10:08:30 WAF token skipped.
Enter the code from your authenticator app: <6 digits>
10:08:36 Logged in.
Traceback (most recent call last):
  File "/home/u1000/.cache/uv/archive-v0/nuulbc0jtt9D8WTE/bin/pytr", line 12, in <module>
    sys.exit(main())
             ~~~~^^
  File "/home/u1000/.cache/uv/archive-v0/nuulbc0jtt9D8WTE/lib/python3.14/site-packages/pytr/main.py", line 616, in main
    else login(
         ~~~~~^
        phone_no=args.phone_no,
        ^^^^^^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
        v2=args.v2,
        ^^^^^^^^^^^
    ),
    ^
  File "/home/u1000/.cache/uv/archive-v0/nuulbc0jtt9D8WTE/lib/python3.14/site-packages/pytr/account.py", line 91, in login
    log.debug(get_settings(tr))
              ~~~~~~~~~~~~^^^^
  File "/home/u1000/.cache/uv/archive-v0/nuulbc0jtt9D8WTE/lib/python3.14/site-packages/pytr/account.py", line 14, in get_settings
    formatted_json = json.dumps(tr.settings(), indent=2)
                                ~~~~~~~~~~~^^
  File "/home/u1000/.cache/uv/archive-v0/nuulbc0jtt9D8WTE/lib/python3.14/site-packages/pytr/api.py", line 1021, in settings
    r = self._web_request("/api/v2/auth/account")
  File "/home/u1000/.cache/uv/archive-v0/nuulbc0jtt9D8WTE/lib/python3.14/site-packages/pytr/api.py", line 520, in _web_request
    r.raise_for_status()
    ~~~~~~~~~~~~~~~~~~^^
  File "/home/u1000/.cache/uv/archive-v0/nuulbc0jtt9D8WTE/lib/python3.14/site-packages/requests/models.py", line 1167, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.traderepublic.com/api/v1/auth/web/session

Environment

  • OS: Ubuntu
  • pytr version: 0.4.11 (GIT HEAD via uvx)

Additional context
I only enabled the TOTP recently in the TR app. 2 days ago, the "v2" login with just the app worked OK.

Activity

  1. added
    bug
    on Aug 14, 2026
  2. marcozambi commented on Sep 20, 2026

    @marcozambi

    Can confirm this independently, with some additional detail that might help narrow it down.

    Setup: pytr at the pinned commit e7f3ba37167bda15c0b5501f7ddab24c8d24d37e (current master around this age), called via the Python API (pytr.account.login(waf_token="default", v2=True, store_credentials=True)), not the CLI -- but the crash is inside login() itself, so it should reproduce identically either way. Account has TOTP/authenticator enabled, same as OP.

    What happens: the login itself completes fully -- phone/PIN accepted, initiate_weblogin() succeeds, the authenticator-verification POST to /api/v2/auth/web/login/processes/{id}/authenticator-verification succeeds (_raise_for_login_error doesn't raise), "Logged in." prints. The crash is entirely after that, in login()'s own trailing debug line:

    log.debug(get_settings(tr))

    Python evaluates get_settings(tr) eagerly regardless of the actual log level, so this always runs tr.settings() -> _web_request("/api/v2/auth/account"), which 401s.

    Two layers to this, both reproduced:

    1. _web_request() itself has a side effect that makes this worse: on the very first REST call after any login, _session_expires_at is still 0, so it first tries to refresh the session via a legacy endpoint, GET /api/v1/auth/web/session. That refresh call 401s too, and since it's unconditional (r.raise_for_status(), no try/except), it's actually the very first exception raised -- before you even get to the "real" /api/v2/auth/account call. Patching around just this refresh ping (skip it, proceed to the real request anyway) gets you one layer deeper...
    2. ...but then the actual /api/v2/auth/account GET 401s too, in exactly the same run, using the exact same session cookies that complete_weblogin() just saved via save_websession(). So this isn't just the debug line being unlucky -- the v2-established session genuinely isn't accepted by this REST endpoint. I confirmed this happens even on a second run that resumes the previously-saved session (resume_websession() also fails calling tr.settings() internally, for the same reason) -- fully reproducible, not a one-off network blip.

    Why this blocks more than just the debug line: Portfolio.compact_portfolio() (i.e. actually fetching positions/portfolio data) also calls self.settings() on its own, to look up _sec_acc_no the first time it's needed:

    async def compact_portfolio(self):
        if self._sec_acc_no is None:
            self.settings()
        ...

    So even a caller that doesn't care about login()'s debug output still hits this 401 as soon as it tries to fetch actual portfolio data. It's not cosmetic.

    Possibly relevant: the websocket-authenticated calls (cash(), watchlist(), the portfolio subscription itself) don't go through _web_request() at all -- they authenticate via cookies sent as a raw Cookie header on the websocket handshake instead. I haven't yet confirmed whether those succeed under v2 (haven't gotten past compact_portfolio()'s settings() call to find out), but if they do, that would suggest the v2 session cookies are valid for something, and the REST-layer 401 is specific to whatever /api/v2/auth/account (or REST auth generally) expects that the authenticator-verification response isn't actually granting -- maybe an additional header, a different cookie attribute (domain/path/SameSite), or something set only by the other v2 branch (_await_weblogin_confirmation(), the app-push path) that the authenticator-verification branch skips.

    Given OP's report that this only started after enabling TOTP, that lines up with my own account (TOTP enabled) also failing -- worth checking whether the app-push confirmation branch (no TOTP) is actually unaffected, if anyone can test that combination.

    Happy to help test a fix against a real account if someone has a theory on what the authenticator-verification response should additionally be doing.

  3. tma-fritz commented on Sep 21, 2026

    @tma-fritz
    Author

    FWIW, I disabled the authenticator from within the TR app, again. I can now use pytr again.

  4. Zorgosto commented on Oct 5, 2026

    @Zorgosto
    Contributor

    It seems that the authentication does not wait for the confirmation in the app. When I entered the TOTP token the app displays a confirmation to accept for the login but this error gets thrown before being able to confirm it:

    pytr login --v2
    01:18:12 Using credentials from file /home/<user>/.pytr/credentials. Phone: +********, PIN: ****
    01:18:12 Initiating web login...
    01:18:12 WAF token skipped.
    Enter the code from your authenticator app: 085092
    01:18:26 Logged in.
    Traceback (most recent call last):
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/bin/.pytr-wrapped", line 9, in <module>
        sys.exit(main())
                 ~~~~^^
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/lib/python3.14/site-packages/pytr/main.py", line 557, in main
        login(
        ~~~~~^
            phone_no=args.phone_no,
            ^^^^^^^^^^^^^^^^^^^^^^^
        ...<3 lines>...
            v2=args.v2,
            ^^^^^^^^^^^
        )
        ^
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/lib/python3.14/site-packages/pytr/account.py", line 91, in login
        log.debug(get_settings(tr))
                  ~~~~~~~~~~~~^^^^
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/lib/python3.14/site-packages/pytr/account.py", line 14, in get_settings
        formatted_json = json.dumps(tr.settings(), indent=2)
                                    ~~~~~~~~~~~^^
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/lib/python3.14/site-packages/pytr/api.py", line 1021, in settings
        r = self._web_request("/api/v2/auth/account")
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/lib/python3.14/site-packages/pytr/api.py", line 520, in _web_request
        r.raise_for_status()
        ~~~~~~~~~~~~~~~~~~^^
      File "/nix/store/k2bghw14yj34mjbqx3s837314fw75bp9-python3.14-requests-2.34.2/lib/python3.14/site-packages/requests/models.py", line 1167, in raise_for_status
        raise HTTPError(http_error_msg, response=self)
    requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.traderepublic.com/api/v1/auth/web/session
    
  5. Zorgosto commented on Oct 6, 2026

    @Zorgosto
    Contributor

    It seems that the authentication does not wait for the confirmation in the app. When I entered the TOTP token the app displays a confirmation to accept for the login but this error gets thrown before being able to confirm it:

    pytr login --v2
    01:18:12 Using credentials from file /home/<user>/.pytr/credentials. Phone: +********, PIN: ****
    01:18:12 Initiating web login...
    01:18:12 WAF token skipped.
    Enter the code from your authenticator app: 085092
    01:18:26 Logged in.
    Traceback (most recent call last):
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/bin/.pytr-wrapped", line 9, in <module>
        sys.exit(main())
                 ~~~~^^
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/lib/python3.14/site-packages/pytr/main.py", line 557, in main
        login(
        ~~~~~^
            phone_no=args.phone_no,
            ^^^^^^^^^^^^^^^^^^^^^^^
        ...<3 lines>...
            v2=args.v2,
            ^^^^^^^^^^^
        )
        ^
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/lib/python3.14/site-packages/pytr/account.py", line 91, in login
        log.debug(get_settings(tr))
                  ~~~~~~~~~~~~^^^^
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/lib/python3.14/site-packages/pytr/account.py", line 14, in get_settings
        formatted_json = json.dumps(tr.settings(), indent=2)
                                    ~~~~~~~~~~~^^
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/lib/python3.14/site-packages/pytr/api.py", line 1021, in settings
        r = self._web_request("/api/v2/auth/account")
      File "/nix/store/k5j2ahkdwk17gfpf2j7dc4cvhksfgacw-pytr-0.4.10/lib/python3.14/site-packages/pytr/api.py", line 520, in _web_request
        r.raise_for_status()
        ~~~~~~~~~~~~~~~~~~^^
      File "/nix/store/k2bghw14yj34mjbqx3s837314fw75bp9-python3.14-requests-2.34.2/lib/python3.14/site-packages/requests/models.py", line 1167, in raise_for_status
        raise HTTPError(http_error_msg, response=self)
    requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.traderepublic.com/api/v1/auth/web/session
    

    Debug log:

    uvx --python "$(command -v python3)" \
      --from 'git+https://github.com/pytr-org/pytr.git' \
      pytr -v debug login --v2
    2026-10-06 02:16:19+0200 main      DEBUG    logging is set to debug
    2026-10-06 02:16:19+0200 account   INFO     Using credentials from file /home/user/.pytr/credentials. Phone: +********, PIN: ****
    2026-10-06 02:16:19+0200 api       DEBUG    Called resume_websession.
    2026-10-06 02:16:19+0200 api       INFO     Trying to resume websession...
    2026-10-06 02:16:19+0200 api       DEBUG    Calling settings...
    2026-10-06 02:16:20+0200 api       INFO     Resuming websession failed.
    2026-10-06 02:16:20+0200 api       DEBUG    Error calling tr.settings().
    Traceback (most recent call last):
      File "/home/user/.cache/uv/archive-v0/YqL_V3NYq75F0-H8/lib/python3.13/site-packages/pytr/api.py", line 506, in resume_websession
        self.settings()
        ~~~~~~~~~~~~~^^
      File "/home/user/.cache/uv/archive-v0/YqL_V3NYq75F0-H8/lib/python3.13/site-packages/pytr/api.py", line 1021, in settings
        r = self._web_request("/api/v2/auth/account")
      File "/home/user/.cache/uv/archive-v0/YqL_V3NYq75F0-H8/lib/python3.13/site-packages/pytr/api.py", line 520, in _web_request
        r.raise_for_status()
        ~~~~~~~~~~~~~~~~~~^^
      File "/home/user/.cache/uv/archive-v0/YqL_V3NYq75F0-H8/lib/python3.13/site-packages/requests/models.py", line 1167, in raise_for_status
        raise HTTPError(http_error_msg, response=self)
    requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.traderepublic.com/api/v1/auth/web/session
    2026-10-06 02:16:20+0200 api       INFO     Initiating web login...
    2026-10-06 02:16:20+0200 api       INFO     WAF token skipped.
    2026-10-06 02:16:20+0200 api       DEBUG    Web login returned: 200
    2026-10-06 02:16:20+0200 api       DEBUG    Web login data: {
        "processId": "a86f0a40-6ffd-435b-9e2b-b4ea63c8d1f7",
        "claims": null
    }
    Enter the code from your authenticator app: xxxxxx
    2026-10-06 02:16:34+0200 account   INFO     Logged in.
    Traceback (most recent call last):
      File "/home/user/.cache/uv/archive-v0/YqL_V3NYq75F0-H8/bin/pytr", line 12, in <module>
        sys.exit(main())
                 ~~~~^^
      File "/home/user/.cache/uv/archive-v0/YqL_V3NYq75F0-H8/lib/python3.13/site-packages/pytr/main.py", line 557, in main
        login(
        ~~~~~^
            phone_no=args.phone_no,
            ^^^^^^^^^^^^^^^^^^^^^^^
        ...<3 lines>...
            v2=args.v2,
            ^^^^^^^^^^^
        )
        ^
      File "/home/user/.cache/uv/archive-v0/YqL_V3NYq75F0-H8/lib/python3.13/site-packages/pytr/account.py", line 91, in login
        log.debug(get_settings(tr))
                  ~~~~~~~~~~~~^^^^
      File "/home/user/.cache/uv/archive-v0/YqL_V3NYq75F0-H8/lib/python3.13/site-packages/pytr/account.py", line 14, in get_settings
        formatted_json = json.dumps(tr.settings(), indent=2)
                                    ~~~~~~~~~~~^^
      File "/home/user/.cache/uv/archive-v0/YqL_V3NYq75F0-H8/lib/python3.13/site-packages/pytr/api.py", line 1021, in settings
        r = self._web_request("/api/v2/auth/account")
      File "/home/user/.cache/uv/archive-v0/YqL_V3NYq75F0-H8/lib/python3.13/site-packages/pytr/api.py", line 520, in _web_request
        r.raise_for_status()
        ~~~~~~~~~~~~~~~~~~^^
      File "/home/user/.cache/uv/archive-v0/YqL_V3NYq75F0-H8/lib/python3.13/site-packages/requests/models.py", line 1167, in raise_for_status
        raise HTTPError(http_error_msg, response=self)
    requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.traderepublic.com/api/v1/auth/web/session
    
  6. RealCLanger commented on Oct 7, 2026

    @RealCLanger
    Collaborator

    I merged #400. Can somebody test and confirm whether it's fixed or not? Thanks.

  7. christian-haider commented on Oct 8, 2026

    @christian-haider

    I tested it with uvx --refresh --from git+https://github.com/pytr-org/pytr.git@refs/pull/400/head pytr login --v2

    and I still get the following error

    C:\\pytr>uvx --refresh --from git+https://github.com/pytr-org/pytr.git@refs/pull/400/head pytr login --v2
    Please enter your TradeRepublic phone number in the format +4912345678:
    +431111111111
    Please enter your TradeRepublic pin:
    Pin (Input is hidden):
    09:49:09 Initiating web login...
    09:49:09 WAF token skipped.
    Traceback (most recent call last):
      File "<frozen runpy>", line 203, in _run_module_as_main
      File "<frozen runpy>", line 88, in _run_code
      File "C:\Users\user\AppData\Local\uv\cache\archive-v0\1M4MBOKlsx8aGAsW\Scripts\pytr.exe\__main__.py", line 10, in <module>
        sys.exit(main())
                 ~~~~^^
      File "C:\Users\user\AppData\Local\uv\cache\archive-v0\1M4MBOKlsx8aGAsW\Lib\site-packages\pytr\main.py", line 557, in main
        login(
        ~~~~~^
            phone_no=args.phone_no,
            ^^^^^^^^^^^^^^^^^^^^^^^
        ...<3 lines>...
            v2=args.v2,
            ^^^^^^^^^^^
        )
        ^
      File "C:\Users\user\AppData\Local\uv\cache\archive-v0\1M4MBOKlsx8aGAsW\Lib\site-packages\pytr\account.py", line 62, in login
        countdown = tr.initiate_weblogin()
      File "C:\Users\user\AppData\Local\uv\cache\archive-v0\1M4MBOKlsx8aGAsW\Lib\site-packages\pytr\api.py", line 368, in initiate_weblogin
        r.raise_for_status()
        ~~~~~~~~~~~~~~~~~~^^
      File "C:\Users\user\AppData\Local\uv\cache\archive-v0\1M4MBOKlsx8aGAsW\Lib\site-packages\requests\models.py", line 1167, in raise_for_status
        raise HTTPError(http_error_msg, response=self)
    requests.exceptions.HTTPError: 429 Client Error: Too Many Requests for url: https://api.traderepublic.com/api/v2/auth/web/login
    
    
  8. RealCLanger commented on Oct 8, 2026

    @RealCLanger
    Collaborator

    Can you please remove your cookies from $HOME/.pytr and then run fresh. And please also reference the head of the repository like --from git+https://github.com/pytr-org/pytr.git

  9. christian-haider commented on Oct 8, 2026

    @christian-haider

    I deleted now $HOME/.pytr.

    Then I started it with

    uvx --refresh --from git+https://github.com/pytr-org/pytr.git pytr login --v2

    I still get this error. Note the 429 - not 100% sure if its really a problem in the pytr anymore, or TR did "block" me....

    Update: Waited 12h - still the same error.

    C:\pytr>uvx --refresh --from git+https://github.com/pytr-org/pytr.git pytr login --v2
          Built pytr @ git+https://github.com/pytr-org/pytr.git@db34fa389d83adbace39a9d96622352e2e844ed3                                                                                                             Installed 20 packages in 244ms
    Please enter your TradeRepublic phone number in the format +4912345678:
    +431111111111
    Please enter your TradeRepublic pin:
    Pin (Input is hidden):
    10:22:29 Initiating web login...
    10:22:29 WAF token skipped.
    Traceback (most recent call last):
      File "<frozen runpy>", line 203, in _run_module_as_main
      File "<frozen runpy>", line 88, in _run_code
      File "C:\Users\user\AppData\Local\uv\cache\archive-v0\TKdmG-K1K95bLkTA\Scripts\pytr.exe\__main__.py", line 10, in <module>
        sys.exit(main())
                 ~~~~^^
      File "C:\Users\user\AppData\Local\uv\cache\archive-v0\TKdmG-K1K95bLkTA\Lib\site-packages\pytr\main.py", line 576, in main
        login(
        ~~~~~^
            phone_no=args.phone_no,
            ^^^^^^^^^^^^^^^^^^^^^^^
        ...<4 lines>...
            account=args.account,
            ^^^^^^^^^^^^^^^^^^^^^
        )
        ^
      File "C:\Users\user\AppData\Local\uv\cache\archive-v0\TKdmG-K1K95bLkTA\Lib\site-packages\pytr\account.py", line 87, in login
        countdown = tr.initiate_weblogin()
      File "C:\Users\user\AppData\Local\uv\cache\archive-v0\TKdmG-K1K95bLkTA\Lib\site-packages\pytr\api.py", line 415, in initiate_weblogin
        r.raise_for_status()
        ~~~~~~~~~~~~~~~~~~^^
      File "C:\Users\user\AppData\Local\uv\cache\archive-v0\TKdmG-K1K95bLkTA\Lib\site-packages\requests\models.py", line 1167, in raise_for_status
        raise HTTPError(http_error_msg, response=self)
    requests.exceptions.HTTPError: 429 Client Error: Too Many Requests for url: https://api.traderepublic.com/api/v2/auth/web/login
    
    C:\pytr>
    
  10. Zorgosto commented on Oct 8, 2026

    @Zorgosto
    Contributor

    I got rate limited one time while testing. Not sure now long that lasted. Like an hour?

  11. medomatto commented on Oct 30, 2026

    @medomatto

    Same here, 429. Even when waiting till after the given deadline a 429 error is spit out. Hm...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions