-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_readonly_token.py
More file actions
94 lines (76 loc) · 3.14 KB
/
Copy pathgenerate_readonly_token.py
File metadata and controls
94 lines (76 loc) · 3.14 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
"""
Generate a read-only OAuth token for the Product Bot.
This token can only READ data from:
- Google Drive (drive.readonly)
- Google Docs (documents.readonly)
- Gmail (gmail.readonly)
- BigQuery (bigquery - queries only, no data modification)
Usage:
python generate_readonly_token.py
The token will be saved to token.json and can be copied to GOOGLE_OAUTH_TOKEN env var.
"""
import json
import os
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
# Scopes for product bot - read-only except for creating docs
SCOPES = [
'https://www.googleapis.com/auth/drive.file', # Create/edit files created by this app only
'https://www.googleapis.com/auth/documents', # Create/edit docs (needed for write_google_docs)
'https://www.googleapis.com/auth/gmail.readonly', # Read emails only
'https://www.googleapis.com/auth/bigquery', # Run queries (read-only operations)
]
CREDENTIALS_PATH = 'credentials.json'
TOKEN_PATH = 'token.json'
def main():
print("Generating OAuth token for Product Bot...")
print()
print("This token will have access to:")
print(" ✓ Google Drive (create files, edit own files only)")
print(" ✓ Google Docs (create and edit)")
print(" ✓ Gmail (read-only)")
print(" ✓ BigQuery (query only)")
print()
print("This token CANNOT:")
print(" ✗ Delete or modify files it didn't create")
print(" ✗ Send or modify emails")
print(" ✗ Modify BigQuery tables or datasets")
print()
if not os.path.exists(CREDENTIALS_PATH):
print(f"Error: {CREDENTIALS_PATH} not found")
print("Please download OAuth credentials from Google Cloud Console")
return
# Remove existing token to force re-auth
if os.path.exists(TOKEN_PATH):
os.remove(TOKEN_PATH)
print(f"Removed existing {TOKEN_PATH}")
# Run OAuth flow
print("Opening browser for authentication...")
flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_PATH, SCOPES)
creds = flow.run_local_server(port=0)
# Save token
with open(TOKEN_PATH, 'w') as f:
f.write(creds.to_json())
print()
print(f"✓ Token saved to {TOKEN_PATH}")
print()
print("To use in a remote environment, set GOOGLE_OAUTH_TOKEN:")
print()
print(f" The token is saved at: {TOKEN_PATH}")
print(" ⚠️ WARNING: The token JSON contains live credentials (refresh_token, client_secret).")
print(" Do NOT share it, log it, or commit it. To export for a remote environment:")
print(f" cat {TOKEN_PATH}")
print(" Then set GOOGLE_OAUTH_TOKEN to that JSON value in your remote environment's secrets.")
print()
# Verify scopes
with open(TOKEN_PATH) as f:
token_data = json.load(f)
print("Token scopes:")
for scope in token_data.get('scopes', []):
readonly = 'readonly' in scope or scope.endswith('bigquery')
icon = '✓' if readonly else '⚠'
print(f" {icon} {scope}")
if __name__ == '__main__':
main()