bibibi12345 commited on
Commit
16ff901
·
verified ·
1 Parent(s): ce03c87

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +4 -21
app/main.py CHANGED
@@ -179,39 +179,25 @@ def init_vertex_ai():
179
  credentials_json_str = os.environ.get("GOOGLE_CREDENTIALS_JSON")
180
  if credentials_json_str:
181
  try:
182
- print(f"Found GOOGLE_CREDENTIALS_JSON environment variable, length: {len(credentials_json_str)} characters")
183
  # Try to parse the JSON
184
  try:
185
  credentials_info = json.loads(credentials_json_str)
186
- print(f"Successfully parsed JSON from GOOGLE_CREDENTIALS_JSON")
187
  # Check if the parsed JSON has the expected structure
188
  if not isinstance(credentials_info, dict):
189
- print(f"ERROR: Parsed JSON is not a dictionary, type: {type(credentials_info)}")
190
  raise ValueError("Credentials JSON must be a dictionary")
191
-
192
  # Check for required fields in the service account JSON
193
  required_fields = ["type", "project_id", "private_key_id", "private_key", "client_email"]
194
  missing_fields = [field for field in required_fields if field not in credentials_info]
195
  if missing_fields:
196
- print(f"ERROR: Missing required fields in credentials JSON: {missing_fields}")
197
  raise ValueError(f"Credentials JSON missing required fields: {missing_fields}")
198
-
199
- print(f"Credentials JSON contains all required fields")
200
  except json.JSONDecodeError as json_err:
201
  print(f"ERROR: Failed to parse GOOGLE_CREDENTIALS_JSON as JSON: {json_err}")
202
- # Print a small sample of the string for debugging (avoid printing the whole thing for security)
203
- safe_sample = credentials_json_str[:20] + "..." if len(credentials_json_str) > 20 else credentials_json_str
204
- print(f"First few characters: {safe_sample}")
205
  raise
206
-
207
  # Create credentials from the parsed JSON info (json.loads should handle \n)
208
  try:
209
- # DEBUG: Log snippet of private key before passing to library
210
- pk_value = credentials_info.get("private_key", "KEY_NOT_FOUND")
211
- if pk_value != "KEY_NOT_FOUND":
212
- print(f"DEBUG: Snippet of private_key passed to from_service_account_info:\n{pk_value[:30]}...\n...{pk_value[-30:]}")
213
- else:
214
- print("DEBUG: private_key not found in credentials_info dict.")
215
 
216
  credentials = service_account.Credentials.from_service_account_info(
217
  credentials_info, # Pass the dictionary directly
@@ -273,10 +259,7 @@ def init_vertex_ai():
273
  print(f"ERROR: GOOGLE_APPLICATION_CREDENTIALS file does not exist at path: {file_path}")
274
 
275
  # If none of the methods worked
276
- print(f"ERROR: No valid credentials found. Tried all methods:")
277
- print(f" 1. GOOGLE_CREDENTIALS_JSON: {'Present' if os.environ.get('GOOGLE_CREDENTIALS_JSON') else 'Not found'}")
278
- print(f" 2. Credential Manager: Directory {credential_manager.credentials_dir}, Files found: {len(credential_manager.credentials_files)}")
279
- print(f" 3. GOOGLE_APPLICATION_CREDENTIALS: {'Present' if os.environ.get('GOOGLE_APPLICATION_CREDENTIALS') else 'Not found'}")
280
  return False
281
  except Exception as e:
282
  print(f"Error initializing authentication: {e}")
 
179
  credentials_json_str = os.environ.get("GOOGLE_CREDENTIALS_JSON")
180
  if credentials_json_str:
181
  try:
 
182
  # Try to parse the JSON
183
  try:
184
  credentials_info = json.loads(credentials_json_str)
 
185
  # Check if the parsed JSON has the expected structure
186
  if not isinstance(credentials_info, dict):
187
+ # print(f"ERROR: Parsed JSON is not a dictionary, type: {type(credentials_info)}") # Removed
188
  raise ValueError("Credentials JSON must be a dictionary")
 
189
  # Check for required fields in the service account JSON
190
  required_fields = ["type", "project_id", "private_key_id", "private_key", "client_email"]
191
  missing_fields = [field for field in required_fields if field not in credentials_info]
192
  if missing_fields:
193
+ # print(f"ERROR: Missing required fields in credentials JSON: {missing_fields}") # Removed
194
  raise ValueError(f"Credentials JSON missing required fields: {missing_fields}")
 
 
195
  except json.JSONDecodeError as json_err:
196
  print(f"ERROR: Failed to parse GOOGLE_CREDENTIALS_JSON as JSON: {json_err}")
 
 
 
197
  raise
198
+
199
  # Create credentials from the parsed JSON info (json.loads should handle \n)
200
  try:
 
 
 
 
 
 
201
 
202
  credentials = service_account.Credentials.from_service_account_info(
203
  credentials_info, # Pass the dictionary directly
 
259
  print(f"ERROR: GOOGLE_APPLICATION_CREDENTIALS file does not exist at path: {file_path}")
260
 
261
  # If none of the methods worked
262
+ print(f"ERROR: No valid credentials found. Tried GOOGLE_CREDENTIALS_JSON, Credential Manager ({credential_manager.credentials_dir}), and GOOGLE_APPLICATION_CREDENTIALS.")
 
 
 
263
  return False
264
  except Exception as e:
265
  print(f"Error initializing authentication: {e}")