CheddahBot/scripts/google_auth_setup.py

68 lines
1.8 KiB
Python

"""One-time Google OAuth2 setup script.
Usage:
uv run python scripts/google_auth_setup.py
uv run python scripts/google_auth_setup.py --test-drive FOLDER_ID
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from dotenv import load_dotenv
load_dotenv()
from cheddahbot.google_auth import get_credentials # noqa: E402
def main():
parser = argparse.ArgumentParser(description="Google OAuth2 setup for CheddahBot")
parser.add_argument(
"--test-drive",
metavar="FOLDER_ID",
help="Test Drive access by listing files in the given folder",
)
args = parser.parse_args()
print("Authenticating with Google (browser will open on first run)...")
creds = get_credentials()
print("Authentication successful. Token saved to data/google_token.json")
# Show granted scopes
if hasattr(creds, "scopes") and creds.scopes:
print("\nGranted scopes:")
for scope in creds.scopes:
print(" - %s" % scope)
if args.test_drive:
from googleapiclient.discovery import build
service = build("drive", "v3", credentials=creds)
results = (
service.files()
.list(
q="'%s' in parents and trashed = false" % args.test_drive,
fields="files(id, name, mimeType)",
pageSize=10,
)
.execute()
)
files = results.get("files", [])
if files:
print("\nFiles in folder %s:" % args.test_drive)
for f in files:
print(" - %s (%s)" % (f["name"], f["mimeType"]))
else:
print("\nFolder %s is empty or not accessible." % args.test_drive)
print("\nSetup complete.")
if __name__ == "__main__":
main()