#given the playlist id this will return a list of the
#of all videos in the playlist
#
def get_videos_in_playlist(self,playlist_id):
list_of_videos=list()
next_page=None
##python does not have a do while loop this is very similar to one
while True:
request=self.api_resource.playlistItems().list(
part="id,snippet,contentDetails,status",
playlistId=playlist_id,
maxResults=50,
pageToken=next_page
)
response=request.execute()
next_page=response["nextPageToken"]
for video in response["items"]:
list_of_videos.append(video["contentDetails"]["videoId"])
if next_page==None:
return list_of_videos
I am trying to write a method in python using the youtube api that will take the ID of a playlist and return the ID's of all videos in the playlist. I expect it to run through each page of 50 videos before returning the list. However, it ends up throwing this error:
Traceback (most recent call last):
File "C:\Users\vdh24\source\repos\GraphAnalysis\youtube_info.py", line 86, in <module>
youtube_info.test()
File "C:\Users\vdh24\source\repos\GraphAnalysis\youtube_info.py", line 20, in test
first_video_id=object.get_videos_in_playlist(id_of_playlist)[0]
File "C:\Users\vdh24\source\repos\GraphAnalysis\youtube_info.py", line 50, in get_videos_in_playlist
response=request.execute()
File "C:\Users\vdh24\AppData\Roaming\Python\Python37\site-packages\googleapiclient\_helpers.py", line 131, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\vdh24\AppData\Roaming\Python\Python37\site-packages\googleapiclient\http.py", line 937, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://youtube.googleapis.com/youtube/v3/playlistItems?part=id%2Csnippet%2CcontentDetails%2Cstatus&playlistId=UUL5YbN5WLFD8dLIegT5QAbA&maxResults=50&pageToken=CN4CEAA&key=AIzaSyD1iei5Po9z1Qu7eSGZdzr357LMpLWnr4U&alt=json returned "The request specifies an invalid page token.". Details: "[{'message': 'The request specifies an invalid page token.', 'domain': 'youtube.parameter', 'reason': 'invalidPageToken', 'location': 'pageToken', 'locationType': 'parameter'}]">
The thread 'MainThread' (0x1) has exited with code 0 (0x0)
I tried using the advice from this: How do I list more than 50 Youtube videos in a channel using nextPageToken? But it seemed to be what I was already doing. This error was particularly confusing because it seemed to occur at a different page each time I ran the code. Thanks in advance.