py5paisa icon indicating copy to clipboard operation
py5paisa copied to clipboard

How to get Response Token using code for Login?

Open altimetrikchallenge opened this issue 1 year ago • 2 comments

  • 5paisa Python SDK version:
  • Python version:
  • Operating System:

Description

Can someone please share the steps you perform to login via Python? The issue I am facing is that how do you perform these steps?

#> # OAUTH Approach

First get a token by logging in to -> https://dev-openapi.5paisa.com/WebVendorLogin/VLogin/Index?VendorKey=<Your Vendor Key>&ResponseURL=<Redirect URL>

VendorKey is UesrKey for individuals user

for e.g. you can use ResponseURL as https://www.5paisa.com/technology/developer-apis

Pass the token received in the response url after successful login to get an access token (this also sets the token for all the APIs you use)-

Manually we hit the url with our VendorKey and Redirect URL, we can get the response token from the url.

How you guys are achieving this automatically? Or should we get this manually and feed to our script?

What I Did

fivePaisaClient = FivePaisaClient(cred=credentialsKeys) respToken = fivePaisaClient.get_totp_session(phnNumber,'TOTP','secretPIN') fivePaisaClient.get_oauth_session("respToken")

When I try the below code I get this error "SSLError: HTTPSConnectionPool(host='dev-openapi.5paisa.com', port=443): Max retries exceeded with url: /WebVendorLogin/VLogin/Index?VendorKey=token&ResponseURL=https://www.yahoo.com (Caused by SSLError(SSLError(1, '[SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:1007)')))"

SSLError: HTTPSConnectionPool(host='dev-openapi.5paisa.com', port=443): Max retries exceeded with url: /WebVendorLogin/VLogin/Index?VendorKey=hguh&ResponseURL=https://www.yahoo.com (Caused by SSLError(SSLError(1, '[SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:1007)')))

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.

altimetrikchallenge avatar Nov 11 '23 13:11 altimetrikchallenge

@5paisa Is it possible to login without enabling TOTP?

crypt0inf0 avatar Dec 26 '23 09:12 crypt0inf0

Hi @altimetrikchallenge @crypt0inf0 . I used the TOTP method for logging in, i just did some basic test so i am not sure about the length of session. But sharing the process of successful login.

Activating TOTP

You need to activate TOTP from trade station, and copy the TOTP Secret, [which we will use to generate the 2FA password using pyotp which changes every 30 second].

Creating .env file

Create a .env file and add the values which you will find in Trade station, variable names are pretty intuitive

APP_NAME=
APP_SOURCE=
USER_ID=
PASSWORD=
USER_KEY=
ENCRYPTION_KEY=
TOTP_SECRET=
PIN=
CLIENT_ID=

[I named it ".env" and set it in gitignore, you dont want this in repo for obvious reasons, one can also store them manually in your systems environment variables or using "set APP_NAME=" cmd command]

Python code

The repo is pretty easy to follow, below is the code taking reference from it.

from py5paisa import FivePaisaClient
from dotenv import load_dotenv
import os
import pyotp

load_dotenv()
cred={
    "APP_NAME":os.getenv("APP_NAME"),
    "APP_SOURCE":os.getenv("APP_SOURCE"),
    "USER_ID":os.getenv("USER_ID"),
    "PASSWORD":os.getenv("PASSWORD"),
    "USER_KEY":os.getenv("USER_KEY"),
    "ENCRYPTION_KEY":os.getenv("ENCRYPTION_KEY") 
    }

client = FivePaisaClient(cred=cred)

totp = pyotp.TOTP(os.getenv('TOTP_SECRET'))
current_totp = totp.now()  # Generates the current TOTP

#TOTP based authentication
client.get_totp_session(os.getenv("CLIENT_ID"),
                        current_totp,
                        os.getenv("PIN"))

# Basic check if session is active
req_list_ = [{"Exch": "N", "ExchType": "C", "ScripData": "ITC_EQ"}]
print(client.fetch_market_feed_scrip(req_list_))

You will get this sort of output. 1 Hope this helps, cheers

aditya-524 avatar Jan 21 '24 05:01 aditya-524