Change language

Youtube Data API for Video Processing | Set-5

In this article, we will be discussing video rating and getting a video rating.

The examples in this article will require user authentication. So, first we will create the OAuth Credential and install additional libraries. 
Follow the steps below to generate a Client ID and Secret.

  1. Go to developer console Google Google and click Sign In in the top right corner of the page. Sign in with your valid Google account credentials. If you do not have a Google account, first set up an account and then use your login details on the Google Developers homepage.
  2. Now navigate to developer toolbars and create a new project.
  3. Click on the Enable API option.
  4. In the search box, search for Youtube Data API and select the Youtube Data API option that appears in the dropdown. 
  5. You will be redirected to the Youtube Data API information screen along with two options: ENABLE and TRY API .
  6. Click on the ENABLE option to start working with the API.
  7. In the sidebar, under APIs & Services, select Credentials .
  8. At the top of the page, select the OAuth consent screen tab. Select an email address, enter a product name if not already set, and click Save.
  9. On the Credentials tab, select the Create Credentials drop-down list and select OAuth Client ID. OAuth is typically used where authorization is required, such as retrieving a user’s favorite videos.
  10. Select the application type Other, enter the name YouTube Myvideos Data API, click the Create button, and click OK ".
  11. Click the Download button to the right of the client ID to download the JSON file.
  12. Save and rename the file as client_secret.json and move it to working directory.

Install additional libraries using pip :

 pip install --upgrade google-auth google-auth-oauthlib google -auth-httplib2 

Code for video rating.  this example shows you how to rate a video. In this example, we rate a video with a like. You have three options: Like, Dislike, and No (means remove any type of like / dislike rating from the video).

import argparse

import os

import re

import urllib.request

import urllib .error

import google.oauth2.credentials

import google_auth_oauthlib.flow

from googleapiclient.discovery import build

from googleapiclient.errors import HttpError

from google_auth_oauthlib.flow import InstalledAppFlow

 

CLIENT_SECRETS_FILE = ’client_secret.json’

  

SCOPES = [ https://www.googleapis.com /auth/youtube.force-ssl ]

API_SERVICE_NAME = ’youtube’

API_VERSION = ’ v3’

 

def get_authenticated_service ():

flow = InstalledAppFlow.from_client_secrets_file (

CLIENT_SECRETS_FILE, SCOPES)

 

credentials = flow.run_console ()

return build (API_SERVICE_NAME, API_VERSION,

credentials = credentials)

 

def like_video (youtube):

youtube.videos (). rate ( id = ’ZmtLzRJh8n8’ ,

  rating = ’like’ ). execute ()

  
Driver code

if __ name__ = = ’ __main__’ :

 

  youtube = get_authenticated_service ()

 

try :

like_video (youtube)

except urllib.error.HttpError as e:

  print ( ’An HTTP error% d occurred:% s’

% (e.resp.status, e. content))

else :

print ( ’ The rating has been added’ )

Exit:

When you execute the code, you will be prompted for an authorization code. To get the code, you must follow the link indicated on the command line screen above the line: Enter the authorization code. 

Now follow the link and copy and paste the authorization code that you will receive by granting permission. 

From the images of my Youtube account, you can see that there is an addition to the list of related videos.

Code for getRating: in This example shows how to get an authorized user rating for a video list in the options list.

import os

import google.oauth2.credentials

import google_auth_oauthlib.flow

fro m googleapiclient.discovery import build

from googleapiclient.errors import HttpError

from google_auth_oauthlib.flow import InstalledAppFlow

 
# CLIENT_SECRETS_FILE variable
# specifies the name of the file
# contains OAuth 2.0 information
# for this application, including
# client_id and client_secret.

CLIENT_SECRETS_FILE = "client_secret.json"

  
# This OAuth 2.0 scope allows
# full access read / write to authenticated
# user account and requires requests to
# use SSL connection.

SCOPES = [ https://www.googleapis.com/auth/youtube.force-ssl ]

API_SERVICE_NAME = ’youtube’

API_VERSION = ’v3’

  

def get_authenticated_service ():

flow = InstalledAppFlow.from_client_secrets_file (

CLIENT_SECRETS_FILE, SCOPES)

credentials = flow.run_console ()

  return build (API_SERVICE_NAME, API_VERSION,

  credentials = credentials)

 

def print_response (response):

p rint (response)

 
# Remove keyword arguments not set

def remove_empty_kwargs ( * * kwargs):

good_kwargs = {}

 

if kwargs is not None :

for key, value in kwargs.items ():

 

if value:

good_kwargs [key] = value

return good_kwargs

 

def videos_get_rating (client, * * kwargs):

# See full sample for function

kwargs = remove_empty_kwargs ( * * kwargs)

  

response = client.videos (). getRating (

  * * kwargs) .execute ()

 

return print_response (response)

  

if __ name__ = = ’__main__’ :

# When running locally, disable OAuthlib

# HTTP check. When running in

# production * do not * leave this option enabled.

os.environ [ ’OAUTHLIB_INSECURE_TRANSPORT’ ] = ’1’

client = get_authenticated_service ()

 

videos_get_rating (client,

id = ’UPmVTPyE5DM, c0KYU2j0TM4, eIho2S0ZahI’ ,

  onBehalfOfContentOwner = ’’)

 

Exit :

When you execute code, you will be prompted to enter an authorization code. To get the code, you must follow the link indicated on the command line screen above the line: Enter the authorization code. 

Now follow the link and copy and paste the authorization code that you will receive by granting permission. 

As you can see from the output, one of the videos is rated as similar and the other two are not rated.

Links :

  1. https://developers.google.com/youtube/v3/docs/videos/rate
  2. https://developers.google.com/youtube/v3/docs/videos/getRating

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically