(20241122) Port no. adjusted.
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
# Use this to run the microservice without any docker setup.
|
# Use this to run the microservice without any docker setup.
|
||||||
source .venv/bin/activate
|
source .venv/bin/activate
|
||||||
python3 "$(pwd)/api/main.py" --host "127.0.0.1" --port 5108 --workers 4 --script-id "kps_cnv_KBC3MoaU" &
|
python3 "$(pwd)/api/main.py" --host "127.0.0.1" --port 5106 --workers 4 --script-id "kps_cnv_KBC3MoaU" &
|
||||||
deactivate
|
deactivate
|
||||||
|
|
||||||
# All done:
|
# All done:
|
||||||
|
|||||||
@@ -0,0 +1,128 @@
|
|||||||
|
"""
|
||||||
|
|
||||||
|
AUTHOR:
|
||||||
|
|
||||||
|
Khushal P Soonderji
|
||||||
|
|
||||||
|
DATE:
|
||||||
|
|
||||||
|
Friday, 22nd Nov., 2024
|
||||||
|
|
||||||
|
OBJECTIVE:
|
||||||
|
|
||||||
|
To provide a structured way to handle OAuth2.0 behaviour for various services.
|
||||||
|
|
||||||
|
REFERENCES:
|
||||||
|
|
||||||
|
N/A
|
||||||
|
|
||||||
|
DOWNLOADS:
|
||||||
|
|
||||||
|
N/A
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** IMPORT ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
# To make sibling directories accessible for imports:
|
||||||
|
import sys
|
||||||
|
sys.path.append(".")
|
||||||
|
sys.path.append("..")
|
||||||
|
|
||||||
|
# System-level activities:
|
||||||
|
import io
|
||||||
|
|
||||||
|
# For defining the class's structure:
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
# For debugging:
|
||||||
|
from icecream import IceCreamDebugger
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** MACROS / ONE-TIME INIT ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
# --- Nothing Yet
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** VARIABLES ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
# --- Nothing Yet
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** FUNCTIONS ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
# --- Nothing Yet
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** CLASSES ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
class OAuthBase(ABC):
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config: dict,
|
||||||
|
redirect_url: str,
|
||||||
|
debug = True,
|
||||||
|
debug_prefix = "OAuth | ",
|
||||||
|
debug_only_errors = True
|
||||||
|
):
|
||||||
|
|
||||||
|
# Prepare the debugging utility:
|
||||||
|
self._debug_prefix = debug_prefix
|
||||||
|
self._printer = IceCreamDebugger(prefix = debug_prefix, includeContext = True)
|
||||||
|
if not debug: self._printer.disable()
|
||||||
|
self._debug_only_errors = debug_only_errors
|
||||||
|
|
||||||
|
# Accept the input config:
|
||||||
|
self._config = config
|
||||||
|
self._redirect_url = redirect_url
|
||||||
|
|
||||||
|
def enable_debug(self):
|
||||||
|
self._printer.enable()
|
||||||
|
|
||||||
|
def disable_debug(self):
|
||||||
|
self._printer.disable()
|
||||||
|
|
||||||
|
def debug_only_errors(self):
|
||||||
|
self._debug_only_errors = True
|
||||||
|
|
||||||
|
def debug_everything(self):
|
||||||
|
self._debug_only_errors = False
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** MAIN PROGRAM ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
pass
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
"""
|
||||||
|
|
||||||
|
AUTHOR:
|
||||||
|
|
||||||
|
Khushal P Soonderji
|
||||||
|
|
||||||
|
DATE:
|
||||||
|
|
||||||
|
Friday, 22nd Nov., 2024
|
||||||
|
|
||||||
|
OBJECTIVE:
|
||||||
|
|
||||||
|
To handle OAuth2.0 activities for Google's services.
|
||||||
|
|
||||||
|
REFERENCES:
|
||||||
|
|
||||||
|
1. https://developers.google.com/calendar/api/quickstart/python
|
||||||
|
|
||||||
|
DOWNLOADS:
|
||||||
|
|
||||||
|
N/A
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** IMPORT ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
# To make sibling directories accessible for imports:
|
||||||
|
import sys
|
||||||
|
sys.path.append(".")
|
||||||
|
sys.path.append("..")
|
||||||
|
|
||||||
|
# System-level activities:
|
||||||
|
import io
|
||||||
|
|
||||||
|
# My utils:
|
||||||
|
from utils_v2.string import json
|
||||||
|
|
||||||
|
# The base model:
|
||||||
|
from utils_v2.oauth.base import OAuthBase
|
||||||
|
|
||||||
|
# Related to Google:
|
||||||
|
from google.auth.transport.requests import Request
|
||||||
|
from google.oauth2.credentials import Credentials
|
||||||
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||||
|
|
||||||
|
# For asynchronous activities:
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** MACROS / ONE-TIME INIT ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
# Google Scopes:
|
||||||
|
SCOPES_GMAIL_MAIL_MANAGEMENT = [
|
||||||
|
r"https://www.googleapis.com/auth/gmail.modify",
|
||||||
|
r"https://www.googleapis.com/auth/gmail.labels"
|
||||||
|
]
|
||||||
|
SCOPES_GMAIL_FULL = [r"https://mail.google.com/"]
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** VARIABLES ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
# --- Nothing Yet
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** FUNCTIONS ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
# --- Nothing Yet
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** CLASSES ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
class GoogleOAuth(OAuthBase):
|
||||||
|
|
||||||
|
# Class variables:
|
||||||
|
__flow = None
|
||||||
|
|
||||||
|
async def init(
|
||||||
|
self,
|
||||||
|
scopes = None
|
||||||
|
):
|
||||||
|
|
||||||
|
# Initialize your Google App:
|
||||||
|
self._printer("Initializing flow.")
|
||||||
|
self.__flow = InstalledAppFlow.from_client_config(
|
||||||
|
self._config,
|
||||||
|
scopes = scopes or SCOPES_GMAIL_MAIL_MANAGEMENT,
|
||||||
|
redirect_uri = self._redirect_url
|
||||||
|
)
|
||||||
|
|
||||||
|
async def get_authorization_url(self):
|
||||||
|
|
||||||
|
authorization_url, state = self.__flow.authorization_url(
|
||||||
|
access_type = "offline",
|
||||||
|
include_granted_scopes = "true"
|
||||||
|
)
|
||||||
|
|
||||||
|
return authorization_url
|
||||||
|
|
||||||
|
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
# ***** ****
|
||||||
|
# *** MAIN PROGRAM ***
|
||||||
|
# ***** ****
|
||||||
|
# *****************************************************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
secrets_file = r"../../../creds/google_converse_test_oauth.json"
|
||||||
|
secrets_dict = json.from_file(secrets_file)
|
||||||
|
|
||||||
|
my_goog = GoogleOAuth(
|
||||||
|
config = secrets_dict,
|
||||||
|
# redirect_url = r"https://v2.api.bicree.com/user/callback/test",
|
||||||
|
redirect_url = r"127.0.0.1",
|
||||||
|
debug = True,
|
||||||
|
debug_prefix = "OAuth (Goog) | "
|
||||||
|
)
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
|
||||||
|
await my_goog.init()
|
||||||
|
print("AUTH URL:", await my_goog.get_authorization_url())
|
||||||
|
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
Reference in New Issue
Block a user