หมายเหตุ: Content ID API ของ YouTube มีไว้เพื่อการใช้งานโดยพาร์ทเนอร์เนื้อหาของ YouTube และนักพัฒนาซอฟต์แวร์บางรายหรือผู้ใช้ YouTube บางส่วนอาจเข้าถึงไม่ได้ หากไม่เห็น YouTube Content ID API เป็นหนึ่งในบริการที่แสดงในคอนโซล Google API โปรดไปที่ศูนย์ช่วยเหลือของ YouTube เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับโปรแกรมพาร์ทเนอร์ YouTube
บทแนะนำแบบทีละขั้นตอนนี้อธิบายวิธีสร้างสคริปต์ที่เชื่อมต่อกับ ContentOwnersService และเรียกข้อมูลเกี่ยวกับเจ้าของเนื้อหานั้นๆ ตัวอย่างโค้ดที่สมบูรณ์จะแสดงอยู่ที่ส่วนท้ายของบทแนะนำ แม้ว่าโค้ดนี้จะเขียนด้วย Python แต่ก็มีไลบรารีของไคลเอ็นต์สำหรับภาษาโปรแกรมยอดนิยมอื่นๆ ด้วย
ข้อกำหนด
- Python 2.5 ขึ้นไป
- google-api-python-client
การสร้างสคริปต์เพื่อส่งคําขอ API
ขั้นตอนต่อไปนี้อธิบายวิธีสร้างสคริปต์เพื่อส่งคำขอ API สำหรับ Content ID ของ YouTube
ขั้นตอนที่ 1: สร้างสคริปต์พื้นฐาน
สคริปต์ต่อไปนี้ยอมรับพารามิเตอร์บรรทัดคำสั่งต่อไปนี้และตั้งค่าในตัวแปร FLAGS ร่วม
- ต้องระบุพารามิเตอร์
content_owner_idและระบุเจ้าของเนื้อหา CMS ที่คุณกำลังดึงข้อมูล - พารามิเตอร์
logging_levelจะระบุระดับรายละเอียดการบันทึกสําหรับสคริปต์ - พารามิเตอร์
helpทําให้สคริปต์แสดงรายการพารามิเตอร์ที่เข้าใจ
#!/usr/bin/python2.6 # -*- coding: utf-8 -*- import gflags import logging import sys import os from datetime import * # Define flags. The gflags module makes it easy to define command-line params # for an application. Run this program with the '--help' argument to see all # of the flags that it understands. FLAGS = gflags.FLAGS gflags.DEFINE_string('content_owner_id', None, ('Required flag. ' 'Identifies the content owner whose details are printed out.')) gflags.MarkFlagAsRequired('content_owner_id') gflags.DEFINE_enum('logging_level', 'ERROR', ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], 'Set the level of logging detail.') def main(argv): # Let the gflags module process the command-line arguments try: argv = FLAGS(argv) except gflags.FlagsError, e: print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS) sys.exit(1) # Set the logging according to the command-line flag logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) if __name__ == '__main__': main(sys.argv)
ขั้นตอนที่ 2: เปิดใช้การตรวจสอบสิทธิ์และการให้สิทธิ์ผู้ใช้
ในขั้นตอนนี้ เราจะรวมการให้สิทธิ์ OAuth 2.0 ไว้ในสคริปต์ ซึ่งจะช่วยให้ผู้ใช้ที่เรียกใช้สคริปต์สามารถให้สิทธิ์สคริปต์เพื่อดำเนินการตามคำขอ API ที่มาจากบัญชีของผู้ใช้
ขั้นตอนที่ 2ก: สร้างไฟล์ client_secrets.json
API ของ Content ID ของ YouTube ต้องการไฟล์ client_secrets.json ซึ่งประกอบด้วยข้อมูลจากคอนโซล API เพื่อดำเนินการตรวจสอบสิทธิ์ และจะต้องลงทะเบียนแอปพลิเคชันด้วย โปรดดูคำอธิบายวิธีการตรวจสอบสิทธิ์ที่สมบูรณ์ในคู่มือการตรวจสอบสิทธิ์
{ "web": { "client_id": "INSERT CLIENT ID HERE", "client_secret": "INSERT CLIENT SECRET HERE", "redirect_uris": [], "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token" } }
ขั้นตอนที่ 2ข: เพิ่มโค้ดการตรวจสอบสิทธิ์ลงในสคริปต์
ในการเปิดใช้งานการตรวจสอบสิทธิ์และการให้สิทธิ์ผู้ใช้ คุณต้องเพิ่มคำสั่ง import ต่อไปนี้
from oauth2client.file import Storage from oauth2client.client import flow_from_clientsecrets from oauth2client.tools import run
ถัดไป เราจะสร้างออบเจ็กต์ FLOW โดยใช้ข้อมูลลับไคลเอ็นต์ที่กําหนดค่าไว้ในขั้นตอนที่ 2ก หากผู้ใช้อนุญาตให้แอปพลิเคชันของเราส่งคำขอ API ในนามของผู้ใช้ ข้อมูลเข้าสู่ระบบที่ได้จะเก็บอยู่ในออบเจ็กต์ Storage เพื่อใช้ในภายหลัง ผู้ใช้จะต้องให้สิทธิ์แอปพลิเคชันของเราอีกครั้งหากข้อมูลเข้าสู่ระบบหมดอายุ
เพิ่มโค้ดต่อไปนี้ที่ส่วนท้ายของฟังก์ชัน main
# Set up a Flow object to be used if we need to authenticate. FLOW = flow_from_clientsecrets('client_secrets.json', scope='https://www.googleapis.com/auth/youtubepartner', message='error message') # The Storage object stores the credentials. If it doesn't exist, or if # the credentials are invalid or expired, run through the native client flow. storage = Storage('yt_partner_api.dat') credentials = storage.get() if (credentials is None or credentials.invalid or credentials.token_expiry <= datetime.now()): credentials = run(FLOW, storage)
ขั้นตอนที่ 2ค: สร้างออบเจ็กต์ httplib2 และแนบข้อมูลเข้าสู่ระบบ
หลังจากที่ผู้ใช้ให้สิทธิ์สคริปต์ของเราแล้ว เราจะสร้างออบเจ็กต์ httplib2.Http ซึ่งจัดการคำขอ API และแนบข้อมูลเข้าสู่ระบบการให้สิทธิ์กับออบเจ็กต์ดังกล่าว
เพิ่มคำสั่งการนำเข้าต่อไปนี้
import httplib2
และเพิ่มโค้ดนี้ไว้ที่ท้ายฟังก์ชัน main
# Create httplib2.Http object to handle HTTP requests and # attach auth credentials. http = httplib2.Http() http = credentials.authorize(http)
ขั้นตอนที่ 3: รับบริการ
ฟังก์ชัน build ของไลบรารีของไคลเอ็นต์ Python จะสร้างทรัพยากรที่โต้ตอบกับ API ได้ หลังจากผู้ใช้ให้สิทธิ์แอปพลิเคชันของเราแล้ว เราจะสร้างออบเจ็กต์ service ซึ่งให้เมธอดสำหรับการโต้ตอบกับ ContentOwnerService
เพิ่มคำสั่งการนําเข้าต่อไปนี้
from apiclient.discovery import build
และเพิ่มโค้ดนี้ต่อท้ายฟังก์ชัน main
service = build("youtubePartner", "v1", http=http, static_discovery=False) contentOwnersService = service.contentOwners()
ขั้นตอนที่ 4: ดำเนินการตามคําขอ API
ต่อไปเราจะสร้างคำขอบริการและดำเนินการตามคำขอ โค้ดต่อไปนี้จะสร้างและเรียกใช้คำขอ contentOwnersService.get() ซึ่งจะดึงข้อมูลเกี่ยวกับเจ้าของเนื้อหาที่ระบุ
เพิ่มโค้ดนี้ไว้ที่ท้ายฟังก์ชัน main
# Create and execute get request. request = contentOwnersService.get(contentOwnerId=FLAGS.content_owner_id) content_owner_doc = request.execute(http) print ('Content owner details: id: %s, name: %s, ' 'notification email: %s') % ( content_owner_doc['id'], content_owner_doc['displayName'], content_owner_doc['disputeNotificationEmails'])
กรอกใบสมัครให้เสร็จสมบูรณ์
ส่วนนี้จะแสดงแอปพลิเคชันที่สมบูรณ์พร้อมด้วยข้อมูลการอนุญาตให้ใช้สิทธิบางส่วนและความคิดเห็นเพิ่มเติมในสคริปต์ โปรแกรมนี้ทำงานได้ 2 วิธีดังนี้
-
คำสั่งนี้จะเปิดหน้าต่างเบราว์เซอร์ขึ้นมา ซึ่งคุณสามารถตรวจสอบสิทธิ์ได้หากจำเป็น และอนุญาตให้แอปพลิเคชันส่งคำขอ API หากคุณให้สิทธิ์แอปพลิเคชัน ระบบจะส่งต่อข้อมูลเข้าสู่ระบบกลับไปให้สคริปต์โดยอัตโนมัติ
python yt_partner_api.py --content_owner_id=CONTENT_OWNER_ID.
หมายเหตุ: คุณดูค่า
CONTENT_OWNER_IDของบัญชีได้ในหน้าการตั้งค่าบัญชีในบัญชี CMS ค่าจะแสดงเป็นPartner Codeในส่วนข้อมูลบัญชีในหน้านั้น -
คำสั่งนี้จะแสดงผล URL ที่คุณสามารถเปิดในเบราว์เซอร์ และระบบจะแจ้งให้คุณป้อนรหัสการให้สิทธิ์ด้วย เมื่อไปที่ URL หน้าดังกล่าวจะช่วยให้คุณอนุญาตให้แอปพลิเคชันส่งคำขอ API ในนามของคุณได้ หากคุณให้สิทธิ์ดังกล่าว หน้าเว็บจะแสดงรหัสการให้สิทธิ์ที่คุณต้องป้อนเมื่อมีข้อความแจ้งเพื่อดำเนินการขั้นตอนการให้สิทธิ์ให้เสร็จสมบูรณ์
python yt_partner_api.py --content_owner_id=CONTENT_OWNER_ID --noauth_local_webserver.
หมายเหตุ:
oauth2clientmodule จะจดจำพารามิเตอร์noauth_local_webserverแม้ว่าจะไม่ได้กล่าวถึงพารามิเตอร์นั้นในสคริปต์ก็ตาม
client_secrets.json
{ "web": { "client_id": "INSERT CLIENT ID HERE", "client_secret": "INSERT CLIENT SECRET HERE", "redirect_uris": [], "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token" } }
yt_partner_api.py
#!/usr/bin/python2.6 # -*- coding: utf-8 -*- # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Simple command-line sample for YouTube Content ID API. Command-line application that retrieves the information about given content owner. Usage: $ python yt_partner_api.py --content_owner_id=[contentOwnerId] $ python yt_partner_api.py --content_owner_id=[contentOwnerId] --noauth_local_webserver You can also get help on all the command-line flags the program understands by running: $ python yt_partner_api.py --help To get detailed log output run: $ python yt_partner_api.py --logging_level=DEBUG \ --content_owner_id=[contentOwnerId] """ import gflags import httplib2 import logging import sys import os from datetime import * from apiclient.discovery import build from oauth2client.file import Storage from oauth2client.client import flow_from_clientsecrets from oauth2client.tools import run # Define flags. The gflags module makes it easy to define command-line options # for an application. Run this program with the '--help' argument to see all # of the flags that it understands. FLAGS = gflags.FLAGS gflags.DEFINE_string('content_owner_id', None, ('Required flag. ' 'Identifies the content owner id whose details are printed out.')) gflags.MarkFlagAsRequired('content_owner_id') gflags.DEFINE_enum('logging_level', 'ERROR', ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], 'Set the level of logging detail.') def main(argv): # Let the gflags module process the command-line arguments try: argv = FLAGS(argv) except gflags.FlagsError, e: print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS) sys.exit(1) # Set the logging according to the command-line flag logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) # Set up a Flow object to be used if we need to authenticate. FLOW = flow_from_clientsecrets('client_secrets.json', scope='https://www.googleapis.com/auth/youtubepartner', message='error message') # The Storage object stores the credentials. If the credentials are invalid # or expired and the script isn't working, delete the file specified below # and run the script again. storage = Storage('yt_partner_api.dat') credentials = storage.get() if (credentials is None or credentials.invalid or credentials.token_expiry <= datetime.now()): credentials = run(FLOW, storage) http = httplib2.Http() http = credentials.authorize(http) service = build("youtubePartner", "v1", http=http) contentOwnersService = service.contentOwners() # Create and execute get request. request = contentOwnersService.get(contentOwnerId=FLAGS.content_owner_id) content_owner_doc = request.execute(http) print ('Content owner details: id: %s, name: %s, ' 'notification email: %s') % ( content_owner_doc['id'], content_owner_doc['displayName'], content_owner_doc['disputeNotificationEmails']) if __name__ == '__main__': main(sys.argv)