The YouTube Data API v3 allows developers to access a wealth of information about videos, channels, and playlists. In this tutorial, we'll write a simple Python script to fetch details like the description, view count, and tags for a specific video.
The Code
We will use the google-api-python-client library. The script authenticates using a client_secret.json file (OAuth 2.0) and retrieves the contentDetails, statistics, and snippet parts of the video resource.
PYTHON
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
# Define the permissions needed
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
def main():
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "client_secret.json"
# Authenticate user
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes)
credentials = flow.run_console()
# Build the service object
youtube = googleapiclient.discovery.build(api_service_name, api_version, credentials=credentials)
# Request video data
part_string = 'contentDetails,statistics,snippet'
videoid = "YOUR_VIDEO_ID_HERE" # Replace with actual ID
response = youtube.videos().list(part=part_string, id=videoid).execute()
print(response)
if __name__ == "__main__":
main()
