How to get video ids from an YouTube playlist?

后端 未结 2 1307
日久生厌
日久生厌 2020-12-20 05:43

\"googleapis\": \"^16.1.0\"

I have a playlist where there are two videos. How can I get the videos ids?

I tried this:

// Node.js
const google         


        
相关标签:
2条回答
  • 2020-12-20 06:10

    I used the wrong method to retrieve the playlist videos ids. The method to use is playlistItems:

    // Node.js
    const { google } = require('googleapis');
    const youtube = google.youtube('v3');
    const secrets = require('./secrets.json');
    
    youtube.playlistItems.list({
      key: secrets.web.api_key,
      part: 'id,snippet',
      playlistId: 'PLvxLmGsmqdZc-GYVeLhS0N_6jfrzEleQm',
      maxResult: 10,
    }, (err, results) => {
      console.log(err ? err.message : results.items[0].snippet);
    });
    

    Results:

    { publishedAt: '2017-01-21T13:16:09.000Z',
      channelId: 'UCSD9RekiljT4DzK_6VvYY6A',
      title: 'Monster (feat. Jay-Z, Nicki Minaj, Rick Ross, Bon Iver)',
      description: 'Oficial',
      thumbnails:
       { default:
          { url: 'https://i.ytimg.com/vi/EOpQdJ5F5TI/default.jpg',
            width: 120,
            height: 90 },
         medium:
          { url: 'https://i.ytimg.com/vi/EOpQdJ5F5TI/mqdefault.jpg',
            width: 320,
            height: 180 },
         high:
          { url: 'https://i.ytimg.com/vi/EOpQdJ5F5TI/hqdefault.jpg',
            width: 480,
            height: 360 },
         standard:
          { url: 'https://i.ytimg.com/vi/EOpQdJ5F5TI/sddefault.jpg',
            width: 640,
            height: 480 } },
      channelTitle: 'Sergey Bondarenko',
      playlistId: 'PLvxLmGsmqdZc-GYVeLhS0N_6jfrzEleQm',
      position: 0,
      resourceId: { kind: 'youtube#video', videoId: 'EOpQdJ5F5TI' } }
    
    0 讨论(0)
  • 2020-12-20 06:18

    With Axios you can do something like that :

    import axios from "axios";
    const KEY = "";
    
    const getPlayListItems = async playlistID => {
        const result = await axios.get(`https://www.googleapis.com/youtube/v3/playlistItems`, {
          params: {
            part: 'id,snippet',
            maxResults: 10,
            playlistId: playlistID
            key: KEY
          }
        });
        return result.data;
      };
    
      getPlayListItems("PlaylistID").then(data => {
        data.items.forEach(element => {
            console.log(element.snippet.resourceId.videoId)
    });
    

    This will print all videoId of a given playlist, up to position 10.

    0 讨论(0)
提交回复
热议问题