3
3
import os
4
4
import posixpath
5
5
import re
6
+ import tempfile
6
7
import threading
7
8
from collections import defaultdict
8
9
from contextlib import contextmanager
@@ -208,15 +209,25 @@ def _drive(self):
208
209
from pydrive2 .auth import GoogleAuth
209
210
from pydrive2 .drive import GoogleDrive
210
211
211
- if os .getenv (GDriveFileSystem .GDRIVE_CREDENTIALS_DATA ):
212
- with open (self ._gdrive_user_credentials_path , "w" ) as cred_file :
212
+ temporary_save_path = self ._gdrive_user_credentials_path
213
+ is_credentials_temp = os .getenv (
214
+ GDriveFileSystem .GDRIVE_CREDENTIALS_DATA
215
+ )
216
+ if self ._use_service_account :
217
+ temporary_save_path = os .path .join (
218
+ tempfile .gettempdir (), "google-creds.json"
219
+ )
220
+
221
+ if is_credentials_temp :
222
+ with open (temporary_save_path , "w" ) as cred_file :
213
223
cred_file .write (
214
224
os .getenv (GDriveFileSystem .GDRIVE_CREDENTIALS_DATA )
215
225
)
216
226
217
227
auth_settings = {
218
228
"client_config_backend" : "settings" ,
219
229
"save_credentials" : True ,
230
+ "save_credentials_backend" : "file" ,
220
231
"save_credentials_file" : self ._gdrive_user_credentials_path ,
221
232
"get_refresh_token" : True ,
222
233
"oauth_scope" : [
@@ -226,13 +237,16 @@ def _drive(self):
226
237
}
227
238
228
239
if self ._use_service_account :
229
- auth_settings ["save_credentials_backend" ] = "json"
230
240
auth_settings ["service_config" ] = {
231
241
"client_user_email" : self ._service_account_user_email ,
232
242
"client_json_file_path" : self ._service_account_json_file_path ,
233
243
}
244
+ if is_credentials_temp :
245
+ auth_settings ["service_config" ][
246
+ "client_json_file_path"
247
+ ] = temporary_save_path
248
+
234
249
else :
235
- auth_settings ["save_credentials_backend" ] = "file"
236
250
auth_settings ["client_config" ] = {
237
251
"client_id" : self ._client_id or self .DEFAULT_GDRIVE_CLIENT_ID ,
238
252
"client_secret" : self ._client_secret
@@ -267,8 +281,8 @@ def _drive(self):
267
281
except Exception as exc :
268
282
raise GDriveAuthError (self .credentials_location ) from exc
269
283
finally :
270
- if os . getenv ( GDriveFileSystem . GDRIVE_CREDENTIALS_DATA ) :
271
- os .remove (self . _gdrive_user_credentials_path )
284
+ if is_credentials_temp :
285
+ os .remove (temporary_save_path )
272
286
273
287
return GoogleDrive (gauth )
274
288
0 commit comments