-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticate.py
More file actions
74 lines (62 loc) · 3.02 KB
/
Copy pathauthenticate.py
File metadata and controls
74 lines (62 loc) · 3.02 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
import os
import pickle
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
# Define the scopes for Gmail, Google Drive, and Google Calendar
SCOPES = [
'https://mail.google.com/',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/calendar'
]
# Global variables to store the authenticated services
drive_service = None
gmail_service = None
calendar_service = None
def get_token_file(email):
"""Generate a unique token file name based on the user's email."""
return f"token_{email}.json"
def authenticate_google_services(email):
"""Authenticates and returns the Google API service instances for Gmail, Drive, and Calendar."""
global drive_service # Make drive_service globally accessible
creds = None
token_file = get_token_file(email)
# Check if a token for this user already exists
if os.path.exists(token_file):
creds = Credentials.from_authorized_user_file(token_file, SCOPES)
# If credentials are invalid, expired, or lack required scopes, log the user in
if not creds or not creds.valid or not creds.has_scopes(SCOPES):
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
# Use InstalledAppFlow.from_client_secrets_file() to authenticate user
flow = InstalledAppFlow.from_client_secrets_file(
'client_secret-agents.json', SCOPES, redirect_uri='http://localhost:8080/')
creds = flow.run_local_server(port=8080)
# Save the credentials for future runs in a user-specific token file
with open(token_file, 'w') as token:
token.write(creds.to_json())
# Build the service instances for Gmail, Drive, and Calendar
gmail_service = build('gmail', 'v1', credentials=creds)
drive_service = build('drive', 'v3', credentials=creds) # Store the Drive service globally
calendar_service = build('calendar', 'v3', credentials=creds)
return gmail_service, drive_service, calendar_service
def get_drive_service():
"""Returns the globally authenticated Google Drive service."""
global drive_service
if drive_service is None:
raise ValueError("Google Drive service has not been authenticated yet. Call authenticate_google_services() first.")
return drive_service
def get_gmail_service():
"""Returns the globally authenticated Gmail service."""
global gmail_service
if gmail_service is None:
raise ValueError("Gmail service has not been authenticated yet. Call authenticate_google_services() first.")
return gmail_service
def get_calendar_service():
"""Returns the globally authenticated Google Calendar service."""
global calendar_service
if calendar_service is None:
raise ValueError("Google Calendar service has not been authenticated yet. Call authenticate_google_services() first.")
return calendar_service