Youtube API to Get Upload Video List (Node.js)

Jul 26, 2021

Enable YouTube API and obtain credentials

Install dependencies

npm install googleapis --savenpm install google-auth-library --save

Code

const {google} = require('googleapis');const GOOGLE_API_KEY = ...// youtube username or channel id is required// youtube username// const username = ... // or youtube channel id// https://www.youtube.com/channel/UCqK_GSMbpiV8spgD3ZGloSwconst channelId = 'UCqK_GSMbpiV8spgD3ZGloSw'const youtube = google.youtube({  version: 'v3',  auth: GOOGLE_API_KEY});let res = nulllet channelId = nulllet playlistId = nullres = await youtube.channels.list({  part: 'snippet,contentDetails', // ,statistics  // forUsername: username,  id: channelId,  // headers: headers,});for (const channel of res.data.items) {  console.log(channel.id, channel.snippet.title)  // console.log(channel.contentDetails)  // channelId = channel.id  playlistId = channel.contentDetails.relatedPlaylists.uploads}let nextPageToken = nulllet page = 1while (page == 1 || nextPageToken) {  res = await youtube.playlistItems.list({    part: 'id,snippet,contentDetails', // ,    playlistId: playlistId,    pageToken: nextPageToken,    maxResults: 20, // 50    // headers: headers,  });  nextPageToken = res.data.nextPageToken  const videos = res.data.items.map(video => {    return {      url: `https://www.youtube.com/watch?v=${video.contentDetails.videoId}`,      title: video.snippet.title,      date: new Date(video.snippet.publishedAt),    }  })  video.forEach(video => {    console.log(url, title, date)  })  page += 1}

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.