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.


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


Login to google and go to this link:

  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

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 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")

Create function to get the service authenticated

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

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


Finally, add this code:

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

Run the code to get access to your Youtube account.


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.