feat(scouts): persist connected gmail_address on oauth callback
This commit is contained in:
@@ -530,6 +530,29 @@ async def scout_gmail_oauth_callback(
|
|||||||
if scout is None or scout.user_id != current_user.id:
|
if scout is None or scout.user_id != current_user.id:
|
||||||
raise HTTPException(status.HTTP_404_NOT_FOUND, "Scout not found")
|
raise HTTPException(status.HTTP_404_NOT_FOUND, "Scout not found")
|
||||||
scout.oauth_token_encrypted = encrypted
|
scout.oauth_token_encrypted = encrypted
|
||||||
|
|
||||||
|
# Fetch the connected Gmail address for display.
|
||||||
|
try:
|
||||||
|
from googleapiclient.discovery import build
|
||||||
|
from google.oauth2.credentials import Credentials
|
||||||
|
|
||||||
|
def _fetch_email() -> str | None:
|
||||||
|
creds = Credentials(
|
||||||
|
token=creds_dict["token"],
|
||||||
|
refresh_token=creds_dict.get("refresh_token"),
|
||||||
|
token_uri=creds_dict["token_uri"],
|
||||||
|
client_id=creds_dict["client_id"],
|
||||||
|
client_secret=creds_dict["client_secret"],
|
||||||
|
scopes=creds_dict["scopes"],
|
||||||
|
)
|
||||||
|
service = build("gmail", "v1", credentials=creds, cache_discovery=False)
|
||||||
|
profile = service.users().getProfile(userId="me").execute()
|
||||||
|
return profile.get("emailAddress")
|
||||||
|
|
||||||
|
scout.gmail_address = await asyncio.to_thread(_fetch_email)
|
||||||
|
except Exception:
|
||||||
|
logger.exception("failed to fetch gmail address for scout %s", scout_id)
|
||||||
|
|
||||||
await db.commit()
|
await db.commit()
|
||||||
|
|
||||||
# Attempt to set up Gmail push watch so we start receiving Pub/Sub notifications.
|
# Attempt to set up Gmail push watch so we start receiving Pub/Sub notifications.
|
||||||
|
|||||||
Reference in New Issue
Block a user