(20241230) Accepting ICICI Breeze's auth. now.
This commit is contained in:
@@ -49,6 +49,7 @@ from utils_v2.date_time import date_time
|
||||
|
||||
# Data models:
|
||||
from utils_v2.trading.zerodha_kite.models.api_call import ZerodhaKiteApiResponse
|
||||
from utils_v2.trading.zerodha_kite.models.auth_tokens import ZerodhaKiteAuthTokens
|
||||
|
||||
# To make API calls:
|
||||
import httpx
|
||||
@@ -301,9 +302,10 @@ class AsyncZerodhaKite:
|
||||
|
||||
"""
|
||||
When the user authorizes the login flow, Zerodha's serve will send you a GET request on the callback URL that
|
||||
you set on the PI portal for your app. This callback will have, among other things, a 'request_toke'. The
|
||||
you set on the PI portal for your app. This callback will have, among other things, a 'request_token'. The
|
||||
request token is valid only for a very short period, and must be used to get a longer token called
|
||||
'access_token' for actual activities.
|
||||
'access_token' for actual activities. A checksum is needed for verification. Read about it in the official
|
||||
documentation on Kite's API docs.
|
||||
DOCUMENTATION:
|
||||
01. https://kite.trade/docs/connect/v3/user/
|
||||
:param request_token: The request token received from Zerodha when the user logs in.
|
||||
@@ -313,15 +315,45 @@ class AsyncZerodhaKite:
|
||||
self.__request_token = request_token
|
||||
hasher = Hasher()
|
||||
hasher.update(self.__api_key + request_token + self.__api_secret)
|
||||
self.__checksum = hasher.digest().decode()
|
||||
print(self.__checksum)
|
||||
self.__checksum = hasher.hexdigest()
|
||||
|
||||
# async def get_access_token(self):
|
||||
#
|
||||
# client_response = self.__get(
|
||||
# url = r"https://kite.zerodha.com/session/token",
|
||||
#
|
||||
# )
|
||||
async def generate_session(
|
||||
self,
|
||||
raise_exception = False
|
||||
) -> ZerodhaKiteAuthTokens | None:
|
||||
|
||||
"""
|
||||
Once we have the 'request_token' from Zerodha's callback, we must generate a session by fetching an access
|
||||
token. The access token will be used to perform most of the actual activities.
|
||||
DOCUMENTATION:
|
||||
01. https://kite.trade/docs/connect/v3/user/
|
||||
:return: Either the auth-token model of Zerodha, or null if the process failed.
|
||||
"""
|
||||
|
||||
# Start by assuming failure:
|
||||
session = None
|
||||
|
||||
# Try to get a session from Zerodha:
|
||||
client_response = await self.__post(
|
||||
url = r"https://api.kite.trade/session/token",
|
||||
headers = {"X-Kite-Version": "3"},
|
||||
data = {
|
||||
"api_key": self.__api_key,
|
||||
"request_token": self.__request_token,
|
||||
"checksum": self.__checksum
|
||||
}
|
||||
)
|
||||
|
||||
# If the API call was successful, we have a valid session:
|
||||
if client_response.success:
|
||||
client_json = await client_response.get_json()
|
||||
|
||||
self.__access_token = client_json["data"]["access_token"]
|
||||
self.__
|
||||
|
||||
print(client_response.to_markdown())
|
||||
print("CLIENT RESPONSE;", json.to_string(await client_response.get_json()))
|
||||
print(json.to_string(client_response.model_dump(), default = str))
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
@@ -349,5 +381,6 @@ if __name__ == "__main__":
|
||||
# Login flow:
|
||||
print("LOGIN URL:", my_kite.login_url)
|
||||
my_kite.set_request_token(input("Request Token: "))
|
||||
await my_kite.get_access_token()
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
Reference in New Issue
Block a user