kopia lustrzana https://github.com/bellingcat/auto-archiver
refactored and simplified obtaining credentials
rodzic
524b40b869
commit
6124bc5f72
|
@ -17,5 +17,6 @@ config-*.yaml
|
||||||
logs/*
|
logs/*
|
||||||
local_archive/
|
local_archive/
|
||||||
vk_config*.json
|
vk_config*.json
|
||||||
|
gd-token.json
|
||||||
|
credentials.json
|
||||||
secrets/*
|
secrets/*
|
|
@ -117,8 +117,8 @@ class Config:
|
||||||
gd = secrets["google_drive"]
|
gd = secrets["google_drive"]
|
||||||
self.gd_config = GDConfig(
|
self.gd_config = GDConfig(
|
||||||
root_folder_id=gd.get("root_folder_id"),
|
root_folder_id=gd.get("root_folder_id"),
|
||||||
oauth_token_file_path_and_name=gd.get("oauth_token_file_path_and_name"),
|
oauth_token_filename=gd.get("oauth_token_filename"),
|
||||||
service_account=gd.get("service_account")
|
service_account=gd.get("service_account", GDConfig.service_account)
|
||||||
)
|
)
|
||||||
|
|
||||||
if "local" in secrets:
|
if "local" in secrets:
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
from google.auth.transport.requests import Request
|
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.discovery import build
|
||||||
from googleapiclient.errors import HttpError
|
from googleapiclient.errors import HttpError
|
||||||
|
|
||||||
from googleapiclient.http import MediaFileUpload
|
# 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"
|
||||||
# If creating for first time download the json `credentials.json` from https://console.cloud.google.com/apis/credentials OAuth 2.0 Client IDs
|
|
||||||
# https://davemateer.com/2022/04/28/google-drive-with-python for more information
|
# 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
|
# You can run this code to get a new token and verify it belongs to the correct user
|
||||||
# and it will refresh the token accordingly
|
# This token will be refresh automatically by the auto-archiver
|
||||||
|
|
||||||
# Code below from https://developers.google.com/drive/api/quickstart/python
|
# Code below from https://developers.google.com/drive/api/quickstart/python
|
||||||
|
|
||||||
SCOPES = ['https://www.googleapis.com/auth/drive']
|
SCOPES = ['https://www.googleapis.com/auth/drive']
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# token_file = 'gd-token.json'
|
token_file = 'gd-token.json'
|
||||||
|
|
||||||
token_file = 'secrets/token-davemateer-gmail.json'
|
|
||||||
|
|
||||||
creds = None
|
creds = None
|
||||||
|
|
||||||
# The file token.json stores the user's access and refresh tokens, and is
|
# 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')
|
print('First run through so putting up login dialog')
|
||||||
# credentials.json downloaded from https://console.cloud.google.com/apis/credentials
|
# credentials.json downloaded from https://console.cloud.google.com/apis/credentials
|
||||||
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
|
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
|
# Save the credentials for the next run
|
||||||
with open(token_file, 'w') as token:
|
with open(token_file, 'w') as token:
|
||||||
print('Saving new token')
|
print('Saving new token')
|
||||||
|
@ -73,5 +68,6 @@ def main():
|
||||||
except HttpError as error:
|
except HttpError as error:
|
||||||
print(f'An error occurred: {error}')
|
print(f'An error occurred: {error}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -18,15 +18,18 @@ secrets:
|
||||||
|
|
||||||
# needed if you use storage=gd
|
# needed if you use storage=gd
|
||||||
google_drive:
|
google_drive:
|
||||||
# 1.service account to write to google storage - be aware of 15GB limit. Recommend using OAuth user.
|
# To authenticate with google you have two options (1. service account OR 2. OAuth token)
|
||||||
# filename can be the same or different file from google_sheets.service_account
|
|
||||||
|
# 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"
|
# service_account: "service_account.json"
|
||||||
|
|
||||||
# 2.token (only 1. or 2. - if both specified then this 2. token takes precedence)
|
# 2. OAuth token - storage space will count towards the owner of the GDrive folder
|
||||||
# will need to have write access on the server so refresh flow works
|
# (only 1. or 2. - if both specified then this 2. takes precedence)
|
||||||
# run the file `create_update_test_oauth_token.py` to create the token and save in a secrets directory so
|
# needs write access on the server so refresh flow works
|
||||||
# it is not checked into source control
|
# To get the token, run the file `create_update_test_oauth_token.py`
|
||||||
oauth_token_file_path_and_name: "secrets/token-davemateer-gmail.json"
|
# 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
|
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
|
@dataclass
|
||||||
class GDConfig:
|
class GDConfig:
|
||||||
root_folder_id: str
|
root_folder_id: str
|
||||||
oauth_token_file_path_and_name: str
|
oauth_token_filename: str
|
||||||
service_account: str
|
service_account: str = "service_account.json"
|
||||||
folder: str = "default"
|
folder: str = "default"
|
||||||
|
|
||||||
class GDStorage(Storage):
|
class GDStorage(Storage):
|
||||||
|
@ -25,7 +25,7 @@ class GDStorage(Storage):
|
||||||
|
|
||||||
SCOPES=['https://www.googleapis.com/auth/drive']
|
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:
|
if token_file is not None:
|
||||||
"""
|
"""
|
||||||
Tokens are refreshed after 1 hour
|
Tokens are refreshed after 1 hour
|
||||||
|
|
Ładowanie…
Reference in New Issue