Mobii23 commited on
Commit
f05338f
·
verified ·
1 Parent(s): 7e7f419

Update backend/google_utils.py

Browse files
Files changed (1) hide show
  1. backend/google_utils.py +18 -18
backend/google_utils.py CHANGED
@@ -5,38 +5,40 @@ from googleapiclient.discovery import build
5
  import json
6
  import os
7
 
8
- # Configuration
9
- CLIENT_SECRETS_FILE = "backend/client_secret.json"
10
  SCOPES = [
11
- "https://www.googleapis.com/auth/drive", # <-- CHANGED: Upgraded from metadata.readonly
12
  "https://www.googleapis.com/auth/userinfo.email",
13
  "https://www.googleapis.com/auth/userinfo.profile",
14
  "openid"
15
  ]
16
 
17
- REDIRECT_URI = "http://localhost:5173/google-callback"
 
 
 
 
 
 
 
 
18
 
19
  def get_client_id_from_file():
20
- if not os.path.exists(CLIENT_SECRETS_FILE):
21
- return "MISSING_CLIENT_SECRET"
22
- with open(CLIENT_SECRETS_FILE, 'r') as f:
23
- data = json.load(f)
24
- return data['web']['client_id']
25
 
26
  def get_client_secret_from_file():
27
- if not os.path.exists(CLIENT_SECRETS_FILE):
28
- return "MISSING_CLIENT_SECRET"
29
- with open(CLIENT_SECRETS_FILE, 'r') as f:
30
- data = json.load(f)
31
- return data['web']['client_secret']
32
 
33
  def get_google_auth_url():
34
- flow = Flow.from_client_secrets_file(CLIENT_SECRETS_FILE, scopes=SCOPES, redirect_uri=REDIRECT_URI)
 
35
  auth_url, _ = flow.authorization_url(prompt='consent', access_type='offline')
36
  return auth_url
37
 
38
  def exchange_code_for_token(auth_code):
39
- flow = Flow.from_client_secrets_file(CLIENT_SECRETS_FILE, scopes=SCOPES, redirect_uri=REDIRECT_URI)
 
40
  flow.fetch_token(code=auth_code)
41
  creds = flow.credentials
42
  service = build('oauth2', 'v2', credentials=creds)
@@ -60,12 +62,10 @@ def get_drive_service(access_token, refresh_token, token_uri, client_id, client_
60
  return build('drive', 'v3', credentials=creds)
61
 
62
  def list_files_in_folder(service, folder_id="root"):
63
- # --- FIX: REMOVED TRY/EXCEPT SO MAIN.PY CAN CATCH ERRORS ---
64
  query = f"'{folder_id}' in parents and trashed = false"
65
  results = service.files().list(
66
  q=query,
67
  pageSize=100,
68
- # <-- CHANGED: Added size and md5Checksum to the fields list
69
  fields="nextPageToken, files(id, name, mimeType, iconLink, webViewLink, size, md5Checksum)",
70
  orderBy="folder, name"
71
  ).execute()
 
5
  import json
6
  import os
7
 
 
 
8
  SCOPES = [
9
+ "https://www.googleapis.com/auth/drive",
10
  "https://www.googleapis.com/auth/userinfo.email",
11
  "https://www.googleapis.com/auth/userinfo.profile",
12
  "openid"
13
  ]
14
 
15
+ REDIRECT_URI = "https://docusort.vercel.app/google-callback"
16
+
17
+ def get_google_config():
18
+ """Securely load Google keys from Hugging Face Environment Secrets"""
19
+ secret_str = os.getenv("GOOGLE_CLIENT_SECRET_JSON")
20
+ if not secret_str:
21
+ print("WARNING: GOOGLE_CLIENT_SECRET_JSON not found in environment!")
22
+ return None
23
+ return json.loads(secret_str)
24
 
25
  def get_client_id_from_file():
26
+ config = get_google_config()
27
+ return config['web']['client_id'] if config else "MISSING"
 
 
 
28
 
29
  def get_client_secret_from_file():
30
+ config = get_google_config()
31
+ return config['web']['client_secret'] if config else "MISSING"
 
 
 
32
 
33
  def get_google_auth_url():
34
+ config = get_google_config()
35
+ flow = Flow.from_client_config(config, scopes=SCOPES, redirect_uri=REDIRECT_URI)
36
  auth_url, _ = flow.authorization_url(prompt='consent', access_type='offline')
37
  return auth_url
38
 
39
  def exchange_code_for_token(auth_code):
40
+ config = get_google_config()
41
+ flow = Flow.from_client_config(config, scopes=SCOPES, redirect_uri=REDIRECT_URI)
42
  flow.fetch_token(code=auth_code)
43
  creds = flow.credentials
44
  service = build('oauth2', 'v2', credentials=creds)
 
62
  return build('drive', 'v3', credentials=creds)
63
 
64
  def list_files_in_folder(service, folder_id="root"):
 
65
  query = f"'{folder_id}' in parents and trashed = false"
66
  results = service.files().list(
67
  q=query,
68
  pageSize=100,
 
69
  fields="nextPageToken, files(id, name, mimeType, iconLink, webViewLink, size, md5Checksum)",
70
  orderBy="folder, name"
71
  ).execute()