(20241127) Google API OAuth2.0 support ready!
This commit is contained in:
+90
-5
@@ -32,6 +32,9 @@
|
||||
|
||||
# To make sibling directories accessible for imports:
|
||||
import sys
|
||||
|
||||
from utils_v2.goog.models.data.auth_tokens import GoogleAuthTokens
|
||||
|
||||
sys.path.append(".")
|
||||
sys.path.append("..")
|
||||
|
||||
@@ -47,6 +50,7 @@ from utils_v2.goog.models.data.api_call import GoogleApiResponse
|
||||
from utils_v2.mail import mail_parser
|
||||
|
||||
# Related to Google:
|
||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||
from google.auth.transport.requests import Request
|
||||
from google.oauth2.credentials import Credentials
|
||||
from googleapiclient.discovery import build
|
||||
@@ -116,9 +120,10 @@ class AsyncGoogleBase:
|
||||
def __init__(
|
||||
self,
|
||||
service_name: str,
|
||||
client_id: str,
|
||||
client_secret: str,
|
||||
oauth_json: dict,
|
||||
http_client: httpx.AsyncClient,
|
||||
scopes: List[str] = None,
|
||||
redirect_url: str = None,
|
||||
debug = True,
|
||||
debug_prefix = "GMail | ",
|
||||
debug_only_errors = True
|
||||
@@ -128,8 +133,10 @@ class AsyncGoogleBase:
|
||||
To initialize any Google API from one base class. The client's id and secret are available in the file
|
||||
downloaded form https://console.cloud.google.com/apis/credentials (do not forget to select your app).
|
||||
:param service_name: A string to identify this service.
|
||||
:param client_id: From the OAuth JSON downloaded from
|
||||
:param oauth_json: The OAuth credentials downloaded from https://console.cloud.google.com/apis/credentials
|
||||
:param http_client: An asynchronous HTTP client to make API calls.
|
||||
:param redirect_url: Where you would like to receive the confirmation of the user authorization.
|
||||
:param scopes: The list of permissions needed for this particular authorization.
|
||||
:param debug: Whether, or not, you would like to show debugging messages on the terminal.
|
||||
:param debug_prefix: The prefix string to identify the debugging messages.
|
||||
:param debug_only_errors: Whether you would like to show all debugging messages or just error messages.
|
||||
@@ -143,9 +150,16 @@ class AsyncGoogleBase:
|
||||
|
||||
# Accept the input configuration:
|
||||
self._service_name = service_name
|
||||
self._client_id = client_id
|
||||
self._client_secret = client_secret
|
||||
self._http_client = http_client
|
||||
self._oauth_json = oauth_json
|
||||
self._client_id = self._oauth_json["web"]["client_id"]
|
||||
self._client_secret = self._oauth_json["web"]["client_secret"]
|
||||
self._redirect_url = redirect_url
|
||||
self._flow = InstalledAppFlow.from_client_config(
|
||||
self._oauth_json,
|
||||
scopes = scopes,
|
||||
redirect_uri = self._redirect_url
|
||||
)
|
||||
|
||||
def enable_debug(self):
|
||||
self._printer.enable()
|
||||
@@ -159,6 +173,76 @@ class AsyncGoogleBase:
|
||||
def debug_everything(self):
|
||||
self._debug_only_errors = False
|
||||
|
||||
# ┏┓┏┓ ┓ ┏┓ ┏┓
|
||||
# ┃┃┣┫┓┏╋┣┓ ┏┛ ┃┫
|
||||
# ┗┛┛┗┗┻┗┛┗ ┗━•┗┛
|
||||
|
||||
async def get_authorization_url(
|
||||
self,
|
||||
state: str = None,
|
||||
access_type: Literal["online", "offline"] = "offline",
|
||||
approval_prompt: Literal["auto", "force", "consent"] = "force",
|
||||
include_granted_scopes: Literal["true", "false"] = "true",
|
||||
user_email: str = None
|
||||
) -> str:
|
||||
|
||||
"""
|
||||
TO get the OAuth2.0 authorization URL for one user.
|
||||
DOCUMENTATION:
|
||||
1. https://developers.google.com/identity/protocols/oauth2/web-server
|
||||
:param state: A unique identifier for your user. If not supplied, a random string will be generated.
|
||||
:param access_type: Set the value to offline if your application needs to refresh access tokens when the user is
|
||||
not present at the browser.
|
||||
:param approval_prompt: "force" ensures that the consent screen is always shown to the user, regardless of
|
||||
whether the user has previously granted consent for the requested scopes. It forces the user to re-approve
|
||||
the app’s access, which can be useful if the app is requesting new permissions or if the consent needs to be
|
||||
explicitly confirmed. "consent" ensures the user’s consent is required if they haven't approved the app’s
|
||||
requested permissions yet. "auto" allows Google to automatically determine whether the consent screen should
|
||||
be shown.
|
||||
:param include_granted_scopes: Enables applications to use incremental authorization to request access to
|
||||
additional scopes in context. If you set this parameter's value to true and the authorization request is
|
||||
granted, then the new access token will also cover any scopes to which the user previously granted the
|
||||
application access.
|
||||
:param user_email:
|
||||
:return:
|
||||
"""
|
||||
|
||||
# Get an authorization URL:
|
||||
auth_url, state = self._flow.authorization_url(
|
||||
access_type = access_type,
|
||||
approval_prompt = approval_prompt,
|
||||
include_granted_scopes = include_granted_scopes,
|
||||
login_hint = user_email,
|
||||
state = state
|
||||
)
|
||||
|
||||
# Done here:
|
||||
return auth_url
|
||||
|
||||
async def get_authorization_tokens(
|
||||
self,
|
||||
redirect_url: str
|
||||
) -> GoogleAuthTokens:
|
||||
|
||||
"""
|
||||
When the user accepts or declines an authorization request, Google sends you an alert on your redirect URL. Pass
|
||||
the URL as it is to this method to generate the authorization tokens that you can store in the database and
|
||||
reuse for this user's activities.
|
||||
:param redirect_url: The exact URL that was hit (with the query params) that Google hit when the user did
|
||||
something on your authorization URL. Fortunately, this URL is readily available in Quart and Flask by
|
||||
calling 'request.url'.
|
||||
:return: The authorization tokens.
|
||||
"""
|
||||
|
||||
credentials = self._flow.fetch_token(authorization_response = redirect_url)
|
||||
ttl = credentials["expires_in"] - 60
|
||||
return GoogleAuthTokens(
|
||||
accessToken = credentials.get("access_token"),
|
||||
refreshToken = credentials.get("refresh_token"),
|
||||
expiresAt = date_time.get_current_utc_date_time() + datetime.timedelta(seconds = ttl),
|
||||
scopes = credentials.get("scope")
|
||||
)
|
||||
|
||||
# ┏┓ ┳┓ ┓•
|
||||
# ┣ ┏┓┏┓┏┓┏┓ ┃┃┏┓┏┏┓┏┫┓┏┓┏┓
|
||||
# ┗┛┛ ┛ ┗┛┛ ┻┛┗ ┗┗┛┗┻┗┛┗┗┫
|
||||
@@ -242,6 +326,7 @@ class AsyncGoogleBase:
|
||||
:param headers: The headers to pass.
|
||||
:param json: The params to send in the JSON body.
|
||||
:param data: The params to send in the form-data in the body.
|
||||
:param content: The raw content to be sent in the body (typically as an octet-stream).
|
||||
:return: A structured response that includes the raw response, the exception (if any), and so on.
|
||||
"""
|
||||
|
||||
|
||||
@@ -34,6 +34,8 @@
|
||||
|
||||
# To make sibling directories accessible for imports:
|
||||
import sys
|
||||
from http.client import responses
|
||||
|
||||
sys.path.append(".")
|
||||
sys.path.append("..")
|
||||
|
||||
@@ -47,7 +49,6 @@ from utils_v2.date_time import date_time
|
||||
from utils_v2.mail import mail_parser
|
||||
|
||||
# My Google utils:
|
||||
from utils_v2.oauth.services.goog import GoogleOAuth
|
||||
from utils_v2.goog.base import AsyncGoogleBase
|
||||
from utils_v2.goog.models.data.auth_tokens import GoogleAuthTokens
|
||||
from utils_v2.goog.models.data.api_call import GoogleApiResponse
|
||||
@@ -88,7 +89,12 @@ import base64
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
# 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/"]
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
@@ -618,7 +624,7 @@ class AsyncGMailClient(AsyncGoogleBase):
|
||||
if api_response.httpCode in [200]:
|
||||
api_response.success = True
|
||||
api_json = await api_response.get_json()
|
||||
raw_message = base64.urlsafe_b64decode(api_json["raw"])
|
||||
raw_message = base64.urlsafe_b64decode(api_json["raw"]).decode()
|
||||
if return_raw: api_response.data = raw_message
|
||||
else:
|
||||
parsed_message = mail_parser.parse(raw_message)
|
||||
@@ -897,19 +903,33 @@ if __name__ == "__main__":
|
||||
# Create an instance of the client:
|
||||
my_gmail = AsyncGMailClient(
|
||||
service_name = "gmail",
|
||||
client_id = secrets_dict["web"]["client_id"],
|
||||
client_secret = secrets_dict["web"]["client_secret"],
|
||||
oauth_json = secrets_dict,
|
||||
http_client = test_client,
|
||||
redirect_url = r"https://api.thecaoffice.com/converse/mail/callback/gmail",
|
||||
scopes = [
|
||||
r"https://www.googleapis.com/auth/gmail.modify",
|
||||
r"https://www.googleapis.com/auth/gmail.labels"
|
||||
],
|
||||
debug = True,
|
||||
debug_prefix = "GMail (M) | ",
|
||||
debug_only_errors = False
|
||||
)
|
||||
|
||||
# Request Auth:
|
||||
# print("AUTH URL:", await my_gmail.get_authorization_url(
|
||||
# state = "User123-Bhopli"
|
||||
# ))
|
||||
|
||||
# Get tokens from callback:
|
||||
# print("TOKENS:", await my_gmail.get_authorization_tokens(
|
||||
# redirect_url = r"https://api.thecaoffice.com/converse/mail/callback/gmail?state=User123-Bhopli..."
|
||||
# ))
|
||||
|
||||
# Create a sample mail:
|
||||
my_mail = GMailMessage(
|
||||
from_email = "pskhushal@gmail.com",
|
||||
to_email = "orangebhopli@gmail.com",
|
||||
subject = "Re: Bhopli is the best! (Thread Test)",
|
||||
subject = "Bhopli is the best! (Parts Sequence Test)",
|
||||
cc_emails = None,
|
||||
bcc_emails = None
|
||||
)
|
||||
@@ -936,7 +956,7 @@ if __name__ == "__main__":
|
||||
</html>
|
||||
"""
|
||||
)
|
||||
my_mail.add_text("This is how you should pet her 👇")
|
||||
my_mail.add_text("(3rd part - text) This is how you should pet her 👇")
|
||||
my_mail.add_inline_image(r"../../../data/images/cat_petting.png")
|
||||
my_mail.add_attachment(r"../../../data/pdf/sample_label.pdf")
|
||||
# print(my_mail.get_raw_message(as_base64 = False))
|
||||
@@ -945,7 +965,7 @@ if __name__ == "__main__":
|
||||
response = await my_gmail.send_message(
|
||||
tokens = test_tokens,
|
||||
message = my_mail,
|
||||
thread_id = "1936caffb67996d3"
|
||||
thread_id = None
|
||||
)
|
||||
print("SUCCESS:", response.success)
|
||||
print("SUMMARY:", response.to_markdown())
|
||||
|
||||
@@ -180,12 +180,19 @@ class GMailMessage:
|
||||
|
||||
self.message.attach(MIMEText(html_text, "html"))
|
||||
|
||||
def add_inline_image(self, image_file, content_id = None):
|
||||
def add_inline_image(
|
||||
self,
|
||||
image_file: str | io.BytesIO,
|
||||
file_name: str = None,
|
||||
content_id: str = None
|
||||
):
|
||||
|
||||
"""
|
||||
Add an inline image to the body of the mail.
|
||||
NOTE: This is NOT the same as sending an image as an attachment.
|
||||
:param image_file: The image data to attach to the mail body.
|
||||
:param file_name: The name of the file. This is the same name by which it will be downloaded. You need not
|
||||
specify this if the input file is specified as a path. Needed when you give the input file as a buffer.
|
||||
:param content_id: Inline images are inserted via HTML bocks. This field identifies the image resource. If not
|
||||
specified, I will generate a random string. You may write a custom value here if you know what you are
|
||||
doing. For most use cases, please ignore this field.
|
||||
@@ -195,6 +202,7 @@ class GMailMessage:
|
||||
# Read the image as bytes:
|
||||
image_bytes = None
|
||||
if type(image_file) is str:
|
||||
file_name = file_name or os.path.split(image_file)[-1]
|
||||
with open(image_file, "rb") as opened_image_file:
|
||||
image_bytes = opened_image_file.read()
|
||||
if type(image_file) is io.BytesIO:
|
||||
@@ -217,7 +225,13 @@ class GMailMessage:
|
||||
|
||||
# Then add the image:
|
||||
image_part = MIMEImage(image_bytes)
|
||||
image_part.add_header("Content-ID", f"<{content_id}>")
|
||||
image_part.add_header(
|
||||
"Content-ID", f"<{content_id}>"
|
||||
)
|
||||
image_part.add_header(
|
||||
"Content-Disposition",
|
||||
f"inline; filename=\"{file_name}\"",
|
||||
)
|
||||
self.message.attach(image_part)
|
||||
|
||||
def add_attachment(
|
||||
@@ -253,7 +267,7 @@ class GMailMessage:
|
||||
encoders.encode_base64(part)
|
||||
part.add_header(
|
||||
"Content-Disposition",
|
||||
f"attachment; filename= {file_name}",
|
||||
f"attachment; filename=\"{file_name}\"",
|
||||
)
|
||||
self.message.attach(part)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user