Do it your self hobby code

How to obtain authorization—Python Youtube API

In this tutorial, we will write a simple python program will help you to obtain a user authorization on Youtube API by writing a simple Python program.

Requirements:

  1. You should have a google account
  2. Python 3.5+
  3. Pip management tool

Step 1: SETUP GOOGLE API PROJECT

Login to google and go to this link: https://console.developers.google.com/start/api?id=youtube

  1. Create Project
  2. Go to API’s Overview >> Configure Consent Screen
    • Create a user
    • Type: External and then save
    • Enter Product name and the save
  3. Go to API’s Overview >> Credentials
    • Create credentials >> OAuth client ID
    • Application type: Others
    • Name: Python3
      1. Create OAuth client
      2. Download JSON and rename it to client_secret.json
        • Save this json file to a folder where our Python program will be saved.
  4. Enable Youtube Data API
    • On API & Service Dashboard:
    • Go to Library
    • Find Youtube Data API V3
    • Enable it

STEP2: SETUP GOOGLE API’s Client Library for Python

Run windows command prompt (or cmd) and type this:

pip install --upgrade google-api-python-client
pip install --upgrade google-auth-oauthlib google-auth-httplib2
pip install oauth2client

STEP 3: Writing a simple Python code for User Authentication

Create a new python file and name it as authentication.py

Import the libraries, paste the following codes:

import os, sys
import httplib2
import google_auth_oauthlib.flow
import googleapiclient.discovery
#import googleapiclient.errors
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.tools import run_flow
from oauth2client.client import flow_from_clientsecrets

Set global variables. The client_secret.json should be saved on the same directory where our Python code is located. Later client-oauth2.json will be created on the same directory once we run the code.

CLIENT_SECRETS_FILE = os.path.join(sys.path[0], "client_secret.json")
CLIENT_OAUTH2_FILE  = os.path.join(sys.path[0], "client-oauth2.json")
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
MISSING_CLIENT_SECRETS_MESSAGE = ""

Create function to get the service authenticated

def get_authenticated_service(args):
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
    scope=YOUTUBE_READ_WRITE_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)
  
  storage = Storage(CLIENT_OAUTH2_FILE)
  credentials = storage.get()

  if credentials is None or credentials.invalid:
    credentials = run_flow(flow, storage)

  return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    http=credentials.authorize(httplib2.Http()))

Finally, add this code:

if __name__ == "__main__":
    client = get_authenticated_service()

Run the code to get access to your Youtube account.

python authentication.py

On my computer, I got this:

Your web browser should open. If you are logged in on google on the same browser, you have see like this:

Select your prepared Youtube channel.

You will get this warning. Don’t worry about this. Just click advance and click “Go to [Your product name] (unsafe)”

Click Allow to grant permission

Finally, you should see this message on your browser: “The authentication flow has completed”.

And on my computer, it shows authentication successful.

If you check your working directory, an OAuth access token file named as client-oauth2.json was created.

Once you have these two files namely client_secret.json and client-oauth2.json, you dont have to authenticate again and again.

From now on, you can make a request on Youtube API like listing your subscriptions, liked videos and channel activities.

To test this, please read my next post: How to list subscription

Leave a Comment

Your email address will not be published.