Files
DevOps Engineer 08bb0d51b2 feat(provisioning): add Gmail OAuth/App Password guide and helper scripts
Add README.md with decision matrix, step-by-step provisioning flows,
security and compliance checklist, and rollback plan.

Add helper scripts:
- oauth_token_exchange.py: obtains and outputs a refresh token
- gmail_health_check.py: validates API or IMAP connectivity

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-29 21:23:34 +00:00

48 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""
Exchange an authorization code for a refresh token for Gmail readonly scope.
Requires: pip install google-auth-oauthlib
Usage:
export GMAIL_CLIENT_SECRET_PATH=/path/to/client_secret.json
python3 oauth_token_exchange.py
"""
import os
import sys
SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]
def main():
secret_path = os.getenv("GMAIL_CLIENT_SECRET_PATH")
if not secret_path:
print("Error: Set GMAIL_CLIENT_SECRET_PATH to the Google client_secret JSON file.", file=sys.stderr)
sys.exit(1)
try:
from google_auth_oauthlib.flow import InstalledAppFlow
except ImportError as e:
print(f"Error: google-auth-oauthlib is required. Install with: pip install google-auth-oauthlib ({e})", file=sys.stderr)
sys.exit(1)
try:
flow = InstalledAppFlow.from_client_secrets_file(secret_path, SCOPES)
credentials = flow.run_local_server(port=0)
except Exception as e:
print(f"Error during OAuth flow: {e}", file=sys.stderr)
sys.exit(2)
refresh_token = credentials.refresh_token
if not refresh_token:
print("Warning: No refresh token returned. You may need to revoke previous access and retry.", file=sys.stderr)
sys.exit(2)
print("\n=== REFRESH TOKEN ===")
print(refresh_token)
print("=====================\n")
print("Store this token securely (e.g., in a Gitea secret or vault). Do not commit it.")
sys.exit(0)
if __name__ == "__main__":
main()