-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathauth.py
More file actions
83 lines (69 loc) · 3.25 KB
/
auth.py
File metadata and controls
83 lines (69 loc) · 3.25 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
"""
Google Drive authentication and service initialization.
Handles OAuth2 authentication flow and API service setup.
"""
import sys
from pathlib import Path
from typing import Optional
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from config import OCRConfig
from logger import OCRLogger
class GoogleDriveAuth:
"""Handles Google Drive API authentication and service initialization."""
def __init__(self, config: OCRConfig, flags: Optional[object] = None):
self.config = config
# Kept for backward compatibility with existing call sites.
self.flags = flags
self.logger = OCRLogger(enable_file_logging=config.enable_file_logging)
self.service = None
def _get_scopes(self) -> list[str]:
"""Normalize scope config to a list accepted by Google auth flow."""
return self.config.scopes if isinstance(self.config.scopes, list) else [self.config.scopes]
def get_credentials(self) -> Credentials:
"""Get valid user credentials from storage with improved error handling."""
current_directory = Path.cwd()
token_path = current_directory / 'token.json'
scopes = self._get_scopes()
credentials = None
if token_path.exists():
credentials = Credentials.from_authorized_user_file(str(token_path), scopes)
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
credentials_file = Path(self.config.credentials_file)
if not credentials_file.exists():
raise FileNotFoundError(
f"Credentials file '{credentials_file}' not found. "
f"Please ensure you have downloaded the credentials from Google Cloud Console."
)
flow = InstalledAppFlow.from_client_secrets_file(str(credentials_file), scopes)
credentials = flow.run_local_server(port=0)
token_path.write_text(credentials.to_json(), encoding='utf-8')
self.logger.success(f'Credentials stored to {token_path}')
return credentials
def initialize_service(self):
"""Initialize Google Drive API service with comprehensive error handling."""
try:
credentials = self.get_credentials()
self.service = build('drive', 'v3', credentials=credentials)
if self.config.verbose:
self.logger.success("Google Drive API service initialized successfully")
return self.service
except FileNotFoundError as e:
self.logger.error(str(e))
sys.exit(1)
except Exception as e:
self.logger.error(f"Failed to initialize Google Drive service: {e}")
sys.exit(1)
def authenticate_google_drive():
"""
Authenticate with Google Drive and return the service object.
This is a convenience function for the GUI application.
"""
config = OCRConfig()
auth = GoogleDriveAuth(config)
return auth.initialize_service()