Containerized script to test Youtube API in node

How did I come up with a post title like this? bare with me for a few more seconds in this introduction.

I am IT pro, not a Dev. Then, when I tried to test some youtube APIs to start doing some automation. Well, my first issue was. How can I test my API credentials fast? Looking up the answer, I’ve realized: There’s no fast path. Finally, I ended trying some python and node.js scripts from github whit so many errors, because the versions or the modules (some of them were deprecated), that I decided to go back to basic with a post from Youtube Developers called Node.js Quickstart (yeah! it’s very basic, I know)

Then I wanted to do it easy and clean. I decided to use containers. I didn’t want install more packages in my system with so many virtual instances and containers running in it. Also, it will help to keep some conditions over the time. even if I use a different OS later. I won’t need to look up again how to install Node.js in my server.

Also, It’s a great exercise to understand the value of containers and how can I take some script and encapsulated with the things it will need in it: Containerized a script to test Youtube API in node.js

Enable Youtube Data API and obtain your API OAuth 2.0 Client ID

First thing is to enable your Youtube Data API and obtain your client_secret.json file with your credentials.

Containerized script to test Youtube API in node – Google Console – Create a New Project

  • Go to “API & Services” and then to Dashboard

Containerized script to test Youtube API in node – Google Console – APIs & Services

  • Hit “+ ENABLE APIS AND SERVICES”

Containerized script to test Youtube API in node – Google Console – Searching APIs

  • Look up for “Youtube” and select “Youtube Data API V3” and hit “Enable”

Containerized script to test Youtube API in node – Google Console – API Overview

  • Now go to “Credentials” and hit “CONFIGURE CONSENT SCREEN”, and choose “External” and hit “Create”

Containerized script to test Youtube API in node – Google Console – OAuth Consent

  • In my case I just filled the “Name” and I hit the “Save” button

Containerized script to test Youtube API in node – Google Console – OAuth Consent

  • Go Back to Credentials and hit “+ CREATE CREDENTIALS” and choose OAuth client ID

Containerized script to test Youtube API in node – Google Console – OAuth Credentials

  • Choose “Desktop app” and hit “Create” (Desktop App won’t required any URL or redirected link as Web needs )

Containerized script to test Youtube API in node – Google Console – OAuth Credentials

  • Hit download button and have your client_secret.json file on hand.

Next section will create the container image and run it.

Creating Dockerfile to run the script and get token code

I will create the Dockerfile from a node image of my choice. You can also see the files in the repo

FROM node:14.8.0-stretch-slim
RUN npm install googleapis --save
RUN npm install google-auth-library --save
# You have to get your credentials first from Google Console
COPY client_secret.json .
COPY quickstart.js .
RUN mkdir /root/.credentials
#COPY youtube-nodejs-quickstart.json /root/.credentials/youtube-nodejs-quickstart.json
CMD ["node" , "quickstart.js"]

Ok, now it’s time to build our image

sudo docker build -t pinrojas/youtube-test:v0.22 .

And then we’ll run or first container from that new image

sudo docker run -ti -d pinrojas/youtube-test:v0.22 /bin/bash
[root@d4d0ddddd841 /]# node quickstart.js
Authorize this app by visiting this url:  https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly&response_type=code&client_id=663999999128-xoxoxoxoxoxoxoxo3c2omfef0fdsp3p3.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob
Enter the code from that page here:

Use the link in your browser, use your google account to bring access to the app and copy the code you will be shown.

This will create a file create a file with the token at “root/.credentials/youtube-nodejs-quickstart.json” . You can copy that file in the image (I commented that part in the Dockerfile) if you prefer to not have the container running in the background

Next time you run “node quickstart.js” you will see something like this

#sudo docker run pinrojas/youtube-test:v0.22
This channel's ID is UCQaedrSdxScDEBhrgdg. Its title is 'YoutubeDevelopers', and it has 3440 views.

That’s all. Thanks in advance for any comment and share.

See ya!

Source info:

I Made a Bot That Understands YouTube Comments by John Fish

Google Developers: Youtube API > Data API > Node.js Quickstart

Leave a Reply

Your email address will not be published. Required fields are marked *