Page Summary
-
This webpage provides Python code samples demonstrating how to use App Engine to interact with the YouTube Data API (v3).
-
To use the code samples, you must first set up a project in the Google API Console, get an API key, and enable the YouTube Data API (v3) and Freebase API for your project.
-
The code samples include examples for searching by keyword, searching by a Freebase topic, and retrieving a channel's uploaded videos, each utilizing different methods of the YouTube Data API.
-
OAuth 2.0 tokens are required when accessing private user or channel data, while API keys are sufficient for requests that do not require authorization.
-
The code samples require users to replace the
REPLACE_MEplaceholder with their own API key to function correctly.
The following Python code samples demonstrate how to use App Engine to make YouTube Data API (v3) calls. You can download these code samples from the Google App Engine Python code sample repositories on GitHub.
Prerequisites
You must set up a project in the Google Cloud console and get an API key to be able to run the code samples below. Currently, each project defines an API_KEY variable that is set to the value REPLACE_ME. You need to replace that value with your own API key to be able to run the samples.
Important: To be able to run all of these examples, you must enable the YouTube Data API (v3) and the Freebase API for the project associated with your API key. If your application accesses private user or channel data, you will also need an OAuth 2.0 client ID.
Code samples
Search by keyword
The code sample below calls the API's search.list method to retrieve search results associated with a particular keyword.
import os import urllib import webapp2 import jinja2 from apiclient.discovery import build from optparse import OptionParser JINJA_ENVIRONMENT = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), extensions=['jinja2.ext.autoescape']) # Set DEVELOPER_KEY to the "API key" value from the Google Developers Console: # https://console.developers.google.com/project/_/apiui/credential # Please ensure that you have enabled the YouTube Data API for your project. DEVELOPER_KEY = "REPLACE_ME" YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" class MainHandler(webapp2.RequestHandler): def get(self): if DEVELOPER_KEY == "REPLACE_ME": self.response.write("""You must set up a project and get an API key to run this project. Please visit <landing page> to do so.""" else: youtube = build( YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) search_response = youtube.search().list( q="Hello", part="id,snippet", maxResults=5 ).execute() videos = [] channels = [] playlists = [] for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": videos.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["videoId"])) elif search_result["id"]["kind"] == "youtube#channel": channels.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["channelId"])) elif search_result["id"]["kind"] == "youtube#playlist": playlists.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["playlistId"])) template_values = { 'videos': videos, 'channels': channels, 'playlists': playlists } self.response.headers['Content-type'] = 'text/plain' template = JINJA_ENVIRONMENT.get_template('index.html') self.response.write