ตัวอย่างโค้ด Python ต่อไปนี้สาธิตวิธีใช้ App Engine ในการเรียก YouTube Data API (v3) คุณดาวน์โหลดตัวอย่างโค้ดเหล่านี้ได้จากที่เก็บตัวอย่างโค้ด Python ของ Google App Engine ใน GitHub
ข้อกำหนดเบื้องต้น
คุณต้องตั้งค่าโปรเจ็กต์ในคอนโซล Google API และรับคีย์ API เพื่อเรียกใช้ตัวอย่างโค้ดด้านล่าง ปัจจุบันแต่ละโปรเจ็กต์จะกำหนดตัวแปร API_KEY
ที่ตั้งค่าเป็น REPLACE_ME
คุณต้องแทนที่ค่านั้นด้วยคีย์ API ของคุณเองเพื่อให้เรียกใช้ตัวอย่างได้
สำคัญ: หากต้องการเรียกใช้ตัวอย่างเหล่านี้ทั้งหมด คุณต้องเปิดใช้ YouTube Data API (v3) และ Freebase API สำหรับโปรเจ็กต์ที่เชื่อมโยงกับคีย์ API ของคุณ หากแอปพลิเคชันเข้าถึงข้อมูลผู้ใช้หรือแชแนลส่วนตัว คุณจะต้องใช้รหัสไคลเอ็นต์ OAuth 2.0 ด้วย
ดูวิธีการสร้างคีย์ API และรับรหัสไคลเอ็นต์ OAuth 2.0 ของคุณ
สร้างโปรเจ็กต์และรับข้อมูลเข้าสู่ระบบการให้สิทธิ์
- เปิดหน้าข้อมูลเข้าสู่ระบบในคอนโซล API
-
API นี้รองรับข้อมูลเข้าสู่ระบบ 2 ประเภท
สร้างข้อมูลเข้าสู่ระบบแบบใดแบบหนึ่งต่อไปนี้ที่เหมาะกับโปรเจ็กต์ของคุณ
-
OAuth 2.0: เมื่อใดก็ตามที่แอปพลิเคชันขอผู้ใช้ส่วนตัว ข้อมูลนั้นต้องส่งโทเค็น OAuth 2.0 ไปพร้อมกับคำขอ บัญชี ครั้งแรกจะส่งรหัสไคลเอ็นต์ และบางทีอาจเป็นรหัสลับไคลเอ็นต์ไปยัง ขอรับโทเค็น คุณสร้างข้อมูลเข้าสู่ระบบ OAuth 2.0 สำหรับเว็บได้ แอปพลิเคชัน บัญชีบริการ หรือแอปพลิเคชันที่ติดตั้ง
โปรดดูข้อมูลเพิ่มเติมที่เอกสารประกอบเกี่ยวกับ OAuth 2.0
-
คีย์ API คำขอที่ไม่ระบุโทเค็น OAuth 2.0 จะต้องส่ง API คีย์จะระบุโปรเจ็กต์ของคุณและให้สิทธิ์เข้าถึง API, โควต้า และ รายงาน
API รองรับข้อจำกัดหลายประเภทเกี่ยวกับคีย์ API หากคีย์ API ที่คุณ ในการสร้างคีย์ API ในคอนโซลโดย คลิก สร้างข้อมูลรับรอง > คีย์ API คุณสามารถจำกัดคีย์ก่อนใช้งาน สำหรับการใช้งานจริงโดยคลิกจำกัดคีย์ และเลือกหนึ่งใน ข้อจำกัด
-
หากต้องการรักษาคีย์ API ให้ปลอดภัย ให้ทำตามแนวทางปฏิบัติแนะนำสำหรับ อย่างปลอดภัยโดยใช้คีย์ API
ตัวอย่างโค้ด
ค้นหาตามคีย์เวิร์ด
ตัวอย่างโค้ดด้านล่างเรียกเมธอด search.list
ของ API เพื่อดึงผลการค้นหาที่เชื่อมโยงกับคีย์เวิร์ดหนึ่งๆ
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)
ค้นหาตามหัวข้อ
ตัวอย่างโค้ดด้านล่างเรียกใช้เมธอด search.list
ของ API เพื่อดึงผลการค้นหาที่เชื่อมโยงกับหัวข้อ 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)
ดึงข้อมูลการอัปโหลดของช่อง
ตัวอย่างโค้ดด้านล่างเรียกใช้เมธอด playlistItems.list
ของ API เพื่อเรียกรายการวิดีโอที่อัปโหลดไปยังช่องที่ระบุ โดยระบุช่องได้โดยใช้รหัสช่องหรือชื่อช่อง โค้ดนี้ยังเรียกใช้เมธอด channels.list
เพื่อดึงข้อมูลรหัสเพลย์ลิสต์ที่ระบุวิดีโอที่อัปโหลดของช่องด้วย
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)