diff --git a/.github/workflows/auto-share-file.yml b/.github/workflows/auto-share-file.yml index 4542bf4..ffc1e10 100644 --- a/.github/workflows/auto-share-file.yml +++ b/.github/workflows/auto-share-file.yml @@ -1,7 +1,7 @@ name: Auto Share File in Google Drive run-name: ${{ github.actor }} is sharing file by using crontab 🚀 on: - push +# push # schedule: # - cron: '0 16 * * *' diff --git a/.gitignore b/.gitignore index 7fd01ea..9201c02 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,13 @@ .DS_Store Sharely/files_information.py -OAuth_client_ID_credentials/* -!OAuth_client_ID_credentials/.gitkeep +OAuth_client_ID_credentials_desktop/* +!OAuth_client_ID_credentials_desktop/.gitkeep +OAuth_client_ID_credentials_webapp/* +!OAuth_client_ID_credentials_webapp/.gitkeep Service_account_credentials/* !Service_account_credentials/.gitkeep data/* !data/.gitkeep log/* -!log/.gitkeep \ No newline at end of file +!log/.gitkeep +venv diff --git a/OAuth_client_ID_credentials_desktop/.gitkeep b/OAuth_client_ID_credentials_desktop/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Sharely/__pycache__/__init__.cpython-310.pyc b/Sharely/__pycache__/__init__.cpython-310.pyc index 2efd633..a3bb63c 100644 Binary files a/Sharely/__pycache__/__init__.cpython-310.pyc and b/Sharely/__pycache__/__init__.cpython-310.pyc differ diff --git a/Sharely/__pycache__/download_file.cpython-310.pyc b/Sharely/__pycache__/download_file.cpython-310.pyc index 3ad707d..1f61dee 100644 Binary files a/Sharely/__pycache__/download_file.cpython-310.pyc and b/Sharely/__pycache__/download_file.cpython-310.pyc differ diff --git a/Sharely/__pycache__/files_information.cpython-310.pyc b/Sharely/__pycache__/files_information.cpython-310.pyc index 6a042db..bb12c7a 100644 Binary files a/Sharely/__pycache__/files_information.cpython-310.pyc and b/Sharely/__pycache__/files_information.cpython-310.pyc differ diff --git a/Sharely/__pycache__/handle_csv.cpython-310.pyc b/Sharely/__pycache__/handle_csv.cpython-310.pyc index e62be47..f3bd931 100644 Binary files a/Sharely/__pycache__/handle_csv.cpython-310.pyc and b/Sharely/__pycache__/handle_csv.cpython-310.pyc differ diff --git a/Sharely/__pycache__/share_file.cpython-310.pyc b/Sharely/__pycache__/share_file.cpython-310.pyc index 587f492..36e75a4 100644 Binary files a/Sharely/__pycache__/share_file.cpython-310.pyc and b/Sharely/__pycache__/share_file.cpython-310.pyc differ diff --git a/Sharely/__pycache__/update_cell_color.cpython-310.pyc b/Sharely/__pycache__/update_cell_color.cpython-310.pyc index 823e34a..014b15a 100644 Binary files a/Sharely/__pycache__/update_cell_color.cpython-310.pyc and b/Sharely/__pycache__/update_cell_color.cpython-310.pyc differ diff --git a/Sharely/download_file.py b/Sharely/download_file.py index 5147fbf..33a3baf 100644 --- a/Sharely/download_file.py +++ b/Sharely/download_file.py @@ -7,21 +7,17 @@ def update_file(spreadsheet_name, spreadsheet_id, single_sheet_name): - # the requested scopes of access - scopes = ['https://www.googleapis.com/auth/drive'] +def update_file(spreadsheet_name, spreadsheet_id, single_sheet_name): # Authenticate with the Google Sheets API using the credentials - creds = Credentials.from_service_account_file( - filename=os.environ["GOOGLE_APPLICATION_CREDENTIALS"], - scopes=scopes, - ) - + creds = Credentials.from_authorized_user_file(f_i.project_path + '/OAuth_client_ID_credentials_desktop/token.json', SCOPES) sheets_service = build('sheets', 'v4', credentials=creds) # Get the sheet ID of the specified sheet name - sheets_metadata = sheets_service.spreadsheets().get( - spreadsheetId=spreadsheet_id, ranges=[], includeGridData=False - ).execute() + sheets_metadata = sheets_service\ + .spreadsheets()\ + .get(spreadsheetId=spreadsheet_id, ranges=[], includeGridData=False)\ + .execute() sheets = sheets_metadata['sheets'] sheet_id = None diff --git a/Sharely/share_file.py b/Sharely/share_file.py index 898a3c3..7e6a370 100644 --- a/Sharely/share_file.py +++ b/Sharely/share_file.py @@ -1,7 +1,12 @@ from __future__ import print_function +<<<<<<< HEAD + +from datetime import datetime, timedelta +======= import os.path from datetime import datetime, timedelta from google.oauth2.service_account import Credentials +>>>>>>> main from googleapiclient.discovery import build from googleapiclient.errors import HttpError @@ -20,15 +25,8 @@ def share_file(real_file_id, real_user, offset, max_date): TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for the application. """ - - # the requested scopes of access - scopes = ['https://www.googleapis.com/auth/drive'] - - # request for scopes with credentials, and uses the access token from Google to invoke Google API - creds = Credentials.from_service_account_file( - filename=os.environ["GOOGLE_APPLICATION_CREDENTIALS"], - scopes=scopes, - ) + # creds, _ = google.auth.default() + creds = Credentials.from_authorized_user_file(f_i.project_path + '/OAuth_client_ID_credentials_desktop/token.json', SCOPES) try: # create drive API client @@ -45,6 +43,8 @@ def callback(request_id, response, exception): print(F'Permission Id: {response.get("id")}') ids.append(response.get('id')) + # pylint: disable=maybe-no-member + if offset == -2: offset = 0 @@ -52,17 +52,13 @@ def callback(request_id, response, exception): expire_date_utc8 = current_date_utc8 + timedelta(days=1 + offset) batch = service.new_batch_http_request(callback=callback) - # user_permission = {'type': 'user', - # 'role': 'reader', - # "expirationTime": f"{expire_date_utc8.strftime('%Y-%m-%d')}T23:59:59+08:00:00", - # 'emailAddress': real_user - # } # See availability about setting an expiration date for file access in: # https://workspaceupdates.googleblog.com/2022/10/expiring-access-controls-google-drive.html # https://stackoverflow.com/questions/75423531/google-service-account-drive-api-unable-to-set-expirationtime-python user_permission = {'type': 'user', 'role': 'reader', + "expirationTime": f"{expire_date_utc8.strftime('%Y-%m-%d')}T23:59:59+08:00:00", 'emailAddress': real_user } diff --git a/Sharely/update_cell_color.py b/Sharely/update_cell_color.py index db8a955..e23c420 100644 --- a/Sharely/update_cell_color.py +++ b/Sharely/update_cell_color.py @@ -1,27 +1,30 @@ from google.oauth2.service_account import Credentials from googleapiclient.discovery import build from googleapiclient.errors import HttpError -import os.path +from Sharely import files_information as f_i +# 設置API憑證 +SERVICE_ACCOUNT_FILE = f_i.project_path + '/token.json' +SCOPES = ['https://www.googleapis.com/auth/drive'] -# 獲取內部的Sheet ID -def get_sheet_id(sheet_name, spreadsheet_id): +# Google Sheet信息 +RANGE_NAME = 'A1:Z150' # 例如:'Sheet1' + +# 建立憑證 +credentials = Credentials.from_authorized_user_file(f_i.project_path + '/OAuth_client_ID_credentials_desktop/token.json', + SCOPES) - # the requested scopes of access - scopes = ['https://www.googleapis.com/auth/drive'] +# 建立Google Sheets API客戶端 +sheets_service = discovery.build('sheets', 'v4', credentials=credentials) - # Authenticate with the Google Sheets API using the credentials - creds = Credentials.from_service_account_file( - filename=os.environ["GOOGLE_APPLICATION_CREDENTIALS"], - scopes=scopes, - ) +# 獲取今日日期 +today = datetime.datetime.now().strftime('%-m/%-d') - # 建立Google Sheets API客戶端 - sheets_service = build('sheets', 'v4', credentials=creds) - sheets_metadata = sheets_service.spreadsheets().get( - spreadsheetId=spreadsheet_id, fields='sheets(properties)' - ).execute() +# 獲取內部的Sheet ID +def get_sheet_id(sheet_name, spreadsheet_id): + sheets_metadata = sheets_service.spreadsheets().get(spreadsheetId=spreadsheet_id, + fields='sheets(properties)').execute() sheets = sheets_metadata.get('sheets', '') sheet_id = None for sheet in sheets: @@ -33,19 +36,6 @@ def get_sheet_id(sheet_name, spreadsheet_id): # 更新Google Sheet單元格顏色 def update_cell_color(row, col, red, green, blue, spreadsheet_id, sheet_name): - - # the requested scopes of access - scopes = ['https://www.googleapis.com/auth/drive'] - - # Authenticate with the Google Sheets API using the credentials - creds = Credentials.from_service_account_file( - filename=os.environ["GOOGLE_APPLICATION_CREDENTIALS"], - scopes=scopes, - ) - - # 建立Google Sheets API客戶端 - sheets_service = build('sheets', 'v4', credentials=creds) - sheet_id = get_sheet_id(sheet_name, spreadsheet_id) body = { "requests": [ @@ -86,19 +76,6 @@ def update_cell_color(row, col, red, green, blue, spreadsheet_id, sheet_name): # 更新Google Sheet單元格顏色(update boundary color) def update_cells_color(row, col, red, green, blue, spreadsheet_id, sheet_name): - - # the requested scopes of access - scopes = ['https://www.googleapis.com/auth/drive'] - - # Authenticate with the Google Sheets API using the credentials - creds = Credentials.from_service_account_file( - filename=os.environ["GOOGLE_APPLICATION_CREDENTIALS"], - scopes=scopes, - ) - - # 建立Google Sheets API客戶端 - sheets_service = build('sheets', 'v4', credentials=creds) - sheet_id = get_sheet_id(sheet_name, spreadsheet_id) # Prepare a list of cell values with the desired background color @@ -132,27 +109,18 @@ def update_cells_color(row, col, red, green, blue, spreadsheet_id, sheet_name): # 讀取單元格顏色 def get_cell_color(row, col, spreadsheet_id): - - # the requested scopes of access - scopes = ['https://www.googleapis.com/auth/drive'] - - # Authenticate with the Google Sheets API using the credentials - creds = Credentials.from_service_account_file( - filename=os.environ["GOOGLE_APPLICATION_CREDENTIALS"], - scopes=scopes, - ) - - # 建立Google Sheets API客戶端 - sheets_service = build('sheets', 'v4', credentials=creds) - - # Google Sheet信息 - range_name = 'A1:Z150' # 例如:'Sheet1' - - request = sheets_service.spreadsheets().get( - spreadsheetId=spreadsheet_id, ranges=range_name, - fields='sheets(data(rowData(values(userEnteredFormat(backgroundColor)))))' - ) - + request = sheets_service.spreadsheets().get(spreadsheetId=spreadsheet_id, ranges=RANGE_NAME, + fields='sheets(' + 'data(' + 'rowData(' + 'values(' + 'userEnteredFormat(' + 'backgroundColor' + ')' + ')' + ')' + ')' + ')') result = request.execute() sheet_data = result['sheets'][0]['data'][0]['rowData'][row]['values'][col] diff --git a/main.py b/main.py index f67d8dc..0102e7b 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ from Sharely import update_cell_color as up_color from Sharely import files_information as f_i from datetime import datetime, timedelta -import pandas +import time # Get time @@ -16,35 +16,90 @@ # Update local csv files for index, spreadsheet_name in enumerate(f_i.spreadsheet_id_dic): - download_file.update_file( - spreadsheet_name, f_i.spreadsheet_id_dic.get(spreadsheet_name), f_i.single_sheet_namelist[index] - ) - -# Access remote csv files -pandas.set_option("display.max_rows", 100, "display.max_columns", 20, ) -data_L5 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L5.csv", sep=",") -# data_L6 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L6.csv", sep=",") + if index in f_i.to_download: + download_file.update_file( + spreadsheet_name, f_i.spreadsheet_id_dic.get(spreadsheet_name), f_i.single_sheet_namelist[index] + ) + +# Access csv files +pandas.set_option("display.max_rows", 150, "display.max_columns", 20, ) +# 週三班 +# data_L7 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L7.csv", sep=",") +# data_L8 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L8.csv", sep=",") # data_L9 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L9.csv", sep=",") +# data_L10 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L10.csv", sep=",") +# data_L11 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L11.csv", sep=",") +data_L0 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L0.csv", sep=",") +data_L1 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L1.csv", sep=",") + +# 週一班 +# data_L11 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L11.csv", sep=",") + +# 週二班 # data_L14 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L14.csv", sep=",") # data_L1 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L1.csv", sep=",") +# data_L2 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L2.csv", sep=",") +# data_L3 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L3.csv", sep=",") +# data_L4 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L4.csv", sep=",") +# data_L5 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L5.csv", sep=",") +data_L6 = pandas.read_csv(filepath_or_buffer=f_i.project_path + "/data/L6.csv", sep=",") + # Update cells (fill color in sheet boundary) -up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L5.csv"), f_i.single_sheet_namelist[1]) -# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L6.csv"), f_i.single_sheet_namelist[2]) -# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L9.csv"), f_i.single_sheet_namelist[3]) -# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L14.csv"), f_i.single_sheet_namelist[8]) -# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L1.csv"), f_i.single_sheet_namelist[9]) +# 週三班 +# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L8.csv"), f_i.single_sheet_namelist[0]) +# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L9.csv"), f_i.single_sheet_namelist[0]) +# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L10.csv"), f_i.single_sheet_namelist[1]) +# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L11.csv"), f_i.single_sheet_namelist[0]) +up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L0.csv"), f_i.single_sheet_namelist[0]) +up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L1.csv"), f_i.single_sheet_namelist[1]) + +# 週一班 +# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L11.csv"), f_i.single_sheet_namelist[]) + +# 週二班 +# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L14.csv"), f_i.single_sheet_namelist[]) +# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L1.csv"), f_i.single_sheet_namelist[]) +# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L2.csv"), f_i.single_sheet_namelist[4]) +# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L3.csv"), f_i.single_sheet_namelist[3]) +# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L4.csv"), f_i.single_sheet_namelist[2]) +# up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L5.csv"), f_i.single_sheet_namelist[3]) +up_color.update_cells_color(0, 23, 0.8, 0.8, 0.8, f_i.spreadsheet_id_dic.get("L6.csv"), f_i.single_sheet_namelist[2]) + # Send mail -handle_csv.send_mail(data_L5, f_i.L5_video_id_dic, f_i.spreadsheet_id_dic.get("L5.csv"), f_i.single_sheet_namelist[1]) -# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<|") +# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L06 is below") # handle_csv.send_mail(data_L6, f_i.L6_video_id_dic, f_i.spreadsheet_id_dic.get("L6.csv"), f_i.single_sheet_namelist[2]) -# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<|") -# handle_csv.send_mail(data_L9, f_i.L9_video_id_dic, f_i.spreadsheet_id_dic.get("L9.csv"), f_i.single_sheet_namelist[3]) -# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<|") -# handle_csv.send_mail(data_L14, f_i.L14_video_id_dic, f_i.spreadsheet_id_dic.get("L14.csv"), f_i.single_sheet_namelist[8]) -# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<|") -# handle_csv.send_mail(data_L1, f_i.L1_video_id_dic, f_i.spreadsheet_id_dic.get("L1.csv"), f_i.single_sheet_namelist[9]) +# time.sleep(3) +# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L08 is below") +# handle_csv.send_mail(data_L8, f_i.L8_video_id_dic, f_i.spreadsheet_id_dic.get("L8.csv"), f_i.single_sheet_namelist[0]) +# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L09 is below") +# handle_csv.send_mail(data_L9, f_i.L9_video_id_dic, f_i.spreadsheet_id_dic.get("L9.csv"), f_i.single_sheet_namelist[0]) +# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L10 is below") +# handle_csv.send_mail(data_L10, f_i.L10_video_id_dic, f_i.spreadsheet_id_dic.get("L10.csv"), f_i.single_sheet_namelist[1]) +# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L11 is below") +# handle_csv.send_mail(data_L11, f_i.L11_video_id_dic, f_i.spreadsheet_id_dic.get("L11.csv"), f_i.single_sheet_namelist[0]) +print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L00 is below") +handle_csv.send_mail(data_L0, f_i.L0_video_id_dic, f_i.spreadsheet_id_dic.get("L0.csv"), f_i.single_sheet_namelist[0]) +print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L01 is below") +handle_csv.send_mail(data_L1, f_i.L1_video_id_dic, f_i.spreadsheet_id_dic.get("L1.csv"), f_i.single_sheet_namelist[1]) + +# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L14 is below") +# handle_csv.send_mail(data_L14, f_i.L14_video_id_dic, f_i.spreadsheet_id_dic.get("L14.csv"), f_i.single_sheet_namelist[]) +# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L01 is below") +# handle_csv.send_mail(data_L1, f_i.L1_video_id_dic, f_i.spreadsheet_id_dic.get("L1.csv"), f_i.single_sheet_namelist[]) +time.sleep(5) +# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L02 is below") +# handle_csv.send_mail(data_L2, f_i.L2_video_id_dic, f_i.spreadsheet_id_dic.get("L2.csv"), f_i.single_sheet_namelist[4]) +# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L03 is below") +# handle_csv.send_mail(data_L3, f_i.L3_video_id_dic, f_i.spreadsheet_id_dic.get("L3.csv"), f_i.single_sheet_namelist[3]) +# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L04 is below") +# handle_csv.send_mail(data_L4, f_i.L4_video_id_dic, f_i.spreadsheet_id_dic.get("L4.csv"), f_i.single_sheet_namelist[2]) +# time.sleep(5) +# print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L05 is below") +# handle_csv.send_mail(data_L5, f_i.L5_video_id_dic, f_i.spreadsheet_id_dic.get("L5.csv"), f_i.single_sheet_namelist[3]) +print("@.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o< @.@ *_* >o<| L06 is below") +handle_csv.send_mail(data_L6, f_i.L6_video_id_dic, f_i.spreadsheet_id_dic.get("L6.csv"), f_i.single_sheet_namelist[2]) print("~End~====================================================================================================", today_str) diff --git a/test/list_filename.py b/test/list_filename.py index 555aebd..b5aa9dd 100644 --- a/test/list_filename.py +++ b/test/list_filename.py @@ -14,12 +14,23 @@ def main(): """Shows basic usage of the Drive v3 API. Prints the names and ids of the first 10 files the user has access to. """ - - # 使用服務帳戶憑證 - creds = Credentials.from_service_account_file( - filename=os.environ["GOOGLE_APPLICATION_CREDENTIALS"], - scopes=scopes, - ) + creds = None + # The file token.json stores the user's access and refresh tokens, and is + # created automatically when the authorization flow completes for the first + # time. + if os.path.exists('../OAuth_client_ID_credentials_desktop/token.json'): + creds = Credentials.from_authorized_user_file('../OAuth_client_ID_credentials_desktop/token.json', SCOPES) + # If there are no (valid) credentials available, let the user log in. + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + '../OAuth_client_ID_credentials_desktop/credentials.json', SCOPES) + creds = flow.run_local_server(port=0) + # Save the credentials for the next run + with open('../OAuth_client_ID_credentials_desktop/token.json', 'w') as token: + token.write(creds.to_json()) try: service = build('drive', 'v3', credentials=creds)