Jinyoung Kim
Jinyoung Kim

Reputation: 1965

How to get playlistItem using videoId and playlistId out of maxResults range?

PlaylistItem is not retrieved by videoId and playlistId. This situation occurs when it is not within maxResults range.

You would get item has data below query.

GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=3&playlistId=PLLALQuK1NDrgb03P3lIgK7SrBdYbuh4_5&videoId=H4lRmVy_qYc&key={YOUR_API_KEY}

You couldn't get item this time.

GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=2&playlistId=PLLALQuK1NDrgb03P3lIgK7SrBdYbuh4_5&videoId=H4lRmVy_qYc&key={YOUR_API_KEY}

Try it here

The only difference thing is just number of maxResults. The playlistItem has index 3, so it was't retrieved.

It is fine unless I'll try to get PlaylistItem which is out of maximum number of maxResults. But I have to do that. It frustrates me a lot.

Is there any workaround?

Upvotes: 1

Views: 218

Answers (1)

paolo
paolo

Reputation: 2538

This behavior of the API is actually pretty confusing, I wouldn't expect something like that to happen. After all, the documentation for the maxResults parameter states:

The maxResults parameter specifies the maximum number of items that should be returned in the result set.

Since the video is in the playlist twice, one should assume that requests with the maxResults parameter set to either 2 or 3 or any number higher than that would return both items.

Possible workaround

I don't really understand why you couldn't just raise the maxResults parameter to 50, but this will work:

First, get a list of all videos in the playlist (without videoId parameter). Set maxResults to 50. That way, you have to do one request for every 50 videos in the playlist, but at least this method is reliable. Make sure to regard any nextPageToken properties in the response. This is the easiest way to get the items which exceeded maxResults.

Optionally, if you do not need further information (like video description, etc.) you can set the fields parameter to items(snippet(position,resourceId/videoId)),nextPageToken. That way, it will only return the necessary information and reduce the size of the response. Your query could look like this:

GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PLLALQuK1NDrgb03P3lIgK7SrBdYbuh4_5&fields=items(snippet(position%2CresourceId%2FvideoId))%2CnextPageToken&key={YOUR_API_KEY}

(the fields parameter was composed using the fields editor, in the "Try it!" area)

Now that you have the video ids of all items in the playlist, simply check if your id is among them.

Upvotes: 1

Related Questions