In this guide, I will show how to use Python and APIs to connect to data sources that Power BI doesn’t support with built-in connectors. I wanted to build a Power BI dashboard for our YouTube channel to track views and other activity, so I connected to the YouTube Analytics API with a Python script and pulled the data into my Power BI model. Normally, I could export data manually from YouTube Studio, but that takes time and needs to be repeated each time I want updated results. This approach also avoids relying on spreadsheets in Google Drive or OneDrive as intermediate storage.
This token is a piece of that proves you have have permission to access the application, so please store it securely.
Open Command Prompt or Terminal, and install these libraries/packages:
pip install google-auth google-auth-oauthlib google-api-python-client pandas
import os
import pandas as pd
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
SCOPES = ["https://www.googleapis.com/auth/yt-analytics.readonly",
"https://www.googleapis.com/auth/youtube.readonly"]
CLIENT_SECRET_FILE = r"C:\Users\OlegSklyarsky\Desktop\Portfolio\youtube_ko\API\client_secret_ko.json"
TOKEN_FILE = "token.json"
def authenticate_google():
creds = None
if os.path.exists(TOKEN_FILE):
creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES)
if not creds or not creds.valid:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
creds = flow.run_local_server(port=0)
with open(TOKEN_FILE, "w") as token:
token.write(creds.to_json())
return creds
creds = authenticate_google()
youtube_analytics = build("youtubeAnalytics", "v2", credentials=creds)
request = youtube_analytics.reports().query(
ids="channel==MINE",
startDate="2025-01-01",
endDate="2025-08-18",
metrics="views,likes",
dimensions="video,day"
)
response = request.execute()
cols = [col["name"] for col in response["columnHeaders"]]
rows = response.get("rows", [])
df = pd.DataFrame(rows, columns=cols)
dataset = df
Using Python and the YouTube Analytics API opens up a lot of flexibility for Power BI users. You are no longer limited to manual exports from YouTube Studio, and you can tailor exactly which metrics and dimensions you want to track. While this tutorial is focused on the YouTube Analytic API, you can use a similar approach for many other data sources without native Power BI connectors. If you’re comfortable experimenting with APIs, you’ll find it’s a powerful way to extend what Power BI can do.