Prathamesh Sarjerao Vaidya commited on
Commit
3424009
·
1 Parent(s): d4481e9

update main.yml & check.yml with OAuth

Browse files
.github/workflows/check.yml CHANGED
@@ -124,20 +124,27 @@ jobs:
124
  cat > upload_to_drive.py << 'EOF'
125
  import os
126
  import json
127
- from google.oauth2 import service_account
 
128
  from googleapiclient.discovery import build
129
  from googleapiclient.http import MediaFileUpload
130
  import mimetypes
131
 
132
- # Load credentials from environment
133
- credentials_json = os.environ['GOOGLE_CREDENTIALS']
134
- credentials_info = json.loads(credentials_json)
135
- credentials = service_account.Credentials.from_service_account_info(credentials_info)
 
 
 
 
 
 
136
 
137
  # Build the Drive service
138
  service = build('drive', 'v3', credentials=credentials)
139
 
140
- # Target folder ID - IMPORTANT: This must be the shared folder ID
141
  FOLDER_ID = '1-8HJcWxsUUQIj9OMXQeoeULS06RA9Hg9'
142
 
143
  def get_mime_type(file_path):
@@ -163,10 +170,10 @@ jobs:
163
  ).execute()
164
  print(f'Updated: {file_name} (ID: {updated_file.get("id")})')
165
  else:
166
- # Create new file - CRITICAL: Must specify parent folder
167
  file_metadata = {
168
  'name': file_name,
169
- 'parents': [parent_folder_id] # This line is crucial!
170
  }
171
  file = drive_service.files().create(
172
  body=file_metadata,
@@ -243,8 +250,7 @@ jobs:
243
  test_results = service.files().list(q=test_query, pageSize=1).execute()
244
  print(f"Successfully accessed folder. Found {len(test_results.get('files', []))} items (showing 1 max)")
245
  except Exception as e:
246
- print(f"ERROR: Cannot access folder {FOLDER_ID}. Make sure the service account has Editor access to this folder.")
247
- print(f"Service account email should be in the folder sharing settings.")
248
  exit(1)
249
 
250
  # Upload all files to Google Drive (excluding MD files)
@@ -254,4 +260,5 @@ jobs:
254
  print("Upload completed - MD files were skipped, PDFs were uploaded!")
255
  EOF
256
 
257
- python upload_to_drive.py
 
 
124
  cat > upload_to_drive.py << 'EOF'
125
  import os
126
  import json
127
+ from google.oauth2.credentials import Credentials
128
+ from google.auth.transport.requests import Request
129
  from googleapiclient.discovery import build
130
  from googleapiclient.http import MediaFileUpload
131
  import mimetypes
132
 
133
+ # Load OAuth credentials from environment
134
+ oauth_token_json = os.environ['GOOGLE_OAUTH_TOKEN']
135
+ token_info = json.loads(oauth_token_json)
136
+
137
+ # Create credentials from the token info
138
+ credentials = Credentials.from_authorized_user_info(token_info)
139
+
140
+ # Refresh the token if needed
141
+ if credentials.expired and credentials.refresh_token:
142
+ credentials.refresh(Request())
143
 
144
  # Build the Drive service
145
  service = build('drive', 'v3', credentials=credentials)
146
 
147
+ # Target folder ID - This is where files will be uploaded
148
  FOLDER_ID = '1-8HJcWxsUUQIj9OMXQeoeULS06RA9Hg9'
149
 
150
  def get_mime_type(file_path):
 
170
  ).execute()
171
  print(f'Updated: {file_name} (ID: {updated_file.get("id")})')
172
  else:
173
+ # Create new file
174
  file_metadata = {
175
  'name': file_name,
176
+ 'parents': [parent_folder_id]
177
  }
178
  file = drive_service.files().create(
179
  body=file_metadata,
 
250
  test_results = service.files().list(q=test_query, pageSize=1).execute()
251
  print(f"Successfully accessed folder. Found {len(test_results.get('files', []))} items (showing 1 max)")
252
  except Exception as e:
253
+ print(f"ERROR: Cannot access folder {FOLDER_ID}. Error: {e}")
 
254
  exit(1)
255
 
256
  # Upload all files to Google Drive (excluding MD files)
 
260
  print("Upload completed - MD files were skipped, PDFs were uploaded!")
261
  EOF
262
 
263
+ python upload_to_drive.py
264
+
.github/workflows/main.yml CHANGED
@@ -113,20 +113,27 @@ jobs:
113
  cat > upload_to_drive.py << 'EOF'
114
  import os
115
  import json
116
- from google.oauth2 import service_account
 
117
  from googleapiclient.discovery import build
118
  from googleapiclient.http import MediaFileUpload
119
  import mimetypes
120
 
121
- # Load credentials from environment
122
- credentials_json = os.environ['GOOGLE_CREDENTIALS']
123
- credentials_info = json.loads(credentials_json)
124
- credentials = service_account.Credentials.from_service_account_info(credentials_info)
 
 
 
 
 
 
125
 
126
  # Build the Drive service
127
  service = build('drive', 'v3', credentials=credentials)
128
 
129
- # Target folder ID - IMPORTANT: This must be the shared folder ID
130
  FOLDER_ID = '1-8HJcWxsUUQIj9OMXQeoeULS06RA9Hg9'
131
 
132
  def get_mime_type(file_path):
@@ -152,10 +159,10 @@ jobs:
152
  ).execute()
153
  print(f'Updated: {file_name} (ID: {updated_file.get("id")})')
154
  else:
155
- # Create new file - CRITICAL: Must specify parent folder
156
  file_metadata = {
157
  'name': file_name,
158
- 'parents': [parent_folder_id] # This line is crucial!
159
  }
160
  file = drive_service.files().create(
161
  body=file_metadata,
@@ -232,8 +239,7 @@ jobs:
232
  test_results = service.files().list(q=test_query, pageSize=1).execute()
233
  print(f"Successfully accessed folder. Found {len(test_results.get('files', []))} items (showing 1 max)")
234
  except Exception as e:
235
- print(f"ERROR: Cannot access folder {FOLDER_ID}. Make sure the service account has Editor access to this folder.")
236
- print(f"Service account email should be in the folder sharing settings.")
237
  exit(1)
238
 
239
  # Upload all files to Google Drive (excluding MD files)
 
113
  cat > upload_to_drive.py << 'EOF'
114
  import os
115
  import json
116
+ from google.oauth2.credentials import Credentials
117
+ from google.auth.transport.requests import Request
118
  from googleapiclient.discovery import build
119
  from googleapiclient.http import MediaFileUpload
120
  import mimetypes
121
 
122
+ # Load OAuth credentials from environment
123
+ oauth_token_json = os.environ['GOOGLE_OAUTH_TOKEN']
124
+ token_info = json.loads(oauth_token_json)
125
+
126
+ # Create credentials from the token info
127
+ credentials = Credentials.from_authorized_user_info(token_info)
128
+
129
+ # Refresh the token if needed
130
+ if credentials.expired and credentials.refresh_token:
131
+ credentials.refresh(Request())
132
 
133
  # Build the Drive service
134
  service = build('drive', 'v3', credentials=credentials)
135
 
136
+ # Target folder ID - This is where files will be uploaded
137
  FOLDER_ID = '1-8HJcWxsUUQIj9OMXQeoeULS06RA9Hg9'
138
 
139
  def get_mime_type(file_path):
 
159
  ).execute()
160
  print(f'Updated: {file_name} (ID: {updated_file.get("id")})')
161
  else:
162
+ # Create new file
163
  file_metadata = {
164
  'name': file_name,
165
+ 'parents': [parent_folder_id]
166
  }
167
  file = drive_service.files().create(
168
  body=file_metadata,
 
239
  test_results = service.files().list(q=test_query, pageSize=1).execute()
240
  print(f"Successfully accessed folder. Found {len(test_results.get('files', []))} items (showing 1 max)")
241
  except Exception as e:
242
+ print(f"ERROR: Cannot access folder {FOLDER_ID}. Error: {e}")
 
243
  exit(1)
244
 
245
  # Upload all files to Google Drive (excluding MD files)