একটি প্রথম অনুরোধ পাঠানো হচ্ছে

দ্রষ্টব্য: YouTube বিষয়বস্তু আইডি API YouTube সামগ্রী অংশীদারদের দ্বারা ব্যবহারের উদ্দেশ্যে এবং সমস্ত বিকাশকারী বা সমস্ত YouTube ব্যবহারকারীদের কাছে ��্যাক্সেসযোগ্য নয়৷ আপনি যদি YouTube Content ID API-কে Google API কনসোলে তালিকাভুক্ত পরিষেবাগুলির মধ্যে একটি হিসাবে দেখতে না পান তবে YouTube পার্টনার প্রোগ্রাম সম্পর্কে আরও জানতে YouTube সহায়তা কেন্দ্র দেখুন৷

এই ধাপে ধাপে টিউটোরিয়াল ব্যাখ্যা করে যে কীভাবে একটি স্ক্রিপ্ট তৈরি করতে হয় যা ContentOwnersService এর সাথে সংযোগ করে এবং একটি প্রদত্ত সামগ্রীর মালিক সম্পর্কে তথ্য পুনরুদ্ধার করে৷ টিউটোরিয়ালের শেষে একটি সম্পূর্ণ কোড নমুনা প্রদান করা হয়েছে। যদিও এই কোডটি পাইথনে লেখা হয়, অন্যান্য জনপ্রিয় প্রোগ্রামিং ভাষার জন্য ক্লায়েন্ট লাইব্রেরিও পাওয়া যায়।

প্রয়োজনীয়তা

API অনুরোধ পাঠানোর জন্য একটি স্ক্রিপ্ট তৈরি করা

YouTube Content ID API অনুরোধ পাঠানোর জন্য কীভাবে একটি স্ক্রিপ্ট তৈরি করতে হয় তা নিম্নলিখিত ধাপগুলি ব্যাখ্যা করে৷

ধাপ 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 অনুরোধগুলি সম্পাদন করার জন্য স্ক্রিপ্টটিকে অনুমোদন করতে স্ক্রিপ্টটি চালাতে সক্ষম করে৷

ধাপ 2a: একটি client_secrets.json ফাইল তৈরি করুন

YouTube Content ID API-এর একটি 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"
  }
}

ধাপ 2b: আপনার স্ক্রিপ্টে প্রমাণীকরণ কোড যোগ করুন

ব্যবহারকারীর প্রমাণীকরণ এবং অনুমোদন সক্ষম করতে, আপনাকে নিম্নলিখিত import বিবৃতি যোগ করতে হবে:

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

এর পরে, আমরা ধাপ 2a এ কনফিগার করা ক্লায়েন্ট সিক্রেট ব্যবহার করে একটি FLOW অবজেক্ট তৈরি করব। ব্যবহারকারী যদি ব্যবহারকারীর পক্ষ থেকে 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)

ধাপ 2c: 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 ফাংশন এমন একটি সংস্থান তৈরি করে যা একটি 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'])

সম্পূর্ণ আবেদন

এই বিভাগে কিছু লাইসেন্সিং তথ্য এবং স্ক্রিপ্টে অতিরিক্ত মন্তব্য সহ সম্পূর্ণ অ্যাপ্লিকেশন দেখায়। প্রোগ্রাম চালানোর দুটি উপায় আছে:

  • এই কমান্ডটি একটি ব্রাউজার উইন্ডো চালু করে যার মাধ্যমে আপনি প্রয়োজনে প্রমাণীকরণ করতে পারেন এবং API অনুরোধগুলি জমা দেওয়ার জন্য অ্যাপ্লিকেশনটিকে অনুমোদন করতে পারেন। আপনি যদি আবেদনটি অনুমোদন করেন, শংসাপত্রগুলি স্বয়ংক্রিয়ভাবে স্ক্রিপ্টে ফিরে আসবে৷

    python yt_partner_api.py --content_owner_id=CONTENT_OWNER_ID.

    দ্রষ্টব্য: আপনি আপনার CMS অ্যাকাউন্টের অ্যাকাউন্ট সেটিংস পৃষ্ঠায় আপনার অ্যাকাউন্টের জন্য CONTENT_OWNER_ID মান খুঁজে পেতে পারেন৷ মানটিকে সেই পৃষ্ঠার অ্যাকাউন্ট তথ্য বিভাগে 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)