Using The Youtube Api With Python and Requests

I’m a big fan of getting to the point. This blog post is going to be a quick, get to the point instruction set with the following mission statement:

As a developer, I want to pull the json data for a youtube channel while only
using requests.

Sure, there are third party libraries that you can pull into your codebase that could help but maybe we only need a tiny portion of the api and we always have requests on hand. As well, you don’t actually need to be a python developer to use this guide, you just need some sort of http client. Let’s get started.

Step 1: Youtube API Credentials

Youtube docs

Use the docs if these steps become out-dated. First we will need to Create a Project. The following pictures should guide you through the steps with little friction.

Create new Google Service Project

We’ll first create our project

Name Google Service Project

Give it a name

Credentials Home Screen

Next you’ll want to navigate to the credentials screen, make sure that you have the correct project name choosen.

Create Api Key

We’ll create an api key.

Api Key

And now we have our key, keep this key secret and don’t share it in github or with other people.

Api Library

Finally we have to enable our project to be able to use youtube.

Api Library

Enable youtube library

And we’re ready to rock.

Step 2: Get the Channel ID

This part can be done by navigating to a channel and looking at the url. The structure should look something like:

https://www.youtube.com/channel/<channel_id>

Copy the channel id for our script.

Step 3: Python + Requests

Search Endpoint Docs

The Search endpoint docs will give you all the information you need for query params if the defaults don’t suit your needs. This little script will give you the minimium to print the json data to your terminal.

import requests

url = 'https://www.googleapis.com/youtube/v3/search'
params = {
    "channelId": <channel_id>,
    "key": <api_key>,
    "part": "snippet,id",
}
response = requests.get(url=url, params=params)
print(response.json())

After you’ve pasted in your values you can run the script with python script.py and you should see a bunch of json data printed to the screen. Happy Deving!