Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Fetching data from youtube API

$
0
0

I want to have a code where on a given search query I can get details such as video title, description, number of views, number of likes, etc. I am getting an error asTypeError: Cannot read properties of undefined (reading 'snippet')

The code block is below

// Replace with your own YouTube API key from the credentials JSON fileconst API_KEY = 'YOUR_YOUTUBE_API_KEY';function fetchYouTubeData(searchQuery) {  const url = `https://www.googleapis.com/youtube/v3/search?part=snippet&q=${encodeURIComponent(searchQuery)}&key=${API_KEY}`;  const response = UrlFetchApp.fetch(url);  const data = JSON.parse(response.getContentText());  const videos = data.items.filter(item => item.id.kind === 'youtube#video');  const results = [];  videos.forEach(video => {    const videoId = video.id.videoId;    const videoDetails = fetchVideoDetails(videoId);    results.push(videoDetails);  });  return results;}function fetchVideoDetails(videoId) {  const url = `https://www.googleapis.com/youtube/v3/videos?part=statistics&id=${videoId}&key=${API_KEY}`;  const response = UrlFetchApp.fetch(url);  const data = JSON.parse(response.getContentText());  const videoDetails = {    videoId: videoId,    title: data.items[0].snippet.title,    views: data.items[0].statistics.viewCount,    likes: data.items[0].statistics.likeCount,    dislikes: data.items[0].statistics.dislikeCount,  };  return videoDetails;}

Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>