-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexchange_code.py
More file actions
68 lines (55 loc) · 2.04 KB
/
exchange_code.py
File metadata and controls
68 lines (55 loc) · 2.04 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
#!/usr/bin/env python3
"""
Exchange OAuth authorization code for token (VPS/headless environments).
Usage:
python3 exchange_code.py <authorization_code>
After running this script, the token will be saved to credentials/token.json
"""
import sys
import json
import requests
from pathlib import Path
CREDS_FILE = Path(__file__).parent / 'credentials' / 'client_secrets.json'
TOKEN_FILE = Path(__file__).parent / 'credentials' / 'token.json'
if len(sys.argv) < 2:
print("Usage: python3 exchange_code.py <authorization_code>")
print()
print("Get the authorization code from the OAuth Playground redirect URL.")
print("Example: https://developers.google.com/oauthplayground/?code=4/0A...&scope=...")
sys.exit(1)
auth_code = sys.argv[1]
# Load client credentials
with open(CREDS_FILE) as f:
creds = json.load(f)['web']
# Exchange code for token
response = requests.post('https://oauth2.googleapis.com/token', data={
'code': auth_code,
'client_id': creds['client_id'],
'client_secret': creds['client_secret'],
'redirect_uri': 'https://developers.google.com/oauthplayground',
'grant_type': 'authorization_code'
})
if response.status_code != 200:
print(f"Error: {response.status_code}")
print(response.text)
sys.exit(1)
token_data = response.json()
# Add client credentials to token (required for refresh)
token_data['client_id'] = creds['client_id']
token_data['client_secret'] = creds['client_secret']
# Save token
TOKEN_FILE.parent.mkdir(parents=True, exist_ok=True)
with open(TOKEN_FILE, 'w') as f:
json.dump(token_data, f, indent=2)
print("=" * 80)
print("TOKEN SAVED SUCCESSFULLY")
print("=" * 80)
print()
print(f"Token file: {TOKEN_FILE}")
print(f"Scopes granted: {token_data.get('scope', 'unknown')}")
print(f"Token type: {token_data.get('token_type', 'unknown')}")
print(f"Expires in: {token_data.get('expires_in', 'unknown')} seconds (~1 hour)")
print()
print("The access token will auto-refresh when needed.")
print("Refresh token lasts 6 months of inactivity (app is published to production).")
print("=" * 80)