From 6124bc5f72b9d2c6c3af62169bae5140be8f3f15 Mon Sep 17 00:00:00 2001 From: msramalho <19508417+msramalho@users.noreply.github.com> Date: Mon, 25 Jul 2022 14:52:50 +0100 Subject: [PATCH] refactored and simplified obtaining credentials --- .gitignore | 3 ++- configs/config.py | 4 ++-- create_update_test_oauth_token.py | 22 +++++++++------------- example.config.yaml | 17 ++++++++++------- storages/gd_storage.py | 6 +++--- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 62a5815..8da75c3 100644 --- a/.gitignore +++ b/.gitignore @@ -17,5 +17,6 @@ config-*.yaml logs/* local_archive/ vk_config*.json - +gd-token.json +credentials.json secrets/* \ No newline at end of file diff --git a/configs/config.py b/configs/config.py index 2298c51..1169048 100644 --- a/configs/config.py +++ b/configs/config.py @@ -117,8 +117,8 @@ class Config: gd = secrets["google_drive"] self.gd_config = GDConfig( root_folder_id=gd.get("root_folder_id"), - oauth_token_file_path_and_name=gd.get("oauth_token_file_path_and_name"), - service_account=gd.get("service_account") + oauth_token_filename=gd.get("oauth_token_filename"), + service_account=gd.get("service_account", GDConfig.service_account) ) if "local" in secrets: diff --git a/create_update_test_oauth_token.py b/create_update_test_oauth_token.py index cfe2709..65b3086 100644 --- a/create_update_test_oauth_token.py +++ b/create_update_test_oauth_token.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import os.path from google.auth.transport.requests import Request @@ -8,23 +6,20 @@ from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError -from googleapiclient.http import MediaFileUpload - -# If creating for first time download the json `credentials.json` from https://console.cloud.google.com/apis/credentials OAuth 2.0 Client IDs +# If creating for first time download the OAuth Client Ids json `credentials.json` from https://console.cloud.google.com/apis/credentials OAuth 2.0 Client IDs +# add "http://localhost:55192/" to the list of "Authorised redirect URIs" # https://davemateer.com/2022/04/28/google-drive-with-python for more information -# Can run this code to get a new token and verify the token is the correct user -# and it will refresh the token accordingly +# You can run this code to get a new token and verify it belongs to the correct user +# This token will be refresh automatically by the auto-archiver # Code below from https://developers.google.com/drive/api/quickstart/python SCOPES = ['https://www.googleapis.com/auth/drive'] + def main(): - # token_file = 'gd-token.json' - - token_file = 'secrets/token-davemateer-gmail.json' - + token_file = 'gd-token.json' creds = None # The file token.json stores the user's access and refresh tokens, and is @@ -42,7 +37,7 @@ def main(): print('First run through so putting up login dialog') # credentials.json downloaded from https://console.cloud.google.com/apis/credentials flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES) - creds = flow.run_local_server(port=0) + creds = flow.run_local_server(port=55192) # Save the credentials for the next run with open(token_file, 'w') as token: print('Saving new token') @@ -73,5 +68,6 @@ def main(): except HttpError as error: print(f'An error occurred: {error}') + if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/example.config.yaml b/example.config.yaml index 60753fa..dc78803 100644 --- a/example.config.yaml +++ b/example.config.yaml @@ -18,15 +18,18 @@ secrets: # needed if you use storage=gd google_drive: - # 1.service account to write to google storage - be aware of 15GB limit. Recommend using OAuth user. - # filename can be the same or different file from google_sheets.service_account + # To authenticate with google you have two options (1. service account OR 2. OAuth token) + + # 1. service account - storage space will count towards the developer account + # filename can be the same or different file from google_sheets.service_account, defaults to "service_account.json" # service_account: "service_account.json" - # 2.token (only 1. or 2. - if both specified then this 2. token takes precedence) - # will need to have write access on the server so refresh flow works - # run the file `create_update_test_oauth_token.py` to create the token and save in a secrets directory so - # it is not checked into source control - oauth_token_file_path_and_name: "secrets/token-davemateer-gmail.json" + # 2. OAuth token - storage space will count towards the owner of the GDrive folder + # (only 1. or 2. - if both specified then this 2. takes precedence) + # needs write access on the server so refresh flow works + # To get the token, run the file `create_update_test_oauth_token.py` + # you can edit that file if you want a different token filename, default is "gd-token.json" + oauth_token_filename: "gd-token.json" root_folder_id: copy XXXX from https://drive.google.com/drive/folders/XXXX diff --git a/storages/gd_storage.py b/storages/gd_storage.py index e60e37f..be12625 100644 --- a/storages/gd_storage.py +++ b/storages/gd_storage.py @@ -14,8 +14,8 @@ from google.auth.transport.requests import Request @dataclass class GDConfig: root_folder_id: str - oauth_token_file_path_and_name: str - service_account: str + oauth_token_filename: str + service_account: str = "service_account.json" folder: str = "default" class GDStorage(Storage): @@ -25,7 +25,7 @@ class GDStorage(Storage): SCOPES=['https://www.googleapis.com/auth/drive'] - token_file = config.oauth_token_file_path_and_name + token_file = config.oauth_token_filename if token_file is not None: """ Tokens are refreshed after 1 hour