Las siguientes muestras de código de Python indican cómo utilizar App Engine para realizar llamadas a la API de YouTube Data (v3). Puedes descargar estas muestras de código desde los repositorios de muestras de código de Google App Engine en GitHub.
Requisitos previos
Debes configurar un proyecto en la Consola de APIs de Google y obtener una clave de API para poder ejecutar las siguientes muestras de código. Actualmente, cada proyecto define una variable API_KEY
que se establece en el valor REPLACE_ME
. Debes reemplazar ese valor por tu propia clave de API para poder ejecutar las muestras.
Importante: Para ejecutar todos estos ejemplos, debes habilitar la API de YouTube Data (v3) y la API de Freebase para el proyecto asociado con tu clave de API. Si tu aplicación accede a datos privados de usuarios o canales, también necesitarás un ID de cliente de OAuth 2.0.
Consulta las instrucciones para crear una clave de API y obtener tu ID de cliente de OAuth 2.0.
Crea tu proyecto y obtén credenciales de autorización
- Abre la página Credenciales en la Consola de APIs.
-
En esta API, se admiten dos tipos de credenciales.
Crea las credenciales apropiadas para tu proyecto:
-
OAuth 2.0: Siempre que se soliciten datos privados del usuario, mediante tu aplicación se debe enviar un token OAuth 2.0 junto con la solicitud. En primer lugar, se envía un ID de cliente y, posiblemente, un secreto de cliente para obtener un token a través de la aplicación. Puedes generar credenciales OAuth 2.0 para aplicaciones web, cuentas de servicio o aplicaciones instaladas.
Para obtener más información, consulta la documentación de OAuth 2.0.
-
Claves de API: Una solicitud que no proporciona un token de OAuth 2.0 debe enviar una API . Con la clave de API, se identifica tu proyecto y se proporciona acceso a la API, la cuota y los informes.
En la API, se admiten varios tipos de restricciones sobre las claves de API. Si la clave de API que no existe, crea una clave de API en la consola haz clic en Crear credenciales > Clave de API Puedes restringir la clave antes de usarla en producción. Para ello, haz clic en Restringir clave y selecciona una de las Restricciones.
-
Para garantizar la seguridad de tus claves de API, sigue las prácticas recomendadas de uso seguro de las claves de API.
Muestras de código
Buscar por palabra clave
El siguiente ejemplo de código llama al método search.list
de la API para recuperar los resultados de la búsqueda asociados con una palabra clave determinada.
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(template.render(template_values))
app = webapp2.WSGIApplication([
('/.*', MainHandler),
], debug=True)
Buscar por tema
El siguiente ejemplo de código llama al método search.list
de la API para recuperar los resultados de búsqueda relacionados con un tema específico de Freebase.
import os
import urllib
import webapp2
import jinja2
from apiclient.discovery import build
from optparse import OptionParser
import json
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'])
REGISTRATION_INSTRUCTIONS = """
You must set up a project and get an API key to run this code. Please see
the instructions for creating a project and a key at <a
href="https://developers.google.com/youtube/registering_an_application"
>https://developers.google.com/youtube/registering_an_application</a>.
<br><br>
Make sure that you have enabled the YouTube Data API (v3) and the Freebase
API for your project."""
# Set API_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 and Freebase API
# for your project.
API_KEY = "REPLACE_ME"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
FREEBASE_SEARCH_URL = "https://www.googleapis.com/freebase/v1/search?%s"
QUERY_TERM = "dog"
class MainHandler(webapp2.RequestHandler):
def get(self):
if API_KEY == 'REPLACE_ME':
self.response.write(REGISTRATION_INSTRUCTIONS)
else:
# Present a list of Freebase topic IDs for the query term
self.list_topics(QUERY_TERM)
def list_topics(self, QUERY_TERM):
# Retrieve a list of Freebase topics associated with the query term
freebase_params = dict(query=QUERY_TERM, key=API_KEY)
freebase_url = FREEBASE_SEARCH_URL % urllib.urlencode(freebase_params)
freebase_response = json.loads(urllib.urlopen(freebase_url).read())
if len(freebase_response["result"]) == 0:
exit("No matching terms were found in Freebase.")
# Create a page that shows a select box listing the topics.
# When the user selects a topic and submits the form, the
# 'post' method below will handle the form submission and
# retrieve videos for the selected topic.
select_topic_page = ('''
<html>
<body>
<p>The following topics were found:</p>
<form method="post">
<select name="topic">
''')
for result in freebase_response["result"]:
select_topic_page += ('<option value="' + result["mid"] + '">' +
result.get("name", "Unknown") + '</option>')
select_topic_page += '''
</select>
<p><input type="submit" /></p>
</form>
</body>
</html>
'''
# Display the HTML page listing the topic choices.
self.response.out.write(select_topic_page)
def post(self):
topic_id = self.request.get('topic')
# Service for calling the YouTube API
youtube = build(YOUTUBE_API_SERVICE_NAME,
YOUTUBE_API_VERSION,
developerKey=API_KEY)
# Execute the search request using default query term and retrieved topic.
search_response = youtube.search().list(
part = 'id,snippet',
type = 'video',
topicId = topic_id
).execute()
videos = []
for search_result in search_response.get("items", []):
videos.append(search_result)
template_values = {
'videos': videos
}
self.response.headers['Content-type'] = 'text/html'
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
app = webapp2.WSGIApplication([
('/.*', MainHandler),
], debug=True)
Cómo recuperar las cargas de un canal
El siguiente ejemplo de código invoca el método playlistItems.list
de la API para recuperar una lista de videos subidos a un canal específico. El canal se puede identificar por su ID o nombre de canal. El código también llama al método channels.list
para recuperar el ID de la playlist que identifica los videos subidos del canal.
import os
import urllib
import webapp2
import jinja2
from apiclient.discovery import build
from optparse import OptionParser
import json
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'])
REGISTRATION_INSTRUCTIONS = """
You must set up a project and get an API key to run this code. Please see
the instructions for creating a project and a key at <a
href="https://developers.google.com/youtube/registering_an_application"
>https://developers.google.com/youtube/registering_an_application</a>.
<br><br>
Make sure that you have enabled the YouTube Data API (v3) and the Freebase
API for your project."""
# Set API_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 and Freebase API
# for your project.
API_KEY = "REPLACE_ME"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
FREEBASE_SEARCH_URL = "https://www.googleapis.com/freebase/v1/search?%s"
QUERY_TERM = "dog"
class MainHandler(webapp2.RequestHandler):
def get(self):
if API_KEY == 'REPLACE_ME':
self.response.write(REGISTRATION_INSTRUCTIONS)
else:
# Present a list of Freebase topic IDs for the query term
self.request_channel()
def request_channel(self):
# Display a text box where the user can enter a channel name or
# channel ID.
select_channel_page = '''
<html>
<body>
<p>Which channel's videos do you want to see?</p>
<form method="post">
<p>
<select name="channel_type">
<option value="id">Channel ID</option>
<option value="name">Channel name</option>
</select>
<input name="channel" size="30">
</p>
<p><input type="submit" /></p>
</form>
</body>
</html>
'''
# Display the HTML page that shows the form.
self.response.out.write(select_channel_page)
def post(self):
# Service for calling the YouTube API
youtube = build(YOUTUBE_API_SERVICE_NAME,
YOUTUBE_API_VERSION,
developerKey=API_KEY)
# Use form inputs to create request params for channel details
channel_type = self.request.get('channel_type')
channels_response = None
if channel_type == 'id':
channels_response = youtube.channels().list(
id=self.request.get('channel'),
part='snippet,contentDetails'
).execute()
else:
channels_response = youtube.channels().list(
forUsername=self.request.get('channel'),
part='snippet,contentDetails'
).execute()
channel_name = ''
videos = []
for channel in channels_response['items']:
uploads_list_id = channel['contentDetails']['relatedPlaylists']['uploads']
channel_name = channel['snippet']['title']
next_page_token = ''
while next_page_token is not None:
playlistitems_response = youtube.playlistItems().list(
playlistId=uploads_list_id,
part='snippet',
maxResults=50,
pageToken=next_page_token
).execute()
for playlist_item in playlistitems_response['items']:
videos.append(playlist_item)
next_page_token = playlistitems_response.get('tokenPagination', {}).get(
'nextPageToken')
if len(videos) > 100:
break
template_values = {
'channel_name': channel_name,
'videos': videos
}
self.response.headers['Content-type'] = 'text/html'
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
app = webapp2.WSGIApplication([
('/.*', MainHandler),
], debug=True)