In Code Sample #1 I am running a successful query that returns the the viewer percentage of different age groups from a user's youtube account. My ultimate goal is to run multiple queries at the same time and return the results for those queries. Code Sample #2 is my example of implementation for this method but Google then forces two OAuth login pages instead of using the credentials from just one to address the queries. What is the best implementation to accomplish this?
Code Sample #1
import osimport jsonimport pandas as pdfrom pandas import json_normalizefrom google_auth_oauthlib.flow import InstalledAppFlowfrom google.auth.transport.requests import Requestfrom googleapiclient.discovery import buildfrom collections import OrderedDictfrom tabulate import tabulateimport mysql.connectorfrom mysql.connector import errorcodecreator_ID = input("Creator ID: ")try: db2 = mysql.connector.connect(user = 'admin', password = 'abcdefg', host = 'database', database = 'app') if db2.is_connected(): db_Info = db2.get_server_info() print("Connected to MySQL Server version ", db_Info) cursor = db2.cursor() cursor.execute("select database();") record = cursor.fetchone() print("You're connected to database: ", record)except mysql.connector.Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("Something is wrong with your user name or password") elif err.errno == errorcode.ER_BAD_DB_ERROR: print("Database does not exist") else: print(err)else: print("Successfully connect to database!")cursor = db2.cursor()try: cursor.execute("""__""", list(creator_ID)) ig_long_lived_token = cursor.fetchall()[0] print("Successfully get long lived token!")except mysql.connector.IntegrityError as err: print("Error: {}".format(err))SCOPES=['https://www.googleapis.com/auth/yt-analytics.readonly']API_SERVICE_NAME = 'youtubeAnalytics'API_VERSION = 'v2'CLIENT_SECRETS_FILE = 'client_secrets3.json'def get_service(): flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) credentials = flow.run_local_server(port=8080, prompt="consent", authorization_prompt_message="") return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)def execute_api_request(client_library_function, **kwargs): response = client_library_function( **kwargs ).execute() print(response) return responsedef create_table(table, headers=None): if headers: headerstring = "\t{}\t" * len(headers) print(headerstring.format(*headers)) rowstring = "\t{}\t" * len(table[0]) for row in table: print(rowstring.format(*row))if __name__ == '__main__': youtubeAnalytics = get_service() result = execute_api_request( youtubeAnalytics.reports().query, ids='channel==MINE', startDate='2005-05-01', endDate='2023-01-01', dimensions='ageGroup', metrics='viewerPercentage' ) headers = ['viewerPercentage'] print(tabulate(result['rows'], headers=headers, tablefmt='pretty'))-----------------------------------------------------------------------------------------Code Sample #2
def ageGroup_fetch(): youtubeAnalytics = get_service() result = execute_api_request( youtubeAnalytics.reports().query, ids='channel==MINE', startDate='2005-05-01', endDate='2023-01-01', dimensions='ageGroup', metrics='viewerPercentage' )def gender_fetch(): youtubeAnalytics = get_service() result = execute_api_request( youtubeAnalytics.reports().query, ids='channel==MINE', startDate='2005-05-01', endDate='2023-01-01', dimensions='gender', metrics='viewerPercentage' )if __name__ == '__main__': ageGroup_fetch() gender_fetch()