(20241204) Updated Gmail client and Nimbus client.

This commit is contained in:
2024-12-04 13:03:51 +05:30
parent 293f753381
commit e51a6de5fc
4 changed files with 28 additions and 63 deletions
+16 -60
View File
@@ -140,6 +140,7 @@ class AsyncGMailClient(AsyncGoogleBase):
DOCUMENTATION:
1. https://developers.google.com/gmail/api/reference/rest/v1/users/getProfile
2. https://developers.google.com/people/api/rest/v1/people/get
3. https://developers.google.com/people/api/rest/v1/people#Person
:param tokens: The object that holds the access token to the service.
:param user_id: The id of the user for which this service must be run. Typically, the e-mail id itself or "me".
:return: A structured response where the list of labels will be in the 'data' variable.
@@ -164,11 +165,13 @@ class AsyncGMailClient(AsyncGoogleBase):
if gmail_api_response.httpCode in [200]:
gmail_api_response.success = True
gmail_api_response.data = await gmail_api_response.get_json()
gmail_api_response.data["displayName"] = None
gmail_api_response.data["displayPictureUrl"] = None
# Make the People API call:
if not self._debug_only_errors: self._printer("Getting User Profile.")
people_api_response = await self.get(
url = f"https://people.googleapis.com/v1/people/me",
url = f"https://people.googleapis.com/v1/people/me?personFields=names,photos,birthdays,phoneNumbers,genders,emailAddresses,addresses",
headers = {"Authorization": f"Bearer {tokens.accessToken}"}
)
@@ -176,10 +179,15 @@ class AsyncGMailClient(AsyncGoogleBase):
if people_api_response.httpCode in [200]:
people_api_response.success = True
people_api_response.data = await people_api_response.get_json()
for k, v in gmail_api_response.data.items(): people_api_response.data[k] = v
for item in people_api_response.data.get("names", []):
if item["metadata"]["primary"]:
gmail_api_response.data["displayName"] = item.get("displayName")
for item in people_api_response.data.get("photos", []):
if item["metadata"]["primary"]:
gmail_api_response.data["displayPictureUrl"] = item.get("url")
# Done here:
return people_api_response
return gmail_api_response
# ┓ ┓ ┓
# ┃ ┏┓┣┓┏┓┃┏
@@ -911,11 +919,6 @@ if __name__ == "__main__":
secrets_file = r"../../../creds/goog/app/google_tcaoff_test_oauth_20241125.json"
secrets_dict = json.from_file(secrets_file)
# Read the user's tokens that will give you app access to that user's account:
tokens_file = r"../../../creds/goog/user/test_user_tokens_20241125.json"
tokens_dict = json.from_file(tokens_file)
test_tokens = GoogleAuthTokens(**tokens_dict)
async def main():
# Create an instance of the client:
@@ -937,61 +940,14 @@ if __name__ == "__main__":
))
# 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 = "Bhopli is the best! (Parts Sequence Test)",
cc_emails = None,
bcc_emails = None
test_tokens = await my_gmail.get_authorization_tokens(
scopes = SCOPES_GMAIL_MAIL_MANAGEMENT,
redirect_url = input("Paste the redirect URL here: ")
)
my_mail.add_html(
"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sample HTML String</title>
<style>
.heading {
color: #ff9025;
}
.sub-heading {
color: #000000;
}
</style>
</head>
<body>
<h1 class="heading">Hello, Bhopli!</h1>
<h2 class="sub-heading">Bhopli is the best, most well-behaved cat in the known universe.</h2>
</body>
</html>
"""
)
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))
print("TOKENS:", test_tokens)
# Test some feature:
# auth_url = await my_gmail.get_authorization_url(
# state = "1234567890",
# scopes = SCOPES_GMAIL_MAIL_MANAGEMENT,
# approval_prompt = "force",
# user_email = "pskhushal@gmail.com"
# )
# print("AUTH URL:", auth_url)
# response = await my_gmail.list_labels(
# tokens = test_tokens,
# )
# response = await my_gmail.get_message(
# tokens = test_tokens,
# message_id = "1936bfbfc912b86f"
# )
response = await my_gmail.get_user_profile(tokens = test_tokens)
print("SUCCESS:", response.success)
print("SUMMARY:", response.to_markdown())
print("\n\n---\n\n")