Skip to content

Authentication error after entering OTP #178

@t0mer

Description

@t0mer

Describe the bug

When trying to perform the client.manual_login() function, right fter entering the OTP the code throws the following error:

Traceback (most recent call last):
  File "/opt/dev/n8n-iec/app.py", line 8, in get_data
    await client.manual_login()  # login with user inputs
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/iec_api/iec_client.py", line 746, in manual_login
    token = await login.manual_authorization(self._session, self._user_id)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/iec_api/login.py", line 188, in manual_authorization
    code = await authorize_session(session, otp_code)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/iec_api/login.py", line 52, in authorize_session
    code = re.findall(
           ^^^^^^^^^^^
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/dev/n8n-iec/app.py", line 24, in <module>
    asyncio.run(get_data())
  File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/opt/dev/n8n-iec/app.py", line 10, in get_data
    logger.error(f"Failed Login: (Code {err.code}): {err.error}")
                                        ^^^^^^^^
AttributeError: 'IndexError' object has no attribute 'code'

To reproduce

Run the following code, and enter the OPT

from iec_api import iec_client as iec
from loguru import logger
import asyncio

async def get_data(): 
    client = iec.IecClient("0123456789

[test.txt](https://github.com/user-attachments/files/20614359/test.txt)

")
    try:
        await client.manual_login()  # login with user inputs
    except Exception as err:
        logger.error(f"Failed Login: (Code {err.code}): {err.error}")
        raise

    customer = await client.get_customer()
    print(customer)

    contracts = await client.get_contracts()
    for contract in contracts:
        print(contract)

    reading = await client.get_last_meter_reading(customer.bp_number, contracts[0].contract_id)
    print(reading)
    
if __name__ == "__main__":
    asyncio.run(get_data())

Expected behavior

The code in the function expect to find hidden input in the html:

code = re.findall(
        r"<input type=\"hidden\" name=\"code\" value=\"(.+)\"/>",
        authorize_response.encode("latin1").decode("utf-8"),
    )[0]

Actual behavior

The hidden input does not exists in the html

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions