Authorization

Kite login and authorization

All arguments are optional. It defaults to Kite web login if no arguments are provided. You will be required to enter your credentials via terminal user input.

Once authorization is complete, the token is set. You can access them as

  • kite.enctoken - For Kite web login

  • kite.access_token - For KiteConnect login

These can be stored or saved and reused on the next run. You can pass the token to the Kite class during initialization.

For Kite web login, Kite.authorize takes the following optional parameters:

  • user_id: Kite Web user-id

  • password: Kite Web login password

  • twofa: string or callable function that returns OTP

To generate OTP you can use a package like pyotp, just pass totp.now to twofa argument.

# Kite Web Login
async with Kite(enctoken=enctoken) as kite:
  await kite.authorize(
      user_id=user_id,
      password=pwd,
      twofa=pyotp.TOTP('TOTP_STRING').now, # dont invoke now()
  )

For KiteConnect login, Kite.authorize takes the following optional parameters:

  • request_token : KiteConnect request_token

  • api_key: KiteConnect API key

  • secret: KiteConnect API secret

# KiteConnect login
async with Kite(access_token=access_token) as kite:
  await kite.authorize(
      request_token=request_token,
      api_key=api_key,
      secret=secret,
  )

KiteFeed requires the following optional arguments

For Kite Web:

  • user_id: Kite Web user-id

  • enctoken: enctoken obtained from Kite Web Login

For KiteConnect:

  • api_key: Kite Web user-id

  • access_token : access_token obtained from KiteConnect login

Authorization Flow

  1. On running Kite.authorize, it first checks if enctoken or access_token was set during initialization.
    • If yes, the authorization headers are updated, and the class is ready to make API requests.

  2. If request_token and secret are provided, proceed with KiteConnect login. Once the access_token is received, set the authorization headers.

  3. For Kite Web login, enctoken is stored in cookies. Check if the cookie file exists and has not expired. If yes, load the enctoken and update headers.

  4. If no cookie file exists or has expired, proceed with Kite Web login. Once the enctoken is received:
    • Set the cookie expiry for the end of the day.

    • Save the cookie to file

    • Update the authorization headers with enctoken

Dhan login and authorization

Authorization with Dhan is simple. Pass the client_id and access_token (provided during app registration) to the constructor.

async with Dhan(client_id="client_id", access_token="access_token") as dhan:
     holdings = await dhan.get_holdings()

Fyers login and authorization

Fyers requires generating an app at https://myapi.fyers.in/ to get the APP id and secret id.

You must also provide the redirect url, which is required during the authorization.

from aio_trader.fyers import FyersAuth


async def main():
    # STEP 1
    auth = FyersAuth(
        client_id="client_id",
        redirect_uri="redirect_uri",
        secret_key="secret_key",
        state="OPTIONAL random string",
    )

    # Open the below url in a browser and login
    # On successful login you will be redirected to the redirect_uri
    # with the auth_token and state in url parameters
    url = auth.generate_authcode()

    # STEP 2: Use the auth_token received in previous step
    auth_token = "auth_token"

    data = await auth.generate_token(auth_code=auth_token)

    if "access_token" not in data:
        print("Failed to get access_token")
        await auth.close()

    access_token = data["access_token"]

Use the access token received to make requests.

async with Fyers(client_id="client_id", token="access_token") as fyers:
     await fyers.get_profile()