पहला अनुरोध भेजना

ध्यान दें: YouTube Content ID API का इस्तेमाल, YouTube के कॉन्टेंट पार्टनर ही कर सकते हैं. यह सभी डेवलपर या YouTube के सभी उपयोगकर्ताओं के लिए उपलब्ध नहीं है. अगर आपको Google API कंसोल में दी गई सेवाओं में, YouTube Content ID API नहीं दिखता है, तो YouTube Partner Program के बारे में ज़्यादा जानने के लिए, YouTube सहायता केंद्र पर जाएं.

इस सिलसिलेवार ट्यूटोरियल में, ऐसी स्क्रिप्ट बनाने का तरीका बताया गया है जो ContentOwnersService से कनेक्ट होती है और कॉन्टेंट के किसी मालिक के बारे में जानकारी हासिल करती है. ट्यूटोरियल के आखिर में, पूरा कोड सैंपल दिया गया है. हालांकि, यह कोड Python में लिखा गया होता है, लेकिन दूसरी लोकप्रिय प्रोग्रामिंग भाषाओं के लिए क्लाइंट लाइब्रेरी भी उपलब्ध होती हैं.

ज़रूरी शर्तें

एपीआई अनुरोध भेजने के लिए स्क्रिप्ट बनाना

YouTube Content ID API का अनुरोध भेजने के लिए, स्क्रिप्ट बनाने का तरीका यहां बताया गया है:

पहला चरण: बेसिक स्क्रिप्ट बनाएं

यह स्क्रिप्ट, इन कमांड-लाइन पैरामीटर को स्वीकार करती है और ग्लोबल FLAGS वैरिएबल पर वैल्यू सेट करती है:

  • content_owner_id पैरामीटर ज़रूरी है. इससे उस सीएमएस कॉन्टेंट के मालिक की पहचान होती है जिसके बारे में आपको जानकारी चाहिए.
  • 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)

दूसरा चरण: उपयोगकर्ता की पुष्टि करने और अनुमति देने की सुविधा चालू करना

इस चरण में, हम स्क्रिप्ट में OAuth 2.0 ऑथराइज़ेशन शामिल करेंगे. इससे, स्क्रिप्ट चलाने वाले उपयोगकर्ता को स्क्रिप्ट को अनुमति देने में मदद मिलती है, ताकि वह उपयोगकर्ता के खाते से एपीआई अनुरोध कर सके.

दूसरा चरण (a): client_secrets.json फ़ाइल बनाना

YouTube Content ID API को पुष्टि करने के लिए, 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"
  }
}

चरण 2b: अपनी स्क्रिप्ट में पुष्टि करने वाला कोड जोड़ना

उपयोगकर्ता की पुष्टि करने और अनुमति देने की सुविधा चालू करने के लिए, आपको ये import स्टेटमेंट जोड़ने होंगे:

from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run

इसके बाद, हम दूसरे चरण के पहले हिस्से में कॉन्फ़िगर किए गए क्लाइंट सीक्रेट का इस्तेमाल करके, FLOW ऑब्जेक्ट बनाएंगे. अगर उपयोगकर्ता हमारे ऐप्लिकेशन को अपनी ओर से एपीआई अनुरोध सबमिट करने की अनुमति देता है, तो इससे मिले क्रेडेंशियल को बाद में इस्तेमाल करने के लिए, 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)

दूसरा चरण (c): httplib2 ऑब्जेक्ट बनाना और क्रेडेंशियल अटैच करना

जब उपयोगकर्ता हमारी स्क्रिप्ट को अनुमति दे देता है, तब हम एक httplib2.Http ऑब्जेक्ट बनाते हैं, जो एपीआई अनुरोधों को मैनेज करता है और उस ऑब्जेक्ट के साथ ऑथराइज़ेशन क्रेडेंशियल अटैच करता है.

यह इंपोर्ट स्टेटमेंट जोड़ें:

  import httplib2

और इस कोड को main फ़ंक्शन के अंत में जोड़ें:

  # Create httplib2.Http object to handle HTTP requests and
  # attach auth credentials.
  http = httplib2.Http()
  http = credentials.authorize(http)

तीसरा चरण: सेवा पाना

Python क्लाइंट लाइब्रेरी का build फ़ंक्शन, ऐसा रिसॉर्स बनाता है जो किसी एपीआई के साथ इंटरैक्ट कर सकता है. उपयोगकर्ता की ओर से हमारे ऐप्लिकेशन को अनुमति मिलने के बाद, हम service ऑब्जेक्ट बनाते हैं. इससे ContentOwnerService के साथ इंटरैक्ट करने के तरीके मिलते हैं.

इंपोर्ट स्टेटमेंट जोड़ें:

from apiclient.discovery import build

साथ ही, main फ़ंक्शन के आखिर में यह कोड जोड़ें:

  service = build("youtubePartner", "v1", http=http, static_discovery=False)
  contentOwnersService = service.contentOwners()

चौथा चरण: एपीआई अनुरोध पूरा करना

अब हम सेवा का अनुरोध बनाएंगे और उसे लागू करेंगे. यह कोड एक 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'])

आवेदन पूरा करना

यह अनुभाग लाइसेंस संबंधी कुछ जानकारी और स्क्रिप्ट में अतिरिक्त टिप्पणियों के साथ संपूर्ण ऐप्लिकेशन दिखाता है. प्रोग्राम को चलाने के दो तरीके हैं:

  • यह निर्देश एक ब्राउज़र विंडो लॉन्च करता है. इसकी मदद से, ज़रूरी होने पर पुष्टि की जा सकती है और ऐप्लिकेशन को एपीआई अनुरोध सबमिट करने की अनुमति दी जा सकती है. अगर आप ऐप्लिकेशन को अनुमति देते हैं, तो क्रेडेंशियल अपने-आप स्क्रिप्ट पर वापस रिले हो जाते हैं.

    python yt_partner_api.py --content_owner_id=CONTENT_OWNER_ID.

    ध्यान दें: कॉन्टेंट मैनेजमेंट सिस्टम खाते के खाता सेटिंग पेज पर जाकर, अपने खाते के लिए CONTENT_OWNER_ID वैल्यू देखी जा सकती है. इस पेज पर, खाता जानकारी वाले सेक्शन में वैल्यू को Partner Code के तौर पर दिखाया जाता है.

  • यह निर्देश एक यूआरएल दिखाता है, जिसे ब्राउज़र में खोला जा सकता है. साथ ही, यह आपको अनुमति वाला कोड डालने के लिए भी कहता है. इस पेज पर जाकर, ऐप्लिकेशन को आपकी ओर से एपीआई अनुरोध सबमिट करने की अनुमति दी जा सकती है. यह अनुमति देने पर, पेज पर वह ऑथराइज़ेशन कोड दिखेगा जिसे आपको ऑथराइज़ेशन फ़्लो को पूरा करने के लिए, प्रॉम्प्ट पर डालना होगा.

    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)