File size: 1,752 Bytes
914e970
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
"""One-time Gmail OAuth setup for the Gmail MCP proxy.

Usage:
  python scripts/gmail_mcp_auth.py --client-secret ~/Downloads/client_secret.json

Steps to get client_secret.json:
  1. Go to https://console.cloud.google.com/apis/credentials
  2. Create OAuth 2.0 Client ID → Desktop app
  3. Download JSON → rename to client_secret.json
  4. Run this script and sign in via browser
"""

import argparse
import json
from pathlib import Path

from google_auth_oauthlib.flow import InstalledAppFlow

SCOPES = [
    "https://www.googleapis.com/auth/gmail.modify",
    "https://www.googleapis.com/auth/gmail.readonly",
    "https://www.googleapis.com/auth/gmail.send",
]

CREDS_DIR = Path.home() / ".config" / "infj_bot"
CREDS_PATH = CREDS_DIR / "gmail_credentials.json"


def main() -> int:
    p = argparse.ArgumentParser(description="Authenticate Gmail for MCP proxy")
    p.add_argument(
        "--client-secret", required=True, help="Path to Google OAuth client_secret.json"
    )
    args = p.parse_args()

    CREDS_DIR.mkdir(parents=True, exist_ok=True)

    # Run OAuth flow
    flow = InstalledAppFlow.from_client_secrets_file(args.client_secret, SCOPES)
    creds = flow.run_local_server(port=0)

    # Save credentials
    creds_data = {
        "token": creds.token,
        "refresh_token": creds.refresh_token,
        "token_uri": creds.token_uri,
        "client_id": creds.client_id,
        "client_secret": creds.client_secret,
        "scopes": creds.scopes,
    }
    CREDS_PATH.write_text(json.dumps(creds_data, indent=2))
    print(f"✅ Gmail credentials saved to: {CREDS_PATH}")
    print("   You can now run the Gmail MCP server.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())