Oauth arguments passthrough

This commit is contained in:
2021-04-13 17:29:19 +03:00
parent 6eaaf1800d
commit 1213231992
2 changed files with 21 additions and 12 deletions

View File

@@ -12,7 +12,7 @@ from rich.progress import track
CLIENT_SECRETS_FILE = "client_secrets.json"
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_READ_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
@@ -35,11 +35,11 @@ https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
CLIENT_SECRETS_FILE)))
def retrieve_youtube_subscriptions():
def retrieve_youtube_subscriptions(args):
# In order to retrieve the YouTube subscriptions for the current user,
# the user needs to authenticate and authorize access to their YouTube
# subscriptions.
youtube_authorization = get_authenticated_service()
youtube_authorization = get_authenticated_service(args)
try:
# init
@@ -69,7 +69,7 @@ def retrieve_youtube_subscriptions():
err.resp.status, err.content))
def get_authenticated_service():
def get_authenticated_service(args):
# Create a Storage instance to store and retrieve a single
# credential to and from a file. Used to store the
# oauth2 credentials for the current python script.
@@ -80,15 +80,16 @@ def get_authenticated_service():
if credentials is None or credentials.invalid:
# Create a Flow instance from a client secrets file
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
scope=YOUTUBE_READ_WRITE_SCOPE,
scope=YOUTUBE_READ_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
# The run_flow method requires arguments.
# Initial default arguments are setup in tools, and any
# additional arguments can be added from the command-line
# and passed into this method.
args = argparser.parse_args()
oauth_args = argparser.parse_args(args=[])
oauth_args.auth_host_name = args.auth_host_name
oauth_args.auth_host_port = [args.auth_host_port]
oauth_args.noauth_local_webserver = args.noauth_local_webserver
# Obtain valid credentials
credentials = run_flow(flow, storage, args)
credentials = run_flow(flow, storage, oauth_args)
# Build and return a Resource object for interacting with an YouTube API
return build(YOUTUBE_API_SERVICE_NAME,

10
main.py
View File

@@ -43,6 +43,14 @@ parser.add_argument('-a', '--all', action='store_true',
parser.add_argument('-f', '--format', action='store', type=str, default='best',
help='Format to pass to youtube-dl. (default: best)')
# oauth2client.tools.run_flow arguments
parser.add_argument('--auth_host_name', action='store', type=str, default='localhost',
help='Host name to use when running a local web server to handle redirects during OAuth authorization.')
parser.add_argument('--auth_host_port', action='store', type=int, default=8080,
help='Port to use when running a local web server to handle redirects during OAuth authorization.')
parser.add_argument('--noauth_local_webserver', action='store_true',
help='Run a local web server to handle redirects during OAuth authorization.')
args = parser.parse_args()
json_input = args.input
@@ -84,7 +92,7 @@ if json_input:
all_channels = json.loads(f.read())
f.close()
else:
all_channels = retrieve_youtube_subscriptions()
all_channels = retrieve_youtube_subscriptions(args)
curr_channel = 0
c.print(
f'You will be prompted if you want to download a channel for each of your subscriptions. (total {len(all_channels)})', style='bold')