หมายเหตุ: API สำหรับ Content ID ของ YouTube มีขึ้นเพื่อให้พาร์ทเนอร์เนื้อหาของ YouTube ใช้งาน และไม่สามารถเข้าถึงได้โดยนักพัฒนาแอปบางรายหรือผู้ใช้ YouTube บางราย หากคุณไม่เห็น API สำหรับ Content ID ของ YouTube เป็นหนึ่งในบริการที่แสดงอยู่ในคอนโซล 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
โดยใช้รหัสลับไคลเอ็นต์ที่กำหนดค่าไว้ในขั้นตอนที่ 2a หากผู้ใช้อนุญาตให้แอปพลิเคชันของเ������่งคำขอ 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.
หมายเหตุ: โมดูล
oauth2client
จะจดจำพารามิเตอร์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)