[Tutorial] YouTube API - Making Comments

Hi there! Recently, I wanted to learn the YouTube API with Python but found no good, working tutorials or repositories - so here’s a full guide on how to write comments with the YouTube API with Python!

1. Signup for the YouTube API/OAuth2!

This is really easy! Head over to the Google Developers Console and make a new project.

Then search for the YouTube API
Select ‘YouTube Data API v3’
Press ‘Enable’
Press ‘Create Credentials’

Select the YouTube API as the API you want and then select an Other/UI. Click ‘Create OAuth consent screen’
Select ‘External’ and press ‘Create’
Give your application a name in the name section and press ‘Save’
Go back to Credentials and create an OAuth client ID
Select ‘Desktop Application’ and press ‘Create’
Press the download button (save as ‘client_secret.json’ in the same folder as the code)

2. Set up Python environment

Time to set up Python!

Run this command:

sudo pip3 install google-api-python-client

and this:

sudo pip3 install google-auth-oauthlib

See below post for setting up on Glitch and post any tips setting this up with Glitch below :slight_smile:

3. The Code

Here's the code you need to run this:
#Coded by Eddie's Tech (eddiestech.co.uk) - Adapted from YouTube API example
import os

import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

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

def main():
    channel = input('Channel id: ')
    video = input('Video id: ')
    commenttext = input('Comment text: ')
    # Disable OAuthlib's HTTPS verification when running locally.
    # *DO NOT* leave this option enabled in production.
    #os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret.json"

    # Get credentials and create an API client
    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()
    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    request = youtube.commentThreads().insert(
            "snippet": {
                "channelId": channel,
                "videoId": video,
                "topLevelComment": {
                        "textOriginal": commenttext
    response = request.execute()


if __name__ == "__main__":

4. Run it!

You'll get asked for the channel id. This is the channel id of the channel you want to send the comment as (This has to be the same as the channel you select at OAuth authorisation)

For example:

Next is the video id. This is the id of the video you want to comment on.
For example:

Then, the comment you want to post. This the content inside the comment. This can be anything :slight_smile:

Finally, you’ll be asked to authorise via OAuth2. Take the link and put it in a browser. Press ‘Allow’ anytime it pops up and if it says that the application isn’t verified, just look below that and press ‘advanced options’ or whatever and press ‘Go to You App Name (unsafe)’. It’s not unsafe - this is for the end-user who has been sent to a dodgy unverified app that may use your account maliciously. Since you made this app, there is nothing to worry about here :joy:
Take the code given and put it into the command line.
And there you go! If you get a response back, it should have worked! Check for a comment!

Hope this helps people understand the API!

Please Note: Google has checks and quotas in place to ensure wrongdoers can’t use the API!

If there is anything wrong with or places I can improve on this guide, just let me know and I’ll fix it!



Just tested this on Glitch and it works! Might write a guide on how to do it specifically for Glitch :slight_smile:

1 Like

To use on Glitch


Remix this (https://glitch.com/edit/#!/yt-api-comments-remixme) and add the client_secret.json file as shown above!


Start a new project with nothing inside. Delete everything!

Open the console and do these commands

pip3 install google-api-python-client


pip3 install google-auth-oauthlib

Then do this:

pip3 freeze > requirements.txt

Create file named start.sh
with this inside:

python3 start.py

Create file called start.py with this inside:

print("Use the terminal to start - python3 script.py")

Then make script.py with the code in the main post/topic
Then upload your client_secret.json file
Then run with:

python3 script.py

in the terminal!