153 lines
5.7 KiB
Python
153 lines
5.7 KiB
Python
"""
|
|
|
|
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())
|