10 Jan 2018 - Tags: programming, python, youtube
This is done with python 2 becuase most of the Youtube DATA libraries seem to support Python 2 better than 3. There may be versions of some of the libraries for Python 3 but I did not spend anytime investigating this route.
This link goes over most of the setup process pretty well: https://developers.google.com/youtube/v3/getting-started
-
Go to the developers console and create a new project https://console.developers.google.com/
-
Now on the project page go to Credentials and create an OAuth 2 Client ID of type Other. Now download the json file which we will use to authenticate our app.
- To install the Youtube Data library you can use pip. I got the instructions from the Youtube api-samples github page. As previously mentioned, we will use Python2.
pip install --upgrade google-api-python-client
- Now install other google libraries that will also be needed
pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2
- Now move the downloaded file and rename it to client_secrets.json, since that is what the script is expecting. I am using my_uploads.py as a reference. If you run this script now you will be prompted to visit a URL and enter the resultingauthorization code.
cramsan@kururu ~/git/cramsan.github.io/scripts $ python2 demo.py Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=743336358314-o1grlivilob3pb4k3ra4b45kulgu20oa.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly&state=plvu3RkeSZL8Laj3wtz6IHsuvXBgY9&prompt=consent&access_type=offline Enter the authorization code:
- This happens becuase the OAuth2 credentials previously created requiere manual copy/paste. There are four types of redirect methods and in this case we are are using the Manual copy/paste mode. Now you have everything you need to access the Youtube Data API. You can run the examples from the Youtube api-samples github page or the scrips from the repo of this blog.
Acessing the API is rather simple.
- Create the YoutubeAPI object
- Now get the list of uploaded videos. This function will return the Id of the playlist that represents the upload list of the authenticated channel
- Now that you have the Id of the uploads playlist, you can use it to iterate over every video