-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth_utils.py
More file actions
69 lines (62 loc) · 2.24 KB
/
Copy pathauth_utils.py
File metadata and controls
69 lines (62 loc) · 2.24 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
import requests
import json
import os
from dotenv import load_dotenv
# Load variables from .env file if it exists
load_dotenv()
def get_access_token():
"""
Programmatically obtains a Google OAuth 2.0 Access Token using a Refresh Token.
Returns the token string on success, or a dictionary with 'error' and 'details' on failure.
"""
client_id = os.getenv("GOOGLE_CLIENT_ID")
client_secret = os.getenv("GOOGLE_CLIENT_SECRET")
refresh_token = os.getenv("GOOGLE_REFRESH_TOKEN")
if not all([client_id, client_secret, refresh_token]):
missing = []
if not client_id: missing.append("GOOGLE_CLIENT_ID")
if not client_secret: missing.append("GOOGLE_CLIENT_SECRET")
if not refresh_token: missing.append("GOOGLE_REFRESH_TOKEN")
return {
"error": "Missing credentials",
"details": f"Missing environment variables: {', '.join(missing)}"
}
url = "https://oauth2.googleapis.com/token"
payload = {
"client_id": client_id,
"client_secret": client_secret,
"refresh_token": refresh_token,
"grant_type": "refresh_token"
}
try:
response = requests.post(url, data=payload, timeout=10)
if response.status_code != 200:
try:
err_data = response.json()
details = err_data.get("error_description", response.text)
except:
details = response.text
return {
"error": "Token Refresh Failed",
"details": f"Google API returned {response.status_code}: {details}"
}
tokens = response.json()
return tokens.get("access_token")
except requests.exceptions.RequestException as e:
return {
"error": "Network Error",
"details": f"Could not connect to Google OAuth server: {str(e)}"
}
except Exception as e:
return {
"error": "Internal Error",
"details": str(e)
}
if __name__ == "__main__":
# If run directly, it prints the token
token = get_access_token()
if isinstance(token, dict):
print(f"Error: {token['error']}")
print(token['details'])
else:
print(f"Access Token: {token}")