2022-07-18 12:39:00 +00:00
import os . path
2025-03-12 14:27:45 +00:00
import click
import json
2022-07-18 12:39:00 +00:00
from google . auth . transport . requests import Request
from google . oauth2 . credentials import Credentials
from google_auth_oauthlib . flow import InstalledAppFlow
from googleapiclient . discovery import build
from googleapiclient . errors import HttpError
2022-07-25 13:52:50 +00:00
# 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
2022-07-18 12:39:00 +00:00
# Code below from https://developers.google.com/drive/api/quickstart/python
2023-05-09 11:14:07 +00:00
# Example invocation: py scripts/create_update_gdrive_oauth_token.py -c secrets/credentials.json -t secrets/gd-token.json
2022-07-18 12:39:00 +00:00
2025-01-30 08:42:23 +00:00
SCOPES = [ " https://www.googleapis.com/auth/drive.file " ]
2022-07-18 12:39:00 +00:00
2023-02-07 21:59:24 +00:00
@click.command (
help = " script to generate Google Drive OAuth token to use gdrive_storage, requires credentials.json and outputs gd-token.json, if you don ' t have credentials.json go to https://console.cloud.google.com/apis/credentials. Be sure to add ' http://localhost:55192/ ' to the Authorized redirect URIs in your OAuth App. More info: https://davemateer.com/2022/04/28/google-drive-with-python "
)
@click.option (
" --credentials " ,
" -c " ,
type = click . Path ( exists = True ) ,
help = " path to the credentials.json file downloaded from https://console.cloud.google.com/apis/credentials " ,
2025-01-30 08:42:23 +00:00
required = True ,
2023-02-07 21:59:24 +00:00
)
@click.option (
" --token " ,
" -t " ,
type = click . Path ( exists = False ) ,
default = " gd-token.json " ,
help = " file where to place the OAuth token, defaults to gd-token.json which you must then move to where your orchestration file points to, defaults to gd-token.json " ,
2025-01-30 08:42:23 +00:00
required = True ,
2023-02-07 21:59:24 +00:00
)
def main ( credentials , token ) :
2022-07-18 12:39:00 +00:00
# The file token.json stores the user's access and refresh tokens, and is
2023-02-07 21:59:24 +00:00
# created automatically when the authorization flow completes for the first time.
creds = None
if os . path . exists ( token ) :
2025-01-30 08:42:23 +00:00
with open ( token , " r " ) as stream :
2023-02-07 21:59:24 +00:00
creds_json = json . load ( stream )
# creds = Credentials.from_authorized_user_file(creds_json, SCOPES)
2025-01-30 08:42:23 +00:00
creds_json [ " refresh_token " ] = creds_json . get ( " refresh_token " , " " )
2023-02-07 21:59:24 +00:00
creds = Credentials . from_authorized_user_info ( creds_json , SCOPES )
2022-07-18 12:39:00 +00:00
# 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 :
2025-01-30 08:42:23 +00:00
print ( " Requesting new token " )
2022-07-18 12:39:00 +00:00
creds . refresh ( Request ( ) )
else :
2025-01-30 08:42:23 +00:00
print ( " First run through so putting up login dialog " )
2022-07-18 12:39:00 +00:00
# credentials.json downloaded from https://console.cloud.google.com/apis/credentials
2023-02-07 21:59:24 +00:00
flow = InstalledAppFlow . from_client_secrets_file ( credentials , SCOPES )
2022-07-25 13:52:50 +00:00
creds = flow . run_local_server ( port = 55192 )
2022-07-18 12:39:00 +00:00
# Save the credentials for the next run
2025-01-30 08:42:23 +00:00
with open ( token , " w " ) as token :
print ( " Saving new token " )
2022-07-18 12:39:00 +00:00
token . write ( creds . to_json ( ) )
else :
2025-01-30 08:42:23 +00:00
print ( " Token valid " )
2022-07-18 12:39:00 +00:00
try :
2025-01-30 08:42:23 +00:00
service = build ( " drive " , " v3 " , credentials = creds )
2022-07-18 12:39:00 +00:00
# About the user
results = service . about ( ) . get ( fields = " * " ) . execute ( )
2025-01-30 08:42:23 +00:00
emailAddress = results [ " user " ] [ " emailAddress " ]
2022-07-18 12:39:00 +00:00
print ( emailAddress )
# Call the Drive v3 API and return some files
2025-03-10 18:44:54 +00:00
results = service . files ( ) . list ( pageSize = 10 , fields = " nextPageToken, files(id, name) " ) . execute ( )
2025-01-30 08:42:23 +00:00
items = results . get ( " files " , [ ] )
2022-07-18 12:39:00 +00:00
if not items :
2025-01-30 08:42:23 +00:00
print ( " No files found. " )
2022-07-18 12:39:00 +00:00
return
2025-01-30 08:42:23 +00:00
print ( " Files: " )
2022-07-18 12:39:00 +00:00
for item in items :
2025-01-30 08:42:23 +00:00
print ( " {0} ( {1} ) " . format ( item [ " name " ] , item [ " id " ] ) )
2022-07-18 12:39:00 +00:00
except HttpError as error :
2025-01-30 08:42:23 +00:00
print ( f " An error occurred: { error } " )
2022-07-18 12:39:00 +00:00
2022-07-25 13:52:50 +00:00
2025-01-30 08:42:23 +00:00
if __name__ == " __main__ " :
2022-07-25 13:52:50 +00:00
main ( )