admin管理员组

文章数量:1406308

"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 = require('googleapis');
const youtube = google.youtube('v3');
const secrets = require('./secrets.json');

const results = youtube.playlists.list({
  auth: secrets.web.api_key,
  part: 'id',
  id: 'PLvxLmGsmqdZc-GYVeLhS0N_6jfrzEleQm'
});

console.log(results);

Upon the code execution, I receive this:

And there is only the playlist id:

// Node.js
> results.responseContent.body.items
[ { kind: 'youtube#playlist',
    etag: '"gMxXHe-zinKdE9lTnzKu8vjcmDI/cYPhPXIoWu4acW3Qux1D5WZ3WwE"',
    id: 'PLvxLmGsmqdZc-GYVeLhS0N_6jfrzEleQm' } ]

I don't have items property inside the results object:

// Node.js
> request.i
request.isPrototypeOf  
request.init   

And as far as I understand the items property must contain the results, like in the following example:

 // Python
 results = youtube.playlists().list(
    part="snippet,localizations",
    id=playlist_id
  ).execute()

  playlist = results["items"][0]

"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 = require('googleapis');
const youtube = google.youtube('v3');
const secrets = require('./secrets.json');

const results = youtube.playlists.list({
  auth: secrets.web.api_key,
  part: 'id',
  id: 'PLvxLmGsmqdZc-GYVeLhS0N_6jfrzEleQm'
});

console.log(results);

Upon the code execution, I receive this: https://gist.github./SergeyBondarenko/ea6a2aad546ded32e4a9b3cf53228fef

And there is only the playlist id:

// Node.js
> results.responseContent.body.items
[ { kind: 'youtube#playlist',
    etag: '"gMxXHe-zinKdE9lTnzKu8vjcmDI/cYPhPXIoWu4acW3Qux1D5WZ3WwE"',
    id: 'PLvxLmGsmqdZc-GYVeLhS0N_6jfrzEleQm' } ]

I don't have items property inside the results object:

// Node.js
> request.i
request.isPrototypeOf  
request.init   

And as far as I understand the items property must contain the results, like in the following example:

 // Python
 results = youtube.playlists().list(
    part="snippet,localizations",
    id=playlist_id
  ).execute()

  playlist = results["items"][0]

https://developers.google./youtube/v3/docs/playlists/list

Share Improve this question edited Jan 25, 2017 at 8:34 srgbnd asked Jan 24, 2017 at 12:01 srgbndsrgbnd 5,64410 gold badges45 silver badges84 bronze badges 5
  • I tried the youtube.playlists.list method, seems it is the one developers.google./youtube/v3/docs/playlists/list – srgbnd Commented Jan 24, 2017 at 12:03
  • Well that return is an object which also has other objects within it. To access the one you have on display..... youtuberesults.items[0].id keep in mind youtuberesults is = to that result you have in your question. If you have multiple results then you can iterate though them changing the items[?] index. – NewToJS Commented Jan 24, 2017 at 12:06
  • You can use your browser console to test things like this. It's much easier than running the page and refreshing or making multiple queries to test. imgur./a/WuSnl For multiple items you can use something like this assuming the object is like the one I have in the image imgur./a/yiMw8 Ops My bad, just noticed you're using nodejs for this – NewToJS Commented Jan 24, 2017 at 12:13
  • @NewToJS I updated my question. I see only the playlist id but I can't find the videos ids. – srgbnd Commented Jan 24, 2017 at 13:26
  • Ah right i understand. I have just gone to work so i will check back on this question later. If someone hasn't managed to help then i will see if i can be of any help. I use the api too but i don't use nodejs. I run it all through pure javascript. – NewToJS Commented Jan 24, 2017 at 13:47
Add a ment  | 

2 Answers 2

Reset to default 4

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./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.

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./vi/EOpQdJ5F5TI/default.jpg',
        width: 120,
        height: 90 },
     medium:
      { url: 'https://i.ytimg./vi/EOpQdJ5F5TI/mqdefault.jpg',
        width: 320,
        height: 180 },
     high:
      { url: 'https://i.ytimg./vi/EOpQdJ5F5TI/hqdefault.jpg',
        width: 480,
        height: 360 },
     standard:
      { url: 'https://i.ytimg./vi/EOpQdJ5F5TI/sddefault.jpg',
        width: 640,
        height: 480 } },
  channelTitle: 'Sergey Bondarenko',
  playlistId: 'PLvxLmGsmqdZc-GYVeLhS0N_6jfrzEleQm',
  position: 0,
  resourceId: { kind: 'youtube#video', videoId: 'EOpQdJ5F5TI' } }

本文标签: javascriptHow to get video ids from an YouTube playlistStack Overflow