YouTube API - get tags for all videos with playlis

2019-07-11 03:45发布

问题:

I'm querying YouTube channels to retrieve playlist metadata like this:

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=5&playlistId={PLAYLIST_ID}&key={API_KEY}

This query returns an array of all of the videos on the channel. Each object in that array includes various fields that provide metadata on each video, but none of these fields include the tags associated with the video. I can get that data using a query like this:

https://www.googleapis.com/youtube/v3/videos?key={API_KEY}&fields=items(snippet(title,description,tags))&part=snippet&id={VIDEO_ID}

The problem with that is that now I need to issue a separate query for every single video that comes back in the first query.

So, my question is, is there a way that I can get these tags included as part of the initial JSON object from the first query? Can I add any parameters that will request this data be included with that response?

Thanks for any help!

回答1:

Short answer: No.

The PlaylistItems: list documentation does not show a way to get video tags from the playlistItems endpoint. The only parts available are id, snippet, status and contentDetails and neither of those contain tags.

However!

You do not have to make a request for each video in the playlist! The documentation states:

The id parameter specifies a comma-separated list of the YouTube video ID(s) for the resource(s) that are being retrieved. In a video resource, the id property specifies the video's ID. (string)

That means you can supply multiple, comma-separated video ids to the videos endpoint.

Example:

GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=kOkQ4T5WO9E,a59gmGkq_pw,Io0fBr1XBUA&key={YOUR_API_KEY}

So in total, it will cost you two requests to get the tags of all videos in a playlist.*

*In practice, you might have to make more than two requests. If I remember correctly, YouTube limits the returned items to 50 per request. Thus, if the playlist contains more than 50 videos, you will have to make another request with the pageToken parameter set.