Uploading a New Reference

  • The YouTube Content ID API is only available to YouTube content partners and requires access through the Google API Console.

  • Uploading a reference via the YouTube Content ID API involves creating an asset, configuring its ownership and match policy, and then uploading the reference file.

  • The process uses OAuth 2.0 for authorization, which involves a client_secrets.json file and the storage of credentials for later use.

  • The code provided demonstrates these steps using a Python script, and it uses httplib2 to manage API requests after they're authorized.

  • The script does not include error handling and requires specific parameters such as a reference file, asset title, and content owner name to execute properly.

Note: The YouTube Data API is intended for use by YouTube content partners and is not accessible to all developers or to all YouTube users. Access requires a YouTube Content Manager account. If you have a YouTube Content Manager account but don't see the YouTube Data API as one of the services listed in the Google Cloud console, contact your assigned partner manager or partner support.

This code sample demonstrates how to upload a reference using the YouTube Content ID API. To upload a reference, you must first create an asset and configure the asset's ownership and match policy. This example walks through all of these steps.

This example is presented as the series of steps involved along with the relevant sections of the code. You can find the entire script at the end of this page. The code is written in Python. Client libraries for other popular programming languages are also available.

The sample script does not do any error handling.

Requirements

In this step, we'll incorporate OAuth 2.0 authorization into the script. This enables the user running the script to authorize the script to perform API requests attributed to the user's account.

Create a client_secrets.json file

The YouTube Data API requires a client_secrets.json file, which contains information from the Cloud console, to perform authentication. You also need to register your application. For a more complete explanation of how authentication works see the authentication guide.

 {
  "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"
  }
}

Add authentication code to your script

To enable user authentication and authorization, you need to add the following import statements:

from datetime import datetime
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import argparser, run_flow

Next, we'll create a FLOW object using the client secrets configured in step 2a. If the user authorizes our application to submit API requests on the user's behalf, the resulting credentials are stored in a Storage object for later use. The user will need to reauthorize our application if the credentials expire.

Add the following code to the end of the main function:

  # 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(FLOW, storage, args)

Create httplib2 object and attach credentials

After the user authorizes our script, we create an httplib2.Http object, which handles API requests, and attach the authorization credentials to that object.

Add the following import statement:

  import httplib2

And add this code to the end of the main function:

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

Obtain services

After successful authorization, the code obtains the necessary services for the operations it will perform. It first creates a service object that provides access to all YouTube Content ID API services. The code then uses the service object to obtain the four resource-specific services it calls.

from apiclient.discovery import build

# ...

service = build("youtubePartner", "v1", http=http, static_discovery=False)
# ...
asset_service = service.assets()
# ...
ownership_service = service.ownership()
# ...
match_policy_service = service.assetMatchPolicy()
# ...
reference_service = service.references()

Create an Asset

The first step in uploading a reference is to create the asset. First we create a simple metadata object that only sets the asset's title. The code then adds that object to the asset_body, which also identifies the asset's type. The asset_body object, in turn, is used as input to the asset_service.insert() method. That method creates the asset and returns its unique ID.

def _create_asset(service, title, metadata_type):
  metadata = {'title': title}
  asset_body = {'metadata': metadata, 'type': metadata_type}
  # Retrieve asset service.
  asset_service = service.assets()

  # Create and execute insert request.
  request = asset_service.insert(body=asset_body)
  response = request.execute()
  logger.info('Asset has been created.\n%s', response)
  asset_id = response['id']
  return asset_id

Update the Ownership

After creating the asset, the script configures the asset's ownership. This example indicates that the content owner owns 100% of the asset but that that ownership is limited to Poland (PL) and Great Britain (GB).

def _create_asset_ownership(service, asset_id, owner_name):