Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get Response Token using code for Login? #265

Open
altimetrikchallenge opened this issue Nov 11, 2023 · 2 comments
Open

How to get Response Token using code for Login? #265

altimetrikchallenge opened this issue Nov 11, 2023 · 2 comments

Comments

@altimetrikchallenge
Copy link

altimetrikchallenge commented Nov 11, 2023

  • 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=&ResponseURL=

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 altimetrikchallenge changed the title How Do you guys Login? How to get Response Token using code for Login? Nov 15, 2023
@crypt0inf0
Copy link

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

@aditya-524
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants