(20250704) - Created Oauth Authentication for google places api with all files

This commit is contained in:
yatmesh
2025-07-04 17:34:01 +05:30
parent c0c5b8aa58
commit 5f493f0ba9
22 changed files with 2750 additions and 4 deletions
+70 -2
View File
@@ -247,13 +247,27 @@ class AsyncGoogleBase:
# Get the credentials:
credentials = flow.fetch_token(authorization_response = redirect_url)
ttl = credentials["expires_in"] - 60
return GoogleAuthTokens(
tokens = GoogleAuthTokens(
accessToken = credentials["access_token"],
refreshToken = credentials["refresh_token"],
expiresAt = date_time.get_current_utc_date_time() + datetime.timedelta(seconds = ttl),
scopes = credentials["scope"]
)
# Fetch the user's info:
if not self._debug_only_errors: self._printer("Getting User Profile.")
api_response = await self.get(
url = f"https://openidconnect.googleapis.com/v1/userinfo",
headers = {"Authorization": f"Bearer {tokens.accessToken}"}
)
if api_response.httpCode in [200]:
api_response_json = await api_response.get_json()
tokens.displayName = api_response_json.get("name")
tokens.displayPictureUrl = api_response_json.get("picture")
# Done here:
return tokens
# ┏┓ ┳┓ ┓•
# ┣ ┏┓┏┓┏┓┏┓ ┃┃┏┓┏┏┓┏┫┓┏┓┏┓
# ┗┛┛ ┛ ┗┛┛ ┻┛┗ ┗┗┛┗┻┗┛┗┗┫
@@ -476,4 +490,58 @@ class AsyncGoogleBase:
if __name__ == "__main__":
pass
from utils_v2.string import json
import asyncio
# Create an HTTP client:
test_client = httpx.AsyncClient(
limits = httpx.Limits(
max_connections = 100, # ............ Maximum number of connections allowed in the pool.
max_keepalive_connections = 50, # ... Maximum number of connections that can be kept alive.
),
timeout = httpx.Timeout(
connect = 2.5, # ... Shorter connection timeout.
read = 2.5, # ...... Like what EasyEcom gives.
write = 10.0, # .... Time to wait for sending data.
pool = 120.0 # ..... Time to wait for a free connection from the pool.
)
)
# Define some scopes that the app will use:
TEST_SCOPES = [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/userinfo.profile"
]
# Read the secrets that give you access to the app:
secrets_file = r"C:\Users\Khushal P Soonderji\Downloads\google_places_test_secret.json"
secrets_dict = json.from_file(secrets_file)
async def main():
# Create an instance of the client:
my_google = AsyncGoogleBase(
service_name = "Goog",
oauth_json = secrets_dict,
http_client = test_client,
redirect_url = r"https://wtt.ditscentre.in/shopify/test/1",
debug = True,
debug_prefix = "Goog (C) | ",
debug_only_errors = False
)
# Request Auth:
print("AUTH URL:", await my_google.get_authorization_url(
scopes = TEST_SCOPES,
state = "Bhopli",
approval_prompt = "force"
))
# Get tokens from callback:
test_tokens = await my_google.get_authorization_tokens(
scopes = TEST_SCOPES,
redirect_url = input("Paste the redirect URL here: ")
)
print("TOKENS:", json.to_string(test_tokens.model_dump(), default = str))
asyncio.run(main())