Python YouTube API Tutorial: Getting Started - Creating an API Key and Querying the API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there how's it going everybody in this video we're going to be learning how to get started with using Python and the YouTube API so this will allow us to connect to YouTube's API to gather information from YouTube and do some analysis from within Python now there are a lot of things that I've used us for in the past so for example I've used this API to tally up the total duration of specific playlists to track channel subscriptions to do some analysis on trending videos and also to grab the most popular video from specific playlists and things like that and I'll make videos for all of those topics in the near future but before we do that we'll need to get started by creating an API key and connecting to Google Google's API in Python so that's what we're going to be doing in this video well learn how to create an API key how to find all the information that we need in the documentation and also make some simple API requests to make sure that everything is working properly so with that said let's go ahead and get started so first of all we're going to need to sign up for an API key in order for for us to use the YouTube API so to do this I'm going to navigate to my developers console over here on Google to get setup with all of this information so this is at console dot developers.google.com and I'll have links to all of these sites in the description section below now when you navigate to this page it could look slightly different than mine depending on whether or not you've already created a project I've deleted my previous project before this video so that I can start from scratch so if you haven't created a project yet then you should see a page that looks something like this so we should see a prompt over here on the far right for creating a new project and all you need to do is click on that and then give your project a name so for my name here I'm just going to type in YouTube API and then I will create and once we have a project created it should look something like this so first of all Google has a lot of API s and if this video is interesting to all of you then we can explore some of the other API is available to us through Google as well but we're wanting to use the YouTube API for this video so we need to enable this API before creating our key so to do this we can see that it says we don't have any API is available yet to use and that we need to enable one so I'm just going to go to their API library here and now we can search for an API so I'm just going to search for YouTube and we can see that we get a few different results here the one that I'm going to be using is going to be the YouTube data API so I'm going to click on that and then we will enable this and now that we've enabled the use of this API for our project now we can create an API key to do this it's linking us to a page up here that says create credentials so I'm going to go ahead and click on that and then it's going to ask us a couple of questions here about our what kind of credentials we need so which API are we're using we've only enabled one so I'm going to choose YouTube data API where will you be calling the API from I am going to be using let's see I'm just gonna pick other non-ui cron job daemon here at the bottom since I'm just gonna be running this from a Python script but if you're running it from a web browser or a web server then you can select whatever option you're going to be using it for but I'm gonna go for non UI here and now we have this question what data will you be accessing so now it's going to ask us what type of credentials we don't want to create we can either create an API key which is used to access public information from YouTube and that's what we'll be doing in this video or we can create an OAuth client which is used for accessing private information from an account if a user is properly authenticated but so that would be user data here now in this video we're just accessing public information that anyone can find on YouTube so I'm gonna click on public data and now we can click on what credentials do I need and it should go ahead and tell us that we just need an API key and go ahead and give us our API key here now you can see here that there's also that they recommend restricting this key before it's used in production so you can restrict a key to like a single IP address or a couple of different other options there I'm not gonna do that in this video because I'm just going to delete this API key after this video is done but for now I'm gonna go ahead and copy that and now I'm going to click on done okay so now we've created our API key and on this page it will show us all of the keys I have available for this project so we can see that I have one API key here and that if I need to copy this again then I can just click this right here to copy this key and again don't worry I'm going to delete this API key after this video so you don't have to worry about anyone stealing this or anything like that now I know that I'm gonna be using this API key later within my Python script so I'm gonna go ahead and paste this into my empty script for later use and this is the script that I'm going to be using to write the code to connect to the API later so I'm going to just paste that here within a string now for the purpose of making things easy for this video I'm just gonna have this API key directly in my script here but when you work with api's you definitely want to keep the secret if someone gets ahold of your API key then they'll be able to access the API service on your behalf so if you ever think that an API key is lost or stolen then you'll need to delete it and create a new one now one way to keep these tokens and passwords safe is to put them in an environment variable or a secret configuration file that isn't committed to any of your repositories and I have a video on using environment variables for secret keys so if you'd like to see what that looks like then I'll leave a link to that video in the description section below okay so now that we have our API key we're ready to use the YouTube API using Python now I mentioned this in my last video when I worked with the patreon API but the documentation for these api's are going to be our best friend when developing anything like this luckily Google has some terrific documentation for their YouTube API now to find this you could just do a Google you know a simple google search for YouTube API but instead of the first result that you'd find on Google that takes you to their official documentation I'm instead going to open their Python client github imitation for now now I find this to be extremely useful since it has all the methods and arguments that we're going to be able to use within Python and they're all listed here now I've got this page pulled up in my browser already but like I said I'll be sure to link all of these pages and the description section below so that you can find them all right okay so this is the github page here and you can see that this is the Google API user and this is the Google API client for Python and I think they did a great job of documenting everything here so that it allows us to work with this API easily now if we scroll down then it will give us some installation instructions here if I scroll down to installation then it's going to tell us what we need to pip install in order to work with this API so this is the package that we need to install here Google API Python client and that's the same on Windows now they're showing you how to activate a virtual environment now you don't have to use a virtual environment in order to install this but I always recommend using these since it's just a good practice to use a different environment for each project now if you'd like to learn more about virtual environments then I'll be sure to leave a link to my video on that topic in the description section below as well okay so I'm going to go ahead and install this Google API Python client using pip now I've already got a virtual environment activated here so I'm just going to say pip install and install this Google API Python client so let's go ahead and install that okay so once that's finished installing let's go back to the browser and I'll show how we can use the documentation to see exactly what we need in order to work with this API correctly so I've got the browser back open here now I'm gonna scroll up here to the top and we can see here that it says to get started see the docs folder so I'm gonna open up this Doc's folder and a new tab here and then I'm going to scroll down here to the bottom and click on getting started and now let's take a look at what we need to get started using this API so we can see here that we've already done some of this so it tells us that if we don't have a Google account that we need to create one that we need to go to our Google API console and create a project and things like that we need to create an API key we've already done all of this so now that we have our API key we can go down here to building and calling a service this is the step that we are currently at so let me go ahead and read this real quick so it says that this section describes how to build an API specific service object make calls to the service and process the response whether you are using a simple or authorized API access you can use the build function to create a service object it takes an API name and API version as arguments you can see the list of all API versions at the supported api's page the service object is constructed constructed with methods specific to that given API so to create it you can do the following ok so we can see that we need to use this build function here and we need to pass in the API name and the API version let me go ahead and open up this build function here and see what this looks like so let me make this browser a little larger so that you all can see so we can see that we have this build function here and we need the service name we need the version for that service and then we can see here that it also takes in a developer key so the developer key is our API key so let me go back here so we need to know our service name and version an order to get to connect to a specific API and to find this they have the supported api's page listed here for us so that we can find the one that we're looking for so I'm going to open this up in a new tab and if I look at this then these are all their API s and API versions now instead of searching through this entire thing I'm just going to do a search for YouTube here so YouTube is the service name and we can see that it's all lowercase and Version three is the version now if I click on this v3 here I'm going to open this up in a new tab these are actually all of the instance methods available to us one we connect to this YouTube service so I'm going to leave this page open here so that we can reference this later but now I'm going to go back here to this building the service object so they actually show us here how to connect to a service by importing this build function and then passing in all of that information that we just saw within the documentation and we'll need to pass in our API key into that function as well so I'm gonna copy this import statement here and paste this into our script so I'm gonna copy that go back to the script and let's import this at the top okay and now from reading that documentation we know that we can connect to the YouTube service by using this build function so I'm gonna call this service YouTube and then I will pass in the build function and that was the first we need to pass in the service name which was YouTube all lowercase and then the version was v3 and then it also took in developer key as an argument as well and that is our API key and again if you're ever working with an API and forget any of this information then you can always go back to the documentation just to be sure okay so we have our YouTube service here and if we typed everything in correctly then this should be ready to use so real quick let's test this by making a very simple API request so I'm just going to fetch my channel information from YouTube and see if I get a response now in order to know how to do this we can go back to the documentation and look at the instance methods available to us with this YouTube service so I'm going to go back to the documentation here and open back up where we had where we clicked on this YouTube version 3 and it gave us all of our instance methods here that are available to us and we can see here towards the top of these instance methods that we have one called channels so we have this channels method and that returns a resource for that API route so if I open this in a new tab then can see that we that the resource that we have a few more methods here now the one that I'm going to be interested in is this list method so that we can list specific channels and we can see the arguments that this list method takes so we need to pass in this part argument here which is basically the information that we want included in the response now look I know that we're probably sick of looking at documentation instead of actually writing some code here but I'm trying to give a realistic example of what it's like to actually learn how to work with these api's there's a lot of different information to track down so the last bit of information that we need right now is what we can actually pass in as an argument for this part parameter here and then we'll be able to make our first API request so to find this information I'm just going to open up the official API documentation so right now we're on the Google API for Python github documentation but I'm going to open up the official YouTube documentation now and I already have this open in the browser but if you google YouTube API then this will be the first result okay so to find more information about what we can pass in to that channels resource and what we can get back we're going to need to click on references up here and then we want to go down to this channels reference and remember I was wanting to list channels so we could either go to an overview to see an overview of this API route or I could go to list which is specifically the route that I'm looking for and then we can see that it gives us common use cases we can even take a look at some code here and they have some Python code examples here so they did a really good job of documenting all this now what I need is I need to scroll down to parameters to see what exactly we can pass in to this part which is a required parameter so right here we can see all the strings that we can pass in to this part parameter so let's say that I want the statistics of a certain Channel then I can just pass in statistics here as an argument so let me go back to our script and I'll try this out so I'm going to go back to our script here and now in order to do this I can create a request and then I'm going to use this YouTube service here and remember we saw those YouTube that YouTube resource earlier and all those instance methods that we had available and one of those was the channels instance method and within there we want to list the channels and now we need to pass in a couple of required parameters so first we need to pass in that part and I said that I was going to look up the statistics for a channel and finally how do we find our what channel we're actually getting statistics for so if I go back to the documentation one more time and look at our list method that we're using up here at the top we can see one here that is a four username so we could either pass in like a channel ID or we could pass in a channel username using the for username parameter so that's what I'm going to use so I'm going to just say for username and that was a capitalized do I believe in that documentation and now my channel username is actually Schaefer five okay so that should create a request to the YouTube API but now in order to get a response we're going to have to execute that request so I'll say request dot execute and now let's just print out our response here so if I save this and run it then we see that we did get a response here so if I look at this response then we can see that I get my that we had one total result and that it is a of kind of a YouTube channel gives me my channel ID and things like that I look down here at my video count it says 226 so that sounds about right for my channel so it does seem like our API connection is working so if we wanted to see stats for another YouTube channel then all we need to do is pass in their username here into this function now a username might be different than their display name on YouTube so for example my display name is Cory Schafer but my username is Schaefer 5 to find the username you'd actually have to go to their YouTube page and then look in the URL so for example let's say that I wanted to look up let's do some Texas channel so if I I think his username is probably syntax but let's go ahead and see if that is correct I'll save this and run it and we can see that yeah this is probably him because he has eight hundred and ninety thousand subscribers and he's definitely blowing me out of the water with the video count there he has over 1200 video so that's very impressive ok so now that we know that we have this API working here within Python now we can use this API to grab just about any public information that we'd want and that's what we're going to be doing in the next few videos so I'll show how we can use this API to do some really useful things that aren't directly available from within YouTube itself so for example in the next video we're gonna learn how to write a script that calculates the duration of videos in a specific playlist and I think this is extremely important information to know and I'm honestly surprised that Google hasn't added this information to the actual playlist data on the actual website so for example if you wanted to watch one of my playlists then you'd likely want to know how long that playlist is so that you can get an idea of how long it would take you to learn that certain topic but currently YouTube doesn't have that information directly on their site so I use scripts like this all the time when I'm trying to calculate information like this to make my life a little bit easier so definitely be sure to check out that next video when it's released if that sounds like something you'd be interested in ok so I think that is going to do it for this video hopefully now you have a pretty good idea for how you can get started with the YouTube API and accessing this information within Python but if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that these these ways to simply LIKE the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon or YouTube and there are links to those pages and the description section below be sure to subscribe for future videos and thank you all for watching you
Info
Channel: Corey Schafer
Views: 105,944
Rating: 4.9725738 out of 5
Keywords: python, youtube api, youtube-api, python youtube, google-api-python-client, youtube data api, youtube, python automate, automate the boring stuff, python api, python tutorial, corey schafer, data science, data analysis, python web api, python automation, python real world examples, api tutorial, application programming interface
Id: th5_9woFJmk
Channel Id: undefined
Length: 20min 49sec (1249 seconds)
Published: Fri May 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.