Các mã mẫu Python sau đây minh hoạ cách sử dụng App Engine để thực hiện lệnh gọi YouTube Data API (v3). Bạn có thể tải các mã mẫu này xuống từ Kho lưu trữ mã mẫu Google App Engine Python trên GitHub.
Điều kiện tiên quyết
Bạn phải thiết lập một dự án trong Google API Console và nhận khoá API để có thể chạy các mã mẫu bên dưới. Hiện tại, mỗi dự án xác định một biến API_KEY
được đặt thành giá trị REPLACE_ME
. Bạn cần phải thay thế giá trị đó bằng khoá API của riêng mình để có thể chạy các mẫu.
Lưu ý quan trọng: Để có thể chạy tất cả các ví dụ này, bạn phải bật YouTube Data API (v3) và Freebase API cho dự án được liên kết với khoá API của bạn. Nếu ứng dụng của bạn truy cập vào dữ liệu kênh hoặc dữ liệu riêng tư của người dùng, thì bạn cũng cần có mã ứng dụng khách OAuth 2.0.
Xem hướng dẫn để tạo khoá API và nhận ID ứng dụng khách OAuth 2.0.
Tạo dự án và lấy thông tin xác thực uỷ quyền
- Mở trang Thông tin đăng nhập trong Bảng điều khiển API.
-
API này hỗ trợ hai loại thông tin xác thực.
Tạo bất cứ thông tin đăng nhập nào phù hợp với dự án của bạn:
-
OAuth 2.0: Bất cứ khi nào ứng dụng của bạn yêu cầu người dùng riêng tư thì ứng dụng phải gửi mã thông báo OAuth 2.0 cùng với yêu cầu. Thông tin trước tiên sẽ gửi ID ứng dụng khách và có thể là mật khẩu ứng dụng khách để lấy mã thông báo. Bạn có thể tạo thông tin đăng nhập OAuth 2.0 cho web ứng dụng, tài khoản dịch vụ hoặc ứng dụng đã cài đặt.
Để biết thêm thông tin, hãy xem tài liệu về OAuth 2.0.
-
Khoá API: Một yêu cầu không cung cấp mã thông báo OAuth 2.0 phải gửi API . Khoá này giúp xác định dự án của bạn và cấp quyền truy cập vào API, hạn mức và .
API này hỗ trợ một số loại quy định hạn chế đối với khoá API. Nếu khoá API mà bạn chưa có, hãy tạo khoá API trong Console bằng cách nhấp vào Tạo thông tin xác thực > Khoá API. Bạn có thể hạn chế khoá này trước khi dùng trong phiên bản phát hành công khai bằng cách nhấp vào Hạn chế khoá rồi chọn một trong Quy định hạn chế.
-
Để bảo mật khoá API, hãy làm theo các phương pháp hay nhất về một cách an toàn bằng cách sử dụng khoá API.
Mã mẫu
Tìm kiếm theo từ khoá
Mã mẫu bên dưới gọi phương thức search.list
của API để truy xuất kết quả tìm kiếm liên kết với một từ khoá cụ thể.
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)
Tìm kiếm theo chủ đề
Mã mẫu bên dưới gọi phương thức search.list
của API để truy xuất kết quả tìm kiếm liên quan đến một chủ đề Freebase cụ thể.
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)
Truy xuất các video tải lên của kênh
Mã mẫu bên dưới gọi phương thức playlistItems.list
của API để truy xuất danh sách video đã tải lên một kênh đã chỉ định. Bạn có thể xác định kênh theo mã nhận dạng kênh hoặc tên kênh. Mã này cũng gọi phương thức channels.list
để truy xuất mã nhận dạng danh sách phát giúp xác định các video được tải lên của kênh.
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)