Getting the error Invalid totp : errorcode: AB1050 python


  • import os
    import sys
    from SmartApi import SmartConnect
    import pandas as pd
    import matplotlib.pyplot as plt
    import pandas_ta as ta
    import pyotp

    Set API credentials directly (for testing purposes)

    os.environ["ANGEL_CLIENT_ID"] = "S165618"
    os.environ["ANGEL_PASSWORD"] = "Sa@774202"
    os.environ["ANGEL_TOTP_SECRET"] = "4H4TKDAHHVXTAUKBS2QODHFRKM"

    Load API credentials from environment variables

    client_id = os.getenv("ANGEL_CLIENT_ID")
    password = os.getenv("ANGEL_PASSWORD")
    totp_secret = os.getenv("ANGEL_TOTP_SECRET")

    if not all([client_id, password, totp_secret]):
    print("Environment Variables:")
    print(f"ANGEL_CLIENT_ID: {client_id}")
    print(f"ANGEL_PASSWORD: {password}")
    print(f"ANGEL_TOTP_SECRET: {totp_secret}")
    raise ValueError("One or more environment variables are missing: ANGEL_CLIENT_ID, ANGEL_PASSWORD, ANGEL_TOTP_SECRET")

    Generate TOTP code

    totp = pyotp.TOTP(totp_secret).now()
    print("Current TOTP:", totp)

    Initialize SmartConnect instance

    smart_api = SmartConnect(api_key=client_id)

    [E 240624 18:31:06 smartConnect:243] Error occurred while making a POST request to https://apiconnect.angelbroking.com/rest/auth/angelbroking/user/v1/loginByPassword. Error: Invalid totp. URL: https://apiconnect.angelbroking.com/rest/auth/angelbroking/user/v1/loginByPassword, Headers: {'Content-type': 'application/json', 'X-ClientLocalIP': '127.0.0.1', 'X-ClientPublicIP': '106.193.147.98', 'X-MACAddress': '40:b0:76:da:58:49', 'Accept': 'application/json', 'X-PrivateKey': 'xx', 'X-UserType': 'USER', 'X-SourceID': 'WEB'}, Request: {'clientcode': 'xxxx', 'password': 'xxxx', 'totp': 'xxx'}, Response: {'status': False, 'message': 'Invalid totp', 'errorcode': 'AB1050', 'data': None}
    Error generating session: 'NoneType' object is not subscriptable


  • Hello @smohol

    Everything(request and headers) looks fine.
    Can you please check and confirm whether you are using the updated TLS version or not ?

    Regards,
    SmartAPI Team