YouTube APIs , the way to the automatic mode

YOUSSEF ALOUANI
4 min readDec 7, 2021

in the first day in my new job as a junior developer , i toke a mission to go and understand YouTube APIs , and while searching in the internet i found a lot of

information but in way not clear , so i decided to make things clear , cause the key to growth is sharing knowledge .

before jump into the YouTube world we have to understand the Authentication and authorization in the World of the public APIs

API Key Vs Client Id ( OAuth2 ):Authentication and authorization in Public API

any public API that you buy for it , needs an unique identifier to tie it to your project for authentication to manage traffic or costs

API key and Oauth are just a unique identifier

Client Id : if your app is using Oauth2 protocol , then use Oauth Client Id , Oauth is used to create acces token , which in turn is a unique identifier , however , the user needs to agree a consent .

API Keys: an API Key is a unique identifier that you generate using console . the advantage is the user does not require an user action or consent , but you cannot use the API key for authorization unlike OAuth2 . use and API key when the data you want is public and does not need a user authentication

YouTube APIs

from the official documentation of google :

YouTube Data API lets you incorporate functions normally executed on the YouTube website into your own website or application. The lists below identify the different types of resources that you can retrieve using the API. The API also supports methods to insert, update, or delete many of these resources.

for use the YouTube APIs , we need to pick one of those choice :

1- call the API by yourself using a specific language that support HTTP or use postman or the browser:

the APIs EndPoints:

youtube API data V3 :

https://www.googleapis.com/youtube/v3/

and you can add the operations that you want to add for do what you want and your API key, for example if you want the result of searching about nodejs keyword:

https://www.googleapis.com/youtube/v3/search?apikey&type=video&part=snippet&q=nodejs

see the documentation for more details: https://developers.google.com/youtube/v3/docs

YouTube Search suggestion

suggestquries.google.com/complete/serch/search?client=<browserName>yt&q=<keyword>

get images of videos:

https://i.ytimg.com/vi/<video-id>/n.jpg

n is between 0 and 3

n can take that values in this array :

[default,mq1,mq2,mq3.mqdefault,hq1,hq2,hq3,hqdefault]

YouTube APIs with Client Library:

in this case we will you NodeJs lib:

for this reason google provide a library called googleapis:

const {google} = require('googleapis');const youTube = google.youtube({version:"v3",auth:process.env.API_KEY})

and to use this APIs in goode way we can do this

var express = require('express');var router = express.Router();var youTube = require('../youtube/index');/* search API provide search on youtube via an API */router.get('/videos/',async function(req, res, next) {let searchQuery = req.query.searchlet data =await youTube.search.list({part:'snippet',q: searchQuery,type:"videos"});console.log(searchQuery);console.log(data.data.items);res.send(data.data.items)});router.get('/channelsbyid/:id',async function(req, res, next) {let data =await youTube.channels.list({part: 'snippet,contentDetails,statistics',id:req.params.id})console.log(data.data.items);res.statusCode = 200res.send(data.data.items)});router.get('/channelsbyusernames/:username',async function(req, res, next) {let data =await youTube.channels.list({part: 'snippet,contentDetails,statistics',forUsername: req.params.username})console.log(data.data.items);res.statusCode = 200res.send(data.data.items)});router.get('/activities/:id',async function(req, res, next) {let data =await youTube.activities.list({part: 'snippet',channelId :req.params.id}).catch((err) =>{console.error(err)})console.log(data.data.items);res.statusCode = 200res.send(data.data.items)});module.exports = router;

for an other part of youtube APIs we have

var express = require('express');var router = express.Router();var youtube = require('../youtube/index');/* GET category of channels and videos*/router.get('/video/:id', async function(req, res, next) {let videoId = req.params.idlet data = await youtube.videoCategories.list({id:videoId,part:'snippet'})console.log(data.data);res.send(data.data)});router.get('/channel/:id', async function(req, res, next) {let videoId = req.params.idlet data = await youtube.channelSections.list({channelId:videoId,part:'snippet'})console.log(data.data);res.send(data.data)});module.exports = router;var express = require('express');var router = express.Router();var youtube = require('../youtube/index');/* GET category of channels and videos*/router.get('/video/:id', async function(req, res, next) {let videoId = req.params.idlet data = await youtube.videoCategories.list({id:videoId,part:'snippet'})var express = require('express');
var router = express.Router();
var youtube = require('../youtube/index');
/* GET category of channels and videos
*/router.get('/video/:id', async function(req, res, next) {
let videoId = req.params.id
let data = await youtube.videoCategories.list({
id:videoId,
part:'snippet'
})
console.log(data.data);
res.send(data.data)
});router.get('/channel/:id', async function(req, res, next) {let videoId = req.params.id
let data = await youtube.channelSections.list({
channelId:videoId,
part:'snippet'
})
console.log(data.data);
res.send(data.data)
});module.exports = router;console.log(data.data);res.send(data.data)});router.get('/channel/:id', async function(req, res, next) {let videoId = req.params.idlet data = await youtube.channelSections.list({channelId:videoId,part:'snippet'})console.log(data.data);res.send(data.data)});module.exports = router;

thanks for reading

--

--

YOUSSEF ALOUANI

junior software engineer , the performance if it was a person , intrested in microservices architecture and System Design . I believe in sharing Knowledge