kopia lustrzana https://github.com/bellingcat/auto-archiver
refactored and simplified obtaining credentials
rodzic
524b40b869
commit
6124bc5f72
|
@ -17,5 +17,6 @@ config-*.yaml
|
|||
logs/*
|
||||
local_archive/
|
||||
vk_config*.json
|
||||
|
||||
gd-token.json
|
||||
credentials.json
|
||||
secrets/*
|
|
@ -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:
|
||||
|
|
|
@ -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()
|
||||
main()
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Ładowanie…
Reference in New Issue